尝试更新MySQL中的行时bindParam错误

时间:2017-05-06 06:31:46

标签: php mysql mysqli

我想我一直在阅读有关此问题的stackoverflow上的每一个威胁,但我仍然无法弄清楚为什么我会收到此错误。

MySQL数据库由以下列名组成:id,stores,results。

我在表单中有一个按钮。当我单击“选择”按钮时,列名记录应更新为+1。每次我点击按钮我都会收到错误:

Fatal error: Uncaught Error: Call to a member function bindParam() on boolean in /Applications/MAMP/htdocs/project/updaterecords.php:12 Stack trace: #0 {main} thrown in /Applications/MAMP/htdocs/project/updaterecords.php on line 12

有谁能看到这里出了什么问题?

HTML

<form action="updaterecords.php" method="post">
  <input type="hidden" value="333" name="id" />
  <button type="submit" name="selectStore" >Select</button>
  <button type="button" data-dismiss="modal">Close</button>
</form>

updaterecords.php

    <?php
    include 'dbconnection.php';

    if(isset($_POST['selectStore'])) {
    $id = $_POST['id']; // Line 8

        $stmt = $mysqli->prepare("UPDATE stores SET records = records + 1 WHERE id = :id");

            $stmt->bindParam(':id', $id); //line 12

        if ($stmt->execute()) { 
            $success = true;
        }

        $stmt->close();

        $mysqli->close();   
        if($success) {
            echo "Updated Succesfull";
        } else {
            echo "Failed: " .  $stmt->error;
          }
        }

    ?>

1 个答案:

答案 0 :(得分:0)

PDO中使用了

bindParam。在mysqli中,我们使用bind_param

并且

  

mysqli不支持命名占位符。

将代码重写为

 $stmt = $mysqli->prepare("UPDATE stores SET records = records + 1 WHERE id = ?");
 $stmt->bind_param('i', $id);
 $stmt->execute();