Reputation: 61
I have a site and the user inputs a title. I want the title to be able to use any punctuation. My problem is ill have a query:
"INSERT INTO table(title, body) VALUES ('$title','$body')";
where $title and $body are GET vars. What happens it when i put a quote in for the title it acts as if it ends the string and creates and invalid sql query. Say i have
$title = "I'm entering a title";
"INSERT INTO table(title, body) VALUES ('$title','$body')";
//"INSERT INTO table(title, body) VALUES ('I'm entering a title','$body')";
It ends the string. I've tried using all double quotes and escape characters but nothing. Does anyone know a solution?
Upvotes: 1
Views: 2555
Reputation: 2775
There is already a built-in method for this situation... have a look in mysql_escape_string
method... see the below code...
$title = "I'm entering a title";
$title = mysql_escape_string( $title );
// $title === I\'m entering a title
"INSERT INTO table(title, body) VALUES ('$title','$body')";
// "INSERT INTO table(title, body) VALUES ('I\'m entering a title','$body')";
Upvotes: 0
Reputation: 1075
You can try this php function..
mysql_real_escape_string
$title1 = "I'm entering a title";
$title = mysqli_real_escape_string($title1);
"INSERT INTO table(title, body) VALUES ('$title','$body')";
Try with this, hope this ll help you...
Upvotes: 0
Reputation: 1667
Use these two functions while entring data to database and out from database.
/****************************************/
/* Encode special chars */
/* */
/****************************************/
function DBin($string)
{
return trim(htmlspecialchars($string,ENT_QUOTES));
}
/****************************************/
/* Decode special chars */
/* */
/****************************************/
function DBout($string)
{
$string = trim($string);
return htmlspecialchars_decode($string,ENT_QUOTES);
}
Upvotes: 1
Reputation: 362
You should sanitize your variables via PHP first and then send them clean via PDO or MySQLi...
Upvotes: 0