I have an input field that should perform auto complete whenever a user types an entry. the entries will be looked up on the database and then returned on the input field as the user types in. everytime I type an entry on the input field I keep getting an error message: Warning division by Zero in. The culprit in the code is this part: if(strlen($queryString) > 0) { and while ($row = mysql_fetch_array($result)).
The php code below is that part that checks that the user types an entry and then performs the look up:
Any idea how to solve the issue?
thanks in advance
<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die
// db connection stuff
if(isset($_POST['queryString'])) {
$queryString = $_POST['queryString'];
if(strlen($queryString) > 0) {
$sql = 'SELECT * FROM my table WHERE destination LIKE '%$queryString%' ' ;
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
<li onclick="fill('<?php echo $row[destination]; ?>');"><?php echo $row[destination]; ?></li>
?>
may be spoken too soon.
There is another problem with sql injection. In order to avoid the sql injection, I added the escape string bit, but it doesn t like it. I get the following error message:
call to undefined function in escape string for the following part of the code:
The command real_escape_string doesn’t exist. It is mysql_real_escape_string that should be used instead. You should only use it at the last possible moment, as is shown in the example on its documentation page.
Here is an appropriate way to do it.
$sql = sprintf(
"SELECT * FROM my table WHERE destination LIKE '%s' ",
mysql_real_escape_string('%' . $queryString . '%')
);
You could also do it like this, where $queryString is set up before the database command:
$queryString = '%' . $queryString . '%';
$sql = sprintf(
"SELECT * FROM my table WHERE destination LIKE '%s' ",
mysql_real_escape_string($queryString)
);
Or even like this, where it’s all done within sprintf itself, where sprintf converts the double percentages to a single percentage:
$sql = sprintf(
"SELECT * FROM my table WHERE destination LIKE '%%%s%%' ",
mysql_real_escape_string($queryString)
);
Look at the single quotes. There are four of them.
The first quote delimits the start of the string, which is okay.
The second quote ends the string, when instead you need to represent the quote within the overall string, instead of saying that you want to close the string.
The simplest change you can make to make your original code work, but does not protect your database any better, is to delimit the string with doublequotes:
$sql = "SELECT * FROM my table WHERE destination LIKE '%$queryString%' ";
I don’t think that it’s a good idea though to tie disabling magic quotes to the mysql_real_escape_string function, because there are situations where you should disable magic quotes without the mysql_real_escape_string function being involved.
You could just automatically remove magic quotes. The code for it is on the disabling magic quotes web page, which is useful when you do not have the full control to disable magic quotes on the server.
Also, the values from $_POST might already be being escaped before you get your hands on them, with something called magic quotes.
If magic quotes are being used, here is what happens:
[list=1][]O’Brian - typed in on the web page
[]O\'Brian - in $_POST thanks to magic quotes
[*]O\\\'Brian - after mysql_real_escape_string[/list]
Why use mysql_real_escape_string when magic quotes are already being used? Because over time it was realised that magic quotes only provides partial protection.
There are two ways to deal with this issue. One is to disable magic quotes on your web server.
If you cannot disable magic quotes, you can strip the slashes from the variable instead.
[list=1][]O’Brian - typed in on the web page
[]O\'Brian - in $_POST thanks to magic quotes
[]O’Brian - after stripslashes
[]O\'Brian - after mysql_real_escape_string[/list]
Or you could create a function that calls mysql_real_escape_string but before it does that tests for magic_quotes, and than call that function instead of testing for magic_quotes and calling mysql_real_escape_string each time separately:
function my_real_escape_string($string)
{
if (get_magic_quotes_gpc())
$string = stripslashes($string);
return mysql_real_escape_string($string);
}