简体   繁体   English

致命错误:未捕获的 PDOException:获取错误的值

[英]Fatal error: Uncaught PDOException: get wrong values

I tried to create an abstract model, but I have an error in this abstract.php file:我试图创建一个抽象模型,但我在这个abstract.php文件中有一个错误:

public function create()
{
    global $connection;
    $sql = 'INSERT INTO ' . static::$tableName . ' SET ' . self::buildNameParameterSQL();
    $stmt = $connection->prepare($sql);

    foreach(static::$tableSchema as $col=>$type){

        if($type == 4){
           $sanitizedValue = filter_var($this->$col, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
           $stmt->bindValue(":{$col}", $sanitizedValue);
        }
        else{
            $stmt->bindValue(":{$col}", $this->$col, $type); // Error here
        }
    }
    return $stmt->execute();
}

And here is my class employee in employee.php that extends this abstract model:这是我在employee.php 中的班级员工,它扩展了这个抽象模型:

require_once 'abstract.php';

cLass Employee extends AbstractModel
{
    private $id;
    private $name;
    private $age;
    private $address;
    private $tax;
    private $salary;


    protected static $tableName = 'employees';
    protected static $tableSchema = array (
        'name' => self::DATA_TYPE_STR,
        'age' => self::DATA_TYPE_INT,
        'address' => self::DATA_TYPE_STR,
        'tax' => self::DATA_TYPE_DECIMAL,
        'salary' => self::DATA_TYPE_DECIMAL
    );

    public function __construct($name, $age, $address, $tax, $salary){

        global $connection;

        $this->name = $name;
        $this->age = $age;
        $this->address = $address;
        $this->tax = $tax;
        $this->salary = $salary;
    }

And when I created a new object of class employee and invoke a created method当我创建了一个员工类的新对象并调用了一个创建的方法时

<?php
    require_once ('db.php');
    require_once ('employee.php');

    $emp = new Employee("abanoub", 21, "Cairo, Egypt", 1.03, 5000);
    var_dump($emp->create());

And here my error这里是我的错误

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;致命错误:未捕获的 PDOException:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误; check the manual that corresponds to your MariaDB server version for the right syntax to use near ': name, address =: address, salary =: salary, tax =: tax, age =: age' at line 1 in C:\\xampp\\htdocs\\test\\pdo\\abstract.php:47 Stack trace: #0 C:\\xampp\\htdocs\\test\\pdo\\abstract.php(47): PDOStatement->execute() #1 C:\\xampp\\htdocs\\test\\pdo\\test.php(6): AbstractModel->create() #2 {main} thrown in C:\\xampp\\htdocs\\test\\pdo\\abstract.php on line 47检查与您的 MariaDB 服务器版本相对应的手册,了解在 C:\\xampp\\ 中第 1 行的“: name, address =: address,salary =:salary, tax =: tax, age =: age”附近使用的正确语法htdocs\\test\\pdo\\abstract.php:47 堆栈跟踪:#0 C:\\xampp\\htdocs\\test\\pdo\\abstract.php(47): PDOStatement->execute() #1 C:\\xampp\\htdocs\\test \\pdo\\test.php(6): AbstractModel->create() #2 {main} 在 C:\\xampp\\htdocs\\test\\pdo\\abstract.php 第 47 行抛出

It seems like spaces are rendered at the wrong place似乎空间呈现在错误的位置

'name =: name, address =: address, salary =: salary, tax =: tax, age =: age'

should be 'name = :name, address = :address, salary = :salary, tax = :tax, age = :age'应该是'name = :name, address = :address, salary = :salary, tax = :tax, age = :age'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM