Jump to content

"error in your SQL syntax"


mikebyrne

Recommended Posts

Im getting the error and cant spot my mistake

 

error in the queryYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIKE '%%'' at line 1

 


<?PHP
include("adminconnect.php");
$tbl_name = "product";

$cat = mysql_real_escape_string($_POST['cat']);
$input =
mysql_real_escape_string($_POST['searchfield']);

$query = "SELECT * FROM $tbl_name WHERE $cat LIKE '%$input%'";
$result = mysql_query($query) or die ("error in the query" . mysql_error());
if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_array) {
         $product = $row['product'];
         echo "$product was found<br>";
     }
} else {
    echo "No search results found";
}
?>
<select name="cat" onChange="setAction(this.options[this.selectedIndex].value);"> 
<option value="cd" selected="selected" >CD</option>
<option value="dvd" >DVD</option>
<option value="game" >Game</option>
</select>
<form name="ex2" method="get" action="searchresults.php">

<input type="text" name="searchfield" size="22" maxlength="40" id="srchdrop" />
<input type="submit" value="Search Now! »" id="gosrch" />

</td></tr>
</form>

Link to comment
Share on other sites

Ive tried this but it doesnt seem to be showing the varible but the page wont load so the problem seems to be with the line

 

$query = "SELECT * FROM $tbl_name WHERE $cat LIKE '%$input%'";

 

 

 

<?PHP
include("adminconnect.php");
$tbl_name = "product";

$cat = mysql_real_escape_string($_POST['cat']);
$input =
mysql_real_escape_string($_POST['searchfield']);

$query = "SELECT * FROM $tbl_name WHERE $cat LIKE '%$input%'";
$result = mysql_query($query) or die ("error in the query" . mysql_error());
echo $query;
if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_array) {
         $product = $row['product'];
         echo "$product was found<br>";
     }
} else {
    echo "No search results found";
}
?>
<select name="cat" onChange="setAction(this.options[this.selectedIndex].value);"> 
<option value="cd" selected="selected" >CDs</option>
<option value="dvd" >DVDs</option>
<option value="game" >Games</option>
</select>
<form name="ex2" method="get" action="searchresults.php">

<input type="text" name="searchfield" size="22" maxlength="40" id="srchdrop" />
<input type="submit" value="Search Now! »" id="gosrch" />

</td></tr>
</form>
</table>

 

Link to comment
Share on other sites

Try changing

 

$query = "SELECT * FROM $tbl_name WHERE $cat LIKE '%$input%'";

 

to

 

$query = "SELECT * FROM $tbl_name WHERE $cat LIKE '%".$input."%'";

 

if that doesn't work I would be monitoring the value of $input through your script to see what value is passed into the $query string.

Link to comment
Share on other sites

<form name="ex2" method="get" action="searchresults.php">

 

$cat = mysql_real_escape_string($_POST['cat']);

 

$input = mysql_real_escape_string($_POST['searchfield']);

 

Since the form method is GET there aren't going to be any POST variables.

 

Your form is also mangled. The select option needs to be inside the form tags. The onChange serves no purpose then.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.