php SQL状态[HY093]:未定义参数[重复]

4ioopgfo  于 2023-01-29  发布在  PHP
关注(0)|答案(2)|浏览(140)
    • 此问题在此处已有答案**:

SQLSTATE[HY093]: Invalid parameter number: parameter was not defined(4个答案)
12小时前关门了。
我是PDO的新手。我被卡住了,现在我在这里。我尝试了基本原油
PDO和获取错误无效参数:未定义参数
从更新部分。请帮助我?

public function update_by_id($data) {
    $id = $data['student_id'];
    $name = $data['student_name'];
    $department = $data['department'];
    $email = $data['email_address'];
    $city = $data['city'];
    try {
        $sql = "UPDATE tbl_student SET student_name=:name, department=:department, email_address=:email, city=:city WHERE student_id='$id' ";

        $stmt = $this->db_conn->prepare($sql);

        $stmt->bindParam(':name', $name, PDO::PARAM_STR);
        $stmt->bindParam(':department', $department, PDO::PARAM_STR);
        $stmt->bindParam(':email_address', $email, PDO::PARAM_STR);
        $stmt->bindParam(':city', $city, PDO::PARAM_STR);

        if($stmt->execute()) {
            header('Location: view_student.php');
        }
        else {
            echo $this->db_conn->errorInfo();
        }

    } catch (Exception $e) {
        echo $e->getMessage();
    }

}
umuewwlo

umuewwlo1#

使用:email代替:email_address

$stmt->bindParam(':email', $email, PDO::PARAM_STR);

因为在查询中绑定email_address=:email,
您也可以在代码中绑定$id

$sql = "UPDATE tbl_student SET student_name=:name, department=:department, email_address=:email, city=:city WHERE student_id=:id";
    $stmt = $this->db_conn->prepare($sql);

    $stmt->bindParam(':name', $name, PDO::PARAM_STR);
    $stmt->bindParam(':department', $department, PDO::PARAM_STR);
    $stmt->bindParam(':email', $email, PDO::PARAM_STR);
    $stmt->bindParam(':city', $city, PDO::PARAM_STR);
    $stmt->bindParam(':id', $id, PDO::PARAM_INT);
tjjdgumg

tjjdgumg2#

变更

$stmt->bindParam(':email_address', $email, PDO::PARAM_STR);

$stmt->bindParam(':email', $email, PDO::PARAM_STR);

当你使用

.., email_address=:email,...

相关问题