Mau*_*o74 8 php mysql escaping insert
我对PHP很新,所以很抱歉,如果这听起来很容易...... :)
我将包含引号的内容插入到我的数据库中时出现错误消息.这是我试图逃避报价但没有奏效的东西:
$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');
Run Code Online (Sandbox Code Playgroud)
你能提供任何解决方案吗?
提前致谢.
毛罗
Tom*_*lak 13
请开始使用准备好的参数化语句.它们消除了对任何SQL转义问题的需要,并关闭了字符串连接的SQL语句保持打开的SQL注入漏洞.此外,它们更易于使用,并且在循环中使用时速度更快.
$con = new mysqli("localhost", "u", "p", "test");
if (mysqli_connect_errno()) die(mysqli_connect_error());
$sql = "INSERT INTO articles (title, body, date) VALUES (?, ?, NOW())";
$stmt = $con->prepare($sql);
$ok = $stmt->bind_param("ss", $_POST[title], $_POST[body]);
if ($ok && $stmt->execute())
header('Location: index.php');
else
die('Error: '.$con->error);
Run Code Online (Sandbox Code Playgroud)
Fla*_*in3 12
它应该没有sprintf的东西工作
$title = mysql_real_escape_string($_POST[title]);
$body = mysql_real_escape_string($_POST[body]);
Run Code Online (Sandbox Code Playgroud)