gpt4 book ai didi

php - mysql_real_escape_string 不让字符串通过

转载 作者:行者123 更新时间:2023-11-29 13:48:41 25 4
gpt4 key购买 nike

我正在尝试清理进入数据库的字符串。但使用下面的代码,我没有得到数据库的更新。

第一页以输入形式发布此内容:

$note="Here is some example text";

接收页面:

$note = $_POST['note'];
$note = mysql_real_escape_string($note);
$sql="UPDATE some_table SET notes='$note' WHERE id='$some_id'";
$result=mysql_query($sql);

当我取出 mysql_real_escape_string 行时,它可以工作,但不能用它在那里。我错过了什么?

谢谢!

最佳答案

我强烈建议使用Prepared Statement,mysql_real_escape_string()不会完全保护你免受SQL注入(inject)。

更新示例:

<?php
// connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);

// query
$sql = "UPDATE some_table
SET notes=?
WHERE id=?";
$q = $conn->prepare($sql);
$q->execute(array($$_POST['note'], $some_id));
?>

更多详细信息:http://www.php.net/manual/en/intro.pdo.php

关于php - mysql_real_escape_string 不让字符串通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17034345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com