gpt4 book ai didi

php - MySQL 查询故障排除

转载 作者:行者123 更新时间:2023-11-29 15:01:22 25 4
gpt4 key购买 nike

我需要有关此代码的帮助,它不会将值插入到我的数据库中。可能是一个简单的问题,但在瑞典已经很晚了,所以如果有人可以看看这个并告诉我出了什么问题,我会很感激:

    include "../../inc/mysql_config.php";
$to = mysql_real_escape_string($_POST['to']);
$message = mysql_real_escape_string($_POST['message']);

mysql_query("INSERT INTO messages (to, message) VALUES ('".$to."', '".$message."')");

每个变量都有一个值,仔细检查该值并且 mysql_config.php 正在工作。

MySQL 错误代码:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'to, message) VALUES ('hubb', 'asd')' at line 1

最佳答案

可以给你提供线索的是使用 mysql_error() 函数,例如

mysql_query("INSERT INTO messages (to, message) 
VALUES ('".$to."', '".$message."')") or die(mysql_error());

我建议使用更新的库,例如 MySQLi 或 PDO(最好是 PDO)。这两者都支持准备好的语句,这使得整个字符串转义变得更加容易和一致。

关于php - MySQL 查询故障排除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3002704/

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