Re: drop down list and mysql
The problem is this line
PHP Code:
echo ("<option value=".$nm['name'].">".$nm['name']."</option>");
When rendered it will look like this:
Code:
<option value=John Smith>John Smith</option>
Do you see the problem? The value needs to be in quotes because there could be spaces in the name. so change it to:
PHP Code:
echo ("<option value=\"".$nm['name']."\">".$nm['name']."</option>");
or
PHP Code:
echo ("<option value='".$nm['name']."'>".$nm['name']."</option>");
One other thing... your script is vulnerable to attacks via SQL Injection. See here for how to prevent SQL Injection in PHP.
Hope this helps:thumb:
Re: drop down list and mysql
Thanks for pointing out the mistake.
Coming to the code injection part, I did go through the article in the link; which of the two is better to screen the user input"
mysql_real_escape_string OR stripslashes
Re: drop down list and mysql
stripslashes() will do the opposite of mysql_real_escape_string(). It will remove the slashes that were added. Are you thinking of addslashes()? addslashes() will appear to do the same as mysql_real_escape_string() but I have heard addslashes() can be exploited.
I use mysql_real_escape_string() myself but there are other ways, such as sprintf() which checks the data type of the inputs, e.g. when using an ID number, you can tell sprintf to not allow a string to be placed in that part of the query.
Hope this helps!
Re: drop down list and mysql
I started playing with prepared statements lately. Pretty much solve all of this problems.