php 致命错误:对[duplicate]中的布尔值调用成员函数bind_param()

bihw5rsg  于 2022-12-02  发布在  PHP
关注(0)|答案(3)|浏览(139)

此问题在此处已有答案

What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such(1个答案)
七年前就关门了。

代码

if(isset($_POST['create'])){
    $fname      = trim($_POST['fname']);
    $lname      = trim($_POST['lname']);
    $ftname         = trim($_POST['ftname']);
    $mtname         = trim($_POST['mtname']);
    $date_of_admission = trim($_POST['date_of_admission']);
    $date_of_birth = trim($_POST['date_of_birth']);
    $photo_location         = trim($_POST['photo_location']);
    $address        = trim($_POST['address']);
    $phone      = trim($_POST['phone']);
    $sex    = trim($_POST['sex']);
    $nationality    = trim($_POST['nationality']);
    $religion   = trim($_POST['religion']); 

    if(empty($fname) && empty($lname) &&empty($ftname) &&empty($mtname) &&empty($date_of_admission) && empty($phone) && empty($sex)){
        $error = "You must fill all fields.";
    }else{
        $insert = $db->prepare("INSERT INTO st_info (fname,  lname,  ftname,  mtname,  date_of_birth,  date_of_admission,  photo_location, address, phone, sex, nationality, religion,joined) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())");
        $insert->bind_param('sssssssssddd',$fname,$lname,$ftname,$mtname,$date_of_birth,$date_of_admission,$photo_location,$address,$phone,$sex,$nationality,$religion);
        if($insert->execute()){
            //$success = "st_info added successfully!";
            header("location:index.php");
        }
    }
}

错误

致命错误:对第26行C:\xampp\htdocs\create.php中的布尔值调用成员函数bind_param()

rxztt3cl

rxztt3cl1#

看起来像$db-〉prepare()返回一个布尔值而不是一个对象。

if(isset($_POST['create'])){
    $fname      = trim($_POST['fname']);
    $lname      = trim($_POST['lname']);
    $ftname         = trim($_POST['ftname']);
    $mtname         = trim($_POST['mtname']);
    $date_of_admission = trim($_POST['date_of_admission']);
    $date_of_birth = trim($_POST['date_of_birth']);
    $photo_location         = trim($_POST['photo_location']);
    $address        = trim($_POST['address']);
    $phone      = trim($_POST['phone']);
    $sex    = trim($_POST['sex']);
    $nationality    = trim($_POST['nationality']);
    $religion   = trim($_POST['religion']);

    if(empty($fname) && empty($lname) &&empty($ftname) &&empty($mtname) &&empty($date_of_admission) && empty($phone) && empty($sex)){
        $error = "You must fill all fields.";
    }else{
        $db->prepare("INSERT INTO st_info (fname,  lname,  ftname,  mtname,  date_of_birth,  date_of_admission,  photo_location, address, phone, sex, nationality, religion,joined) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())");
        $db->bind_param('sssssssssddd',$fname,$lname,$ftname,$mtname,$date_of_birth,$date_of_admission,$photo_location,$address,$phone,$sex,$nationality,$religion);
        if($db->execute()){
            //$success = "st_info added successfully!";
            header("location:index.php");
        }

    }
}
m528fe3b

m528fe3b2#

您的准备语句似乎失败(否则它将返回PDOStatement而不是布尔值)
应使用$db->errorInfo()$db->errorCode()获取错误原因。
原因可能是语句中有一些字段不存在,或者命名错误。

vc6uscn9

vc6uscn93#

预准备语句最常见的错误之一是连接编码。. try

$mysqli->set_charset("utf8");

之后

$mysqli = new mysqli("host", "user", "pass", "db");

相关问题