douluokuang7184 2014-11-07 18:19
浏览 74
已采纳

PHP + MySQL“INSERT INTO”错误

I have this sql string:

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

but it returns this error:

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

What's the problem? How can I solve it?

  • 写回答

1条回答 默认 最新

  • doudi2520 2014-11-07 18:21
    关注

    check is a MySQL reserved word

    which requires it to be wrapped in backticks, or rename it to checks for example in your table.

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

    if you want to rename it, then just do

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

    which won't throw an error; the option is yours.


    Notice where the error starts and points to:

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

    Plus, once you've fixed that, your code would still be open to SQL injection.
    Use mysqli with prepared statements, or PDO with prepared statements, they're much safer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同