Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用更新的PHP mysql real escape_Php_Mysql - Fatal编程技术网

使用更新的PHP mysql real escape

使用更新的PHP mysql real escape,php,mysql,Php,Mysql,我正在使用真正的转义字符串来阻止像“引发SQL错误”这样的字符。 这是我代码的一部分,它应该从表单更新。我需要帮助找出如何使用update执行转义字符串 $Comments = mysql_real_escape_string($_POST['Comments']); $id = mysql_real_escape_string($_POST['id']); $sql="UPDATE Worklog SET Comments = '$Comments' WHERE id = '$id'"; my

我正在使用真正的转义字符串来阻止像“引发SQL错误”这样的字符。 这是我代码的一部分,它应该从表单更新。我需要帮助找出如何使用update执行转义字符串

$Comments = mysql_real_escape_string($_POST['Comments']);
$id = mysql_real_escape_string($_POST['id']);
$sql="UPDATE Worklog SET Comments = '$Comments' WHERE id = '$id'";
mysql_query($sql);

    <form action="comments.php?id=<?php echo $stuff['id'];?>" method="post">

    <br />
    Comments: <br />
    <textarea name="Comments"  method="post" cols="20" rows="5" wrap="hard"><?php echo $stuff['Comments']; ?></textarea> <br />

    <br />
    <input type="submit" name="makeitgos" />
    <br />
    <br />
    </form>

    </body>
    </html>
`

您的代码看起来不错,如果您仍在使用mysql库,那么使用mysql\u real\u escape\u字符串非常有用。但是请注意,我建议在将$id应用于SQL语句之前,先检查$id是否为数字,并使用is_numeric$id

此外,您还可以研究如何使用和PDO。使用准备好的语句的优点是MySQL注入的可能性较小。请注意,SQL注入是一个安全漏洞,而不仅仅是一个不便之处,而且您的SQL查询通常会更快一些,尤其是在重用查询时

说到逃避,你真的应该逃避你的,因为这些仍然会导致一些问题,例如,>警报'hai';作为id或注释输入。你应该避免使用这些工具


编辑:答案并不完全符合问题。

我同意利科维卡的观点,PDO似乎让事情变得更容易

我只是在学习PHP,但我相信可以使用分隔符对双引号字符串中的字符进行转义。此外,当我在自己的MySQL数据库上尝试此操作时,除非在变量周围使用双引号,否则查询无法工作。见工作代码:

$sql = "UPDATE Worklog SET Comments = \"$Comments\" WHERE id = \"$id\"";

是否要添加您看到的错误?请注意,mysql_uu函数已被弃用。您可能应该开始使用PDO,因为它将为您处理转义。感谢您的建议,我正在研究PDO,但仍然无法使其工作,您能否使用上面的代码提供一个示例?谢谢。这有什么不同吗?对不起,没有。我固定了我的岗位。