警告:mysqli_stmt :: bind_param():变量数与数字不匹配

时间:2018-12-20 17:12:55

标签: php mysql

我收到此错误,无法找出原因。

  

警告:mysqli_stmt :: bind_param():第32行的C:\ xampp \ htdocs \ insert.php中已准备好的语句中的变量数量与参数数量不匹配

    $SELECT = "SELECT id FROM heroes WHERE name = ? LIMIT 1";
    $INSERT = "INSERT INTO heroes (id, name, title, bp, ticket, diamond) VALUES ('NULL', '$name', '$title', '$bp', '$ticket', '$diamond')";

    //Prepare statement
    $stmt = $connection->prepare($SELECT);
    $stmt->bind_param("s", $name);
    $stmt->execute();
    $stmt->bind_result($name);
    $stmt->store_result();
    $rnum = $stmt->num_rows;

    if ($rnum==0){
        $stmt->close();
        $stmt = $connection->prepare($INSERT);
        $stmt->bind_param("sssss", $name, $title, $bp, $ticket, $diamond);
        $stmt->execute();
        echo "New hero inserted successfully, sir!";
    } else { 
        echo "There is already a hero with this name, sir!";
    }
    $stmt->close();
    $connection->close();

1 个答案:

答案 0 :(得分:1)

您实际上没有任何要绑定的参数:

$INSERT = "INSERT INTO heroes (id, name, title, bp, ticket, diamond) VALUES ('NULL', '$name', '$title', '$bp', '$ticket', '$diamond')";

执行以下操作:

$INSERT = "INSERT INTO heroes (name, title, bp, ticket, diamond) VALUES (?, ?, ?, ?, ?)";

然后绑定的值替换问号。

还要注意NULL'NULL'之间有很大的区别-后者是一个字符串。如果您具有自动递增的ID字段,请将其保留在插入内容之外,数据库将为您填充它。