SQLSTATE [HY093]:参数号无效:未定义参数

时间:2016-01-06 12:47:58

标签: php pdo

我是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();
    }

}

2 个答案:

答案 0 :(得分:1)

而不是:email使用 $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);

{{1}}

答案 1 :(得分:0)

更改

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

.., email_address=:email,...

正在使用

<video id="myVideo" src = "./play.mp4" autoplay loop></video>