The code below allows the user to enter in a phrase in plain English, which is then added to a database as “$site”. If the user enters in an apostrophe, the phrase is stored with a backslash in front of the apostrophe. How can I get the variable “$site” to be added to the database without backslashes in front of apostrophes?
print "<div class=\"siteadd\">
<form action='process.php?find=$find1' method='post'>
Add a book to this topic: <input name='site' type='text' size='50'>
<input type='submit' value='Submit'>
</form>
</div>";
Then, in process.php:
$site = str_replace($remove_array, "", $_POST['site']);
$site = strtolower($site);
$site = mysql_real_escape_string($site);
$illegal = array("/", "\"");
$site = str_replace($illegal, '', $site);
mysql_query("INSERT INTO `$find` VALUES (NULL, '$site',1,0)");
I assume the backslash is added by PHP for security reasons. Read more about magic quotes. And since you’re using the proper function to escape strings passed to mysql queries, you don’t have to rely on PHP’s dummy escaping.
At the beginning of the script, check if
magic_quotesare on, and if so, remove the slashes:BTW, in your code,
$findvariable comes from an untrusted source and should be escaped/filtered as well.