为什么这个准备好的语句会抛出错误?

 1076263105_69ae53 发布于 2023-02-09 11:33

在学习了PHP和MySQL的基础知识之后,我现在正在学习如何使用预准备语句来防止SQL注入攻击.我有以下代码:

for ($i = 0; $i < $delegateno ;$i++){

        $q = "INSERT INTO delegates (delegate_id,booker_name, booker_email, booker_tel, booker_company, delegate_name, delegate_email, delegate_tel) VALUES (NULL, ?, ?, ?, ?, ?, ?, ? )";//Insert delegate information into delegate tables
        $stmt = mysqli_query($dbc, $q);
        mysqli_stmt_bind_param($stmt,'sssssss', $fullname, $email, $tel, $company,$delegatename[$i],$delegateemail[$i],$delegatetel[$i]);
        mysqli_stmt_execute($stmt);
}

但是,这会引发:

Notice: Query: INSERT INTO delegates (delegate_id,booker_name, booker_email, booker_tel, booker_company, delegate_name, delegate_email, delegate_tel) VALUES (NULL, ?, ?, ?, ?, ?, ?, ? )
MySQL Error: 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 '?, ?, ?, ?, ?, ?, ? )'

我究竟做错了什么?

1 个回答
  • 因为您正在执行包含占位符的原始查询.您需要先prepare()查询.

    这是它(通常)的方式:prepare- > bind_param- > execute- > fetch.

    更改:

    $stmt = mysqli_query($dbc, $q);
    

    至:

    $stmt = mysqli_prepare($dbc, $q);
    

    2023-02-09 11:36 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有