I'm quite new to PHP so sorry if sounds such an easy problem... :)
I'm having an error message when inserting content which contains quotes into my db. here's what I tried trying to escape the quotes but didn't work:
$con = mysql_connect("localhost","xxxx","xxxxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$nowdate = date('d-m-Y')
$title = sprintf($_POST[title], mysql_real_escape_string($_POST[title]));
$body = sprintf($_POST[body], mysql_real_escape_string($_POST[body]));
$sql="INSERT INTO articles (title, body, date) VALUES ('$title','$body','$nowdate'),";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
header('Location: index.php');
Could you provide any solution please?
Thanks in advance.
Mauro
Please start using prepared parameterized statements. They remove the need for any SQL escaping woes and close the SQL injection loophole that string-concatenated SQL statements leave open. Plus they are much more pleasing to work with and much faster when used in a loop.
With any database query, especially inserts from a web based application, you should really be using parameters. See here for PHP help on how to use parameters in your queries: PHP parameters
This will help to prevent SQL injection attacks as well as prevent you from having to escape characters.
it should work without the sprintf stuff
Your code
should be as follows
comma should not be there at the end of query