PDA

View Full Version : Searching for a match from a text box.


EvilCensor
8-21-09, 11:46 AM
Whilst using PHP to process a form I'm trying to match a text entry from a text box, however I'm not having much luck - the $charname part below is the part I'm unsure of:

$query = "SELECT * FROM character_01 WHERE name = $charname"; = fails

If I enter a numeric value in the text box corresponding to $charname and compare it with the id field I get successful matches

$query = "SELECT * FROM character_01 WHERE id = $charname"; = success

The aim is to find the name entered into the textbox in the mysql db.

Apologies for a messy explaination of my problem.

satis
8-21-09, 12:56 PM
if it's an integer, your format is correct. If it's a string/varchar/whatever, then it's wrong. Shoudl be

$query = "SELECT * FROM character_01 WHERE name='$charname'";

Note the single quotes. However be careful.. if you're just taking something from an input field in a form and putting it in a query like that, it's possible to do something called SQL injection.

http://en.wikipedia.org/wiki/SQL_injection

and some hands-on examples if you care
http://unixwiz.net/techtips/sql-injection.html

SQL injection is a BIG deal. A very large percentage of the hacks that happen on websites is due to SQL injection vulnerabilities.

EvilCensor
8-21-09, 01:14 PM
Thanks, I can't believe of all the things I tried I didn't try that.

Will read up on issue you mention also thank you.