我试图创建一个抽象模型,但我有一个错误 这个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();
}`
这里是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;
}
当我创建一个类employee的新对象并调用一个创建的方法
时<?php
require_once ('db.php');
require_once ('employee.php');
$emp = new Employee("abanoub", 21, "Cairo, Egypt", 1.03, 5000);
var_dump($emp->create());
这是我的错误
致命错误:未捕获PDOException:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法出错;检查与您的MariaDB服务器版本相对应的手册,以获得正确的语法,以便使用&#39;:name,address =:address,salary =:salary,tax =:tax,age =:age&#39;在C:\ xampp \ htdocs \ test \ pdo \ abstract.php的第1行:47堆栈跟踪:#0 C:\ xampp \ htdocs \ test \ pdo \ abstract.php(47):PDOStatement-&gt; execute() #1 C:\ xampp \ htdocs \ test \ pdo \ test.php(6):在线上C:\ xampp \ htdocs \ test \ pdo \ abstract.php中抛出的AbstractModel-&gt; create()#2 {main} 47
答案 0 :(得分:0)
似乎空格在错误的地方呈现
res - Strings
应为'name =: name, address =: address, salary =: salary, tax =: tax, age =: age'