好的,这是我正在使用的代码
function AddProducts($aTitle, $aDescription, $aPrice, $aQty, $aPhoto)
{
try {
$stmt = $this->pdo->prepare("INSERT INTO products(title, price, description, qty, photo) VALUES(:title, :price, :description, :qty, :photo)");
if(!$stmt){
$err = $this->pdo->errorInfo();
throw new RuntimeException('PRODUCT INSERT FAILED: '.$err[2]);
}
$stmt->bindValue(':title', $this->title, PDO::PARAM_STR);
$stmt->bindValue(':description', $this->description, PDO::PARAM_STR);
$stmt->bindValue(':price', $this->price, PDO::PARAM_INT);
$stmt->bindValue(':qty', $this->qty, PDO::PARAM_INT);
$stmt->bindValue(':photo', $this->photo, PDO::PARAM_STR);
$stmt->execute();
}catch (PDOException $e) {
echo $e->getMessage();
}
}
$addProducts = $database->AddProducts('Ford Mustang', 'This is a Descriptiom', 299.99, 1, 'images/includes/5.jpg');
数据库类和数据库调用函数工作。此外,如果你发现任何迟钝的东西,请指出它,我正在努力学习。
答案 0 :(得分:3)
为什么$this->photo
?你真的在班上有这个属性吗?或许你需要这样的东西?
function AddProducts($aTitle, $aDescription, $aPrice, $aQty, $aPhoto)
{
try {
$stmt = $this->pdo->prepare("INSERT INTO products(title, price, description, qty, photo) VALUES(:title, :price, :description, :qty, :photo)");
if(!$stmt){
$err = $this->pdo->errorInfo();
throw new RuntimeException('PRODUCT INSERT FAILED: '.$err[2]);
}
$stmt->bindValue(':title', $aTitle, PDO::PARAM_STR);
$stmt->bindValue(':description', $aDescription, PDO::PARAM_STR);
$stmt->bindValue(':price', $aPrice, PDO::PARAM_INT);
$stmt->bindValue(':qty', $aQty, PDO::PARAM_INT);
$stmt->bindValue(':photo', $aPhoto, PDO::PARAM_STR);
$stmt->execute();
}catch (PDOException $e) {
echo $e->getMessage();
}
}
$addProducts = $database->AddProducts('Ford Mustang', 'This is a Descriptiom', 299.99, 1, 'images/includes/5.jpg');