Page 1 of 4 123 ... LastLast
Results 1 to 10 of 33
  1. #1

    Post how to SEARCH a record from multiple tables using mysql


    Hi everyone,

    I would like to ask your expertise.
    The code writtten to search for a record(name) from multiple tables is this:

    $query="SELECT * FROM `table1`,`table2` WHERE `table1.name,table2.name` like'". $_POST["searchword"]."' ;";

    This is as far as I know. And the tables are independent of each other. But when the <search> is being tested, the error that appears is: "Could not connect: Unknown column 'table1.name,table2.name' in 'where clause' ".
    What would be the possible solution for this?

    Thanx in advance

  2. #2
    Quote Originally Posted by sogate View Post
    Hi everyone,

    I would like to ask your expertise.
    The code writtten to search for a record(name) from multiple tables is this:

    $query="SELECT * FROM `table1`,`table2` WHERE `table1.name,table2.name` like'". $_POST["searchword"]."' ;";

    This is as far as I know. And the tables are independent of each other. But when the <search> is being tested, the error that appears is: "Could not connect: Unknown column 'table1.name,table2.name' in 'where clause' ".
    What would be the possible solution for this?

    Thanx in advance
    $search = $_POST['searchword'];
    $query = "SELECT t1.id,name1,t2.id,t2.name2 FROM t1 left join t2 on t1.id= t2.id where table1.name like $search and table1.name like $search";
    or
    $query = "SELECT t1.id,name1,t2.id,t2.name2 FROM t1 left join t2 on t1.name = t2.name where table1.name like $search";
    Last edited by emailroy2002; 02-24-2010 at 01:39 PM.

  3. #3
    Because we are poor, shall we be vicious? vern's Avatar
    Join Date
    Feb 2003
    Gender
    Male
    Posts
    5,790
    On a side note, I hope you seriously aren't using $_POST directly.

  4. #4
    Quote Originally Posted by vern View Post
    On a side note, I hope you seriously aren't using $_POST directly.
    mao...
    vulnerable kaayo na ug sql injection...

    here's a sample code to prevent your page from sql injection

    PHP Code:
    if(get_magic_quotes_gpc()) {
                
    $product_name        stripslashes($_POST['product_name']);
                
    $product_description stripslashes($_POST['product_description']);
    }

    else {
                
    $product_name        $_POST['product_name'];
                
    $product_description $_POST['product_description'];
    }

    // Make a safe query
    $query sprintf("INSERT INTO products (`name`, `description`, `user_id`) VALUES ('%s', '%s', %d)",
                        
    mysql_real_escape_string($product_name),
                        
    mysql_real_escape_string($product_description),
                        
    $_POST['user_id']); 

  5. #5
    Quote Originally Posted by sogate View Post
    Hi everyone,

    I would like to ask your expertise.
    The code writtten to search for a record(name) from multiple tables is this:

    $query="SELECT * FROM `table1`,`table2` WHERE `table1.name,table2.name` like'". $_POST["searchword"]."' ;";

    This is as far as I know. And the tables are independent of each other. But when the <search> is being tested, the error that appears is: "Could not connect: Unknown column 'table1.name,table2.name' in 'where clause' ".
    What would be the possible solution for this?

    Thanx in advance
    by the way, the situation here is this: first, the name is being searched in table 1 and if not found, then it will search the name to other table which is table 2. I hope this will clarify things.

  6. #6
    Quote Originally Posted by xiao_xiao View Post
    snip
    Thanks for this one. I regularly use POST for variables used once and i'm gonna stop using it. It never came into my mind that using POST is vulnerable to injection however I was critical about sql injection.

    By the way, on original post. You can use something like SELECT a.Name, b.Address FROM Name_Details AS a, Address_Details AS b WHERE <insert conditions here>. You can use this method so you won't have to retype the table name all over again.
    Last edited by personalmgt; 02-24-2010 at 03:11 PM.

  7. #7
    Quote Originally Posted by personalmgt View Post
    Thanks for this one. I regularly use POST for variables used once and i'm gonna stop using it. It never came into my mind that using POST is vulnerable to injection however I was critical about sql injection.

    By the way, on original post. You can use something like SELECT a.Name, b.Address FROM Name_Details AS a, Address_Details AS b WHERE <insert conditions here>. You can use this method so you won't have to retype the table name all over again.
    Thanx bro, but can you please give more details on the 'where clause' because i think the main error is found there.

  8. #8
    Quote Originally Posted by xiao_xiao View Post
    mao...
    vulnerable kaayo na ug sql injection...

    here's a sample code to prevent your page from sql injection

    PHP Code:
    if(get_magic_quotes_gpc()) {
                
    $product_name        stripslashes($_POST['product_name']);
                
    $product_description stripslashes($_POST['product_description']);
    }

    else {
                
    $product_name        $_POST['product_name'];
                
    $product_description $_POST['product_description'];
    }

    // Make a safe query
    $query sprintf("INSERT INTO products (`name`, `description`, `user_id`) VALUES ('%s', '%s', %d)",
                        
    mysql_real_escape_string($product_name),
                        
    mysql_real_escape_string($product_description),
                        
    $_POST['user_id']); 
    Thanx very much bro for giving tips on the vulnerability issue regarding sql injection.

  9. #9
    Use the OR condition instead of comma or "AND"?

    WHERE table1.name LIKE $_POST["searchword"] OR table2.name LIKE $_POST["searchword"]

    PS, dont use the post(in my WHERE clause) above. be creative

  10. #10
    Quote Originally Posted by personalmgt View Post
    Use the OR condition instead of comma or "AND"?

    WHERE table1.name LIKE $_POST["searchword"] OR table2.name LIKE $_POST["searchword"]

    PS, dont use the post(in my WHERE clause) above. be creative
    oh of course i tried OR and AND already but same error appears : Could not connect: Unknown column 'name' in 'where clause'

  11.    Advertisement

Page 1 of 4 123 ... LastLast

Similar Threads

 
  1. How to remove a site from Google search results.
    By the_wandering_slasher in forum Networking & Internet
    Replies: 5
    Last Post: 02-05-2011, 04:11 PM
  2. How to convert files recorded in mini DV digicam to DVD?
    By sikput in forum Computer Hardware
    Replies: 8
    Last Post: 08-29-2006, 01:01 PM
  3. How to save video streaming from youtube.com
    By koto in forum Software & Games (Old)
    Replies: 29
    Last Post: 03-26-2006, 10:54 PM
  4. Replies: 0
    Last Post: 01-28-2006, 01:48 PM
  5. How to make a connection from my 6600 to computer?
    By rey04 in forum Gizmos & Gadgets (Old)
    Replies: 7
    Last Post: 12-03-2005, 05:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top