#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    56
    Rep Power
    2

    Query on Foreign Key syntax


    Hi,

    I have created a simple query to search database on multiple columns. I have also included a criteria to search on a foreign key in the same table.

    Whe I search on any other string field the query runs just fine but when I search with a foreign key (cat_id) the query does not run.

    Here is my code:

    Code:
    if ($_REQUEST["ref"]<>'') {
    	$search_criteria = " AND (creference LIKE '%".mysql_real_escape_string($_REQUEST["ref"])."%')";
    }
    if ($_REQUEST["cat_id"]<>'') {
    	$search_criteria = " AND cat_id='".($_REQUEST["cat_id"])."'";	
    }
    if ($_REQUEST["cmaterial"]<>'') {
    	$search_criteria = " AND (cmaterial LIKE '%".mysql_real_escape_string($_REQUEST["cmaterial"])."%')";	
    }
    
    $sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE c_id>0".$search_criteria;
    
    $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);

    I need your help.

    Thanx.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,904
    Rep Power
    1045
    Hi,

    Originally Posted by josephbupe
    Whe I search on any other string field the query runs just fine but when I search with a foreign key (cat_id) the query does not run.
    Sorry, but I'm not interested in guessing games. What does MySQL say?

    It might also be a good idea to learn from your previous thread and go through the debugging steps we already mentioned: outputting the query, checking it etc.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    56
    Rep Power
    2
    Ok, I appreciate that.

    Thanx

IMN logo majestic logo threadwatch logo seochat tools logo