gpt4 book ai didi

php - MySQL/PHP - 无法更新数据库条目

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

目前我正在尝试为自己制作一个迷你博客/cms 类型的东西来测试我的技能并希望通过 PHP 学习一两件事。

所以我有一个表单,里面有一个文本字段。提交后,它应该运行以下查询,但是我收到以下错误...

Resource id #4 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 'cms, description = hello world, maintenance = off, regsi' at line 1

这是该区域的代码...

mysql_query("UPDATE settings SET name = " . $siteName . ", description = " . $siteDesc . ", maintenance = " . $siteMode . " [...] ") or die($settings . "<br/>" . mysql_error());

我使用“[...]”缩短了它,因为它遵循相同的样式(即“test1 = $test1, test2 = $test2”等...)。

有什么帮助吗?谢谢!

最佳答案

您实际上不需要使用 .(连接)运算符关闭和重新打开字符串

php 字符串解析器会将变量插入到字符串中。

所以你可以这样做:

mysqli_query("UPDATE settings SET name = '$siteName', description = ...";

单引号告诉 mysql 将变量视为字符串文字而不是列名。

您还应该做的(如果还没有)转义您的用户输入变量,请参阅 How can I prevent SQL injection in PHP?

你不应该做的是使用 mysql*_ 函数,因为它们已经过时了。看大红框here使用 mysqli*_ 代替

关于php - MySQL/PHP - 无法更新数据库条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25877307/

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