Jump to content

Search form returns an error when entering an apostrophie


mattc_uk

Recommended Posts

The dreadful apostrophie problem... This search form returns an error whenever searching with an apostrophie (')

 

Here's the code on the form (html)

 

<td align="center" width="135"><form method="post" action="srch_advert.php"><input type=text name='search' size=15 maxlength=255><br><input type=submit></form></td>

<td align="center" width="135"><form method="post" action="srch_details.php"><input type=text name='search' size=15 maxlength=255><br><input type=submit></form></td>

<td align="center" width="135"><form method="post" action="srch_artist.php"><input type=text name='search' size=15 maxlength=255><br><input type=submit></form></td>

<td align="center" width="135"><form method="post" action="srch_track.php"><input type=text name='search' size=15 maxlength=255><br><input type=submit></form></td>

 

 

and heres the code on srch_advert.php

 

if ($search)  // perform search only if a string was entered.

  {

    mysql_connect($host, $user, $pass) or die ("Problem connecting to Database");

 

    $srch="%".$search."%";

 

    $query = "select * from tvads WHERE advert LIKE '$srch' ORDER BY advert, year DESC, details ASC LIMIT 0,30";

 

$result = mysql_db_query("cookuk_pn", $query);

    if(mysql_num_rows($result)==0) {

        print "<h2>Your search returned 0 Results</h2>";

  }

 

    else if  ($result)

    {

 

 

Link to comment
Share on other sites

Always use the function mysql_real_escape_string() on any user input in mysql queries:

<?php
$srch="%".mysql_real_escape_string($search)."%";
$query = "select * from tvads WHERE advert LIKE '$srch' ORDER BY advert, year DESC, details ASC LIMIT 0,30"; 
?>

 

BTW, please when entering code in this forum, please surround your code with


tags.

 

Ken

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.