-1

嗨,我在 mysql 中有一个表,有六个字段

ID, mobileNumber, firstName, lastName, email, password , rollID

ID 是自动增量的,我想使用 php 插入它

php代码

public function run($firstName, $lastName, $mobileNumber, $email, $password, $rePassword) {
        if (!$this->db->isExistedMobile($mobileNumber)) {
            try {
               $query = "INSERT INTO user (firstName, lastName, mobileNumber, email, password, rollID)
                VALUES (:firstName, :lastName, :mobileNumber, :email, :password, 'rollID')";
                $sth = $this->db->prepare($query);
                $sth->execute(array(
                    ':firstName' => $firstName,
                    ':lastName' => $lastName,
                    ':mobileNumber' => $mobileNumber,
                    ':email' => $email,
                    ':password' => $password,
                    ':rollID' => "roma"
                ));
                header("location: " . URL . "index");
            } catch (PDOException $e) {
                die($e->getMessage());
                exit;
            }
        } else {
            include 'controlers/Error.php';
            $error = new Error();
            $error->index("you enterd a used mobile number");
        }
    }

我有这个例外

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

函数 isExistedMobile 总是返回 false 所以不用担心,我不知道哪里错了,是语法吗?或者在rollID cos中我让它成为静态的?谢谢你的帮忙 :)

4

1 回答 1

2

问题(使用您的原始代码)是参数数组有一个尾随逗号,它在其中创建了一个附加null元素。

我建议rollID按照最初的预期恢复到该版本(使用 的文字值),但请确保在 之后删除尾随逗号$password

public function run($firstName, $lastName, $mobileNumber, $email, $password, $rePassword) {
        if (!$this->db->isExistedMobile($mobileNumber)) {
            $query = "INSERT INTO user (firstName, lastName, mobileNumber, email, password, rollID)
                VALUES (:firstName, :lastName, :mobileNumber, :email, :password, 'ddd')";
            $sth = $this->db->prepare($query);
            $sth->execute(array(
                ':firstName' => $firstName,
                ':lastName' => $lastName,
                ':mobileNumber' => $mobileNumber,
                ':email' => $email,
                ':password' => $password  // , removed from here
            ));
            header("location: " . URL . "index");
        } else {
            include 'controlers/Error.php';
            $error = new Error();
            $error->index("you enterd a used mobile number");
        }
    }
于 2012-05-12T16:23:28.530 回答