gpt4 book ai didi

PHP + MySQL "INSERT INTO"错误

转载 作者:太空宇宙 更新时间:2023-11-03 12:05:33 25 4
gpt4 key购买 nike

我有这个 sql 字符串:

$sql = "INSERT INTO foto (id, nome, check)
VALUES ('', '" . md5($file) . '.' .$Type. "', '" . md5($file2) . '.' .$Type2. "')";

但它返回这个错误:

INSERT INTO foto (nome, check) VALUES ('57f030f902b9fbd6907d1af52ec2a1ba.jpg', '8c96b1254a72dbd080a9b79a9a224865.jpg')
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 'check) VALUES ('57f030f902b9fbd6907d1af52ec2a1ba.jpg', '8c96b1254a72dbd080a9' at line 1

'

md5($file) + $Type = 57f030f902b9fbd6907d1af52ec2a1ba.jpg
md5($file2) + $Type2 = 8c96b1254a72dbd080a9b79a9a224865.jpg

有什么问题?我该如何解决?

最佳答案

check是MySQL保留字

这要求它用反引号括起来,或者在您的表格中将其重命名为 checks

$sql = "INSERT INTO foto (id, nome, `check`)

如果你想重命名它,那么就这样做

$sql = "INSERT INTO foto (id, nome, checks)

不会抛出错误;选择权在你。


注意错误从哪里开始并指向:

>...MySQL server version for the right syntax to use near 'check                                                          ^ starts there

此外,一旦您修复了该问题,您的代码仍将对 SQL injection 开放.
使用 mysqli with prepared statements , 或 PDO with prepared statements它们更安全

关于PHP + MySQL "INSERT INTO"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26807579/

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