dongpu2727 2019-03-31 00:42
浏览 281
已采纳

prepare()输出bool false,语句对我来说是正确的[重复]

I have a function that executes a prepared statement. I've pinpointed the problem to the SQL code by wrapping the prepare() line with an exception catcher, but I can't figure out what is wrong with it.

This is the function

    function update_table($column, $value, $conn, $email) {
        require "config.php";
        $stmtupdate = $conn->prepare("UPDATE $table SET ? = ? WHERE email = ?");
        $stmtupdate->bind_param("sis", $column, $value, $email);
        $stmtupdate->execute();
        $stmtupdate->close();
    }

This is one of the function calls

update_table("failedCount", 0, $conn, $email);

Error printed to the page

Fatal error: Uncaught Error: Call to a member function bind_param() on boolean in /var/www/html/usenergy/login.php:125 Stack trace: #0 /var/www/html/usenergy/login.php(218): update_table('failedCount', 0, Object(mysqli), 'testmail@test...') #1 {main} thrown in /var/www/html/usenergy/login.php on line 125

column is a string that defines the column name where $value will be set to ($value is integer).

</div>
  • 写回答

2条回答 默认 最新

  • dongmale0656 2019-03-31 00:58
    关注

    You have this:

    $stmtupdate = $conn->prepare("UPDATE $table SET ? = ? WHERE email = ?");
    $stmtupdate->bind_param("sis", $column, $value, $email);
    

    This results in this SQL:

    UPDATE $table
    SET @var1 = @var2 WHERE email = @var3
    

    substituted for example with

    UPDATE $table
    SET 'column' = 123 WHERE email = 'something'
    

    But you want something like

    UPDATE mytable
    SET mycolumn = 123 WHERE email = 'something'
    

    So there are two mistakes: You want a table name, not $table. And you want a column name, not a string bind variable value.

    Use string concatenation:

    $stmtupdate = $conn->prepare("UPDATE ".$table." SET ".$column." = ? WHERE email = ?");
    $stmtupdate->bind_param("is", $value, $email);
    

    (I hope I'm not mistaken. It's been some time since I used PHP with SQL.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配