大家好,我有一个“名称”字段的表“项目”。我正在尝试插入数据,但出现错误。请参阅我的以下代码以获取更多信息。
项目.php
class Item {
public function create(){
$attributes = $this->attributes();
$pair = array();
foreach($attributes as $key => $value){
$pair[] = "'{$key}'=>{$value}";
}
$bind = ":" . implode(", :",array_keys($attributes));
$sql = "INSERT INTO " . static::$table_name
. " (" . join(",",array_keys($attributes)) . " ) VALUES "
. " ( " . $bind . " )";
$stmt = $this->database->prepare($sql);
$result = $stmt->execute($pair);
if($result){
return $result;
} else {
$error = $this->database->errorInfo();
return $error;
}
}
}
索引.php
<?php
$item->name = "Sample Name";
$item->create();
?>
错误:
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in PATH on line 62