Hi guys I have Table "Item" that have a "Name" field. I am trying to insert a data but I am getting an error. Please see my below code for further information.
item.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;
}
}
}
index.php
<?php
$item->name = "Sample Name";
$item->create();
?>
Error:
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in PATH on line 62