Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2008
    Posts
    2

    Unanswered: SELECT query problem

    Hi hope some one can help me!
    Let me quickly describe what I am trying to do. I have a stock system and I want to have a simple search where the user sumbits the poduct name, then the script find the product name in the tbl_products and returns the results.

    I have the following SELECT query:
    Code:
    //This code runs if the form has been submitted
    if (isset($_POST['submit'])) { 
    
    $search_product = $_POST['search_product'];
    
    // now we search the products table 
    $query = "SELECT * FROM tbl_products WHERE product_name = $search_product";
    $result = mysql_query($query)
    	or die ("Search Query Fault");
    For some reason this doesn't work
    Any suggestions?

  2. #2
    Join Date
    Nov 2003
    Posts
    2,935
    Provided Answers: 12
    Quote Originally Posted by rvdb86
    I have the following SELECT query:
    I don't see a SQL query, you only posted PHP code.

    What happens if you run the query without PHP?
    Did you print the real SQL query that gets executed?

    product_name sounds like the column could be a VARCHAR column.

    Shouldn't $search_product be quoted then with single quotes inside the PHP code (note that I don't know PHP).

    At least this would be necessary without PHP (because character literals need to be enclosed in single quotes)

  3. #3
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    You don't have quotes around the product name that's being sent to MySQL.
    Code:
    $query = "SELECT * FROM tbl_products WHERE product_name = '$search_product'";
    You could also increase the functionality of the search by allowing the user to just enter a part of the product name using the following SQL :
    Code:
    $query = "SELECT * FROM tbl_products WHERE product_name like '%$search_product%'";
    It wouldn't be a bad idea if you showed the SQL being executed when you get a fail ie :
    Code:
    $result = mysql_query($query)	or die ("Search Query Fault : $query");
    You could also add the error message as well.

    EDIT : Sorry shammat - you must of posted while I was typing.

  4. #4
    Join Date
    Nov 2003
    Posts
    2,935
    Provided Answers: 12
    Quote Originally Posted by mike_bike_kite
    EDIT : Sorry shammat - you must of posted while I was typing.
    Happens to me all the time...

  5. #5
    Join Date
    Dec 2008
    Posts
    2
    hey guys, thanks so much
    all i can offer you in return is my gratitude!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •