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 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决