If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > SELECT query problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-01-08, 09:02
rvdb86 rvdb86 is offline
Registered User
 
Join Date: Dec 2008
Posts: 2
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?
Reply With Quote
  #2 (permalink)  
Old 12-01-08, 09:12
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
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)
Reply With Quote
  #3 (permalink)  
Old 12-01-08, 09:21
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
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.
Reply With Quote
  #4 (permalink)  
Old 12-01-08, 09:37
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Quote:
Originally Posted by mike_bike_kite
EDIT : Sorry shammat - you must of posted while I was typing.
Happens to me all the time...
Reply With Quote
  #5 (permalink)  
Old 12-01-08, 10:14
rvdb86 rvdb86 is offline
Registered User
 
Join Date: Dec 2008
Posts: 2
hey guys, thanks so much
all i can offer you in return is my gratitude!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On