插入致命错误函数bind_param()

时间:2017-02-15 11:34:41

标签: php mysql

我在INSERT查询

上收到错误消息
  

致命错误:在...

中的boolean上调用成员函数bind_param()

虽然我只是将字符串插入到期望字符串值为:: VARCHAR(255)

的表列中

我的表格如下:

+---------------+--------------+-----+------+---+--+--+
| id            | int(11)      | NO  | PRI  |   |  |  |
| username      | varchar(45)  | YES | UNI  |   |  |  |
| email_address | varchar(45)  | YES | UNI  |   |  |  |
| password      | varchar(255) | YES |      |   |  |  |
| role          | int(11)      | YES | NULL | 0 |  |  |
| dashboard_id  | int(11)      | YES | NULL |   |  |  |
+---------------+--------------+-----+------+---+--+--+

我的Php看起来像这样......

    $stmt = $conn->prepare("INSERT INTO users VALUES (?, ?, ?)");
    $stmt->bind_param('sss', $username, $emailAddress, $upassword);

    $username = $_POST['username'];
    $emailAddress = $_POST['email'];
    $upassword = password_hash($_POST['upassword'], PASSWORD_DEFAULT);

    $stmt->execute();

    printf("%d Row inserted.\n", $stmt->affected_rows);

    $stmt->close();
    $conn->close();

更新

我的连接代码:

$conn = new mysqli($db_host,$db_user,$db_password,$db_name);

1 个答案:

答案 0 :(得分:2)

这是在我的本地工作: -

表格结构: -

Name            Type            Null    Default  Extra
id              int(11)         No      None     AUTO_INCREMENT  Primary Key
user_name       varchar(256)    No      None    
email_address   varchar(256)    No      None
password        varchar(256)    No      None
role            int(11)         Yes     0
dashboard_id    int(11)         Yes     0

Php代码: -

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);

$conn = new mysqli('*****','***','****','****');
$conn->set_charset("utf8");
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
$_POST['username'] = 'anant'; // i have hard coded for checking
$_POST['email'] = 'anant@gmail.in';// i have hard coded for checking
$_POST['upassword'] = 'anant@123';// i have hard coded for checking
if(!empty(trim($_POST['username'])) && !empty(trim($_POST['email'])) && !empty(trim($_POST['upassword']))){
 $username = $_POST['username'];
 $emailAddress = $_POST['email'];
 $upassword = password_hash($_POST['upassword'], PASSWORD_DEFAULT);

  if($stmt = $conn->prepare("INSERT INTO users (user_name,email_address,password) VALUES (?, ?, ?)")){

      $stmt->bind_param('sss', $username, $emailAddress, $upassword);

      $stmt->execute();
      $stmt->close();
    }else{
        echo $conn->error;
    }
  $conn->close();
}else{
  echo "form values are not coming";
}
?>

注意: -

我遇到了问题,它是关于不在prepare()内提供列名。当我echo $conn->error;时,我收到了此错误Column count doesn't match value count at row 1

所以当我提供相应的列名时一切正常并开始工作。

希望它有所帮助。