0

I have a database of mountains the names of which often contain an apostrophe, e.g. Beinn A'Chroin. All my search criteria work fine except when the user inputs a search via the form with the apostrophe included, i.e. Beinn A'Chroin, and then it throws up an error - Beinn A will work, Chroin will work, but never with the apostrophe. As most users will invariably insert the proper name, including the apostrophe I would prefer always to have it in the table data - Your help appreciated - Thanks John

The relative portion of my code is:

$srch = $_POST['srch'];


// Prepare  query 

// Named Mountain Search Search for:



if ($srch != '') { //Options for specific walk types in a specific area selected

    $query = "SELECT walk, status, distance, report, dateofwalk FROM walkslist WHERE walk LIKE '%$srch%' ORDER BY walk ASC;";
1
  • Try replacing the user's apostrophe with two apostrophes. Not sure of mySQL, but some SQL servers handle apostrophes that way Commented Dec 1, 2015 at 11:43

1 Answer 1

0

update your code use PDO

try {
    $conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);

    $sql = 'SELECT walk, status, distance, report, dateofwalk 
FROM walkslist WHERE walk LIKE ? ORDER BY walk ASC;';

    $q = $conn->prepare($sql);
    $q->execute(array('%$srch%'));
    $q->setFetchMode(PDO::FETCH_ASSOC);

    while ($r = $q->fetch()) {

    }
} catch (PDOException $pe) {
    die("Could not connect to the database $dbname :" . $pe->getMessage());
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Rahautos Thanks for your reply, however when I use the escape string as you suggest I get the following error:
Hi Rahautos Thanks for your reply, however when I test the escape string as you suggest I get the following error: – Deprecated: mysql_real_escape_string(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\wamp\www\Walkslog\dbwalks\srchforA.php on line 46 Any suggestions - Thanks John Call Stack

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.