编程自己很新,所以请原谅愚蠢的错误..我有一个拒绝工作的PDF MySQL声明。我可以通过Ajax / POST成功传递数据,但它不适用于数据库。
我已根据以下指南PDO Class准备了一个“数据库”类,并将其扩展到包含以下功能的“电子邮件”类:
public function updateEmailTemplate($emailTemplateDescription,
$emailTemplateSubject, $emailTemplateBody,
$emailTemplateType, $id)
{
$this->query('UPDATE email_templates
SET emailTemplateTitle = :title,
emailTemplateSubject = :subject,
emailTemplateBody = :body,
emailTemplateType = :type
WHERE emailTemplateID= :id');
$this->bind(':title', $emailTemplateDescription);
$this->bind(':subject', $emailTemplateSubject);
$this->bind(':body', $emailTemplateBody);
$this->bind(':type', $emailTemplateType);
$this->bind(':id', $id);
$this->execute();
if ($this->lastInsertId() == true) {
echo 'Template added successfully';
} else {
echo 'There was an error';
}
}
当它运行但我只是得到“有一个错误”并且数据库中没有更新任何想法?
class Database{
private $host = "localhost";
private $user = "root";
private $pass = "MyP@ymentPortal";
private $dbname = "mppcompany";
private $dbh;
private $error;
private $stmt;
public function __construct(){
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instanace
try{
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
// Catch any errors
catch(PDOException $e){
$this->error = $e->getMessage();
echo 'ERROR: ' . $e->getMessage();
}
}
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
public function rowCount(){
return $this->stmt->rowCount();
}
public function lastInsertId(){
return $this->dbh->lastInsertId();
}
public function beginTransaction(){
return $this->dbh->beginTransaction();
}
public function endTransaction(){
return $this->dbh->commit();
}
public function cancelTransaction(){
return $this->dbh->rollBack();
}
public function debugDumpParams(){
return $this->stmt->debugDumpParams();
}
}
class email extends Database {
public function emailTemplates() {
$this->query('SELECT * FROM email_templates');
$rows = $this->resultset();
return $rows;
}
public function selectOneEmail($id) {
$this->query('SELECT * FROM email_templates WHERE emailTemplateID=:id');
$this->bind(':id', $id);
$this->execute();
$row = $this->single();
return $row;
}
public function createEmailTemplate($emailTemplateDescription, $emailTemplateSubject, $emailTemplateBody) {
$this-> query('INSERT INTO email_templates (emailTemplateTitle, emailTemplateSubject, emailTemplateBody, emailTemplateType) VALUES (:title, :subject, :body, 1)');
$this-> bind(':title', $emailTemplateDescription);
$this-> bind(':subject', $emailTemplateSubject);
$this-> bind(':body', $emailTemplateBody);
$this-> execute();
if ($this-> lastInsertId() == true) {
echo 'Template added successfully';
} else {
echo 'There was an error';
}
}
public function updateEmailTemplate($emailTemplateDescription, $emailTemplateSubject, $emailTemplateBody, $emailTemplateType, $id) {
$this->query('UPDATE email_templates SET emailTemplateTitle = :title, emailTemplateSubject = :subject, emailTemplateBody = :body, emailTemplateType = :type WHERE emailTemplateID= :id');
$this-> bind(':title', $emailTemplateDescription);
$this-> bind(':subject', $emailTemplateSubject);
$this-> bind(':body', $emailTemplateBody);
$this->bind(':type', $emailTemplateType);
$this-> bind(':id', $id);
$this-> execute();
}
public function deleteEmailTemplate($id)
{
$this->query('DELETE FROM email_templates WHERE emailTemplateID=:id');
$this->bind(':id', $id);
$this->execute();
if ($this->execute() > 0)
{echo 'Template Deleted';
}else{
echo 'Error Deleting Template';
}}
}
答案 0 :(得分:1)
罪魁祸首是lastInsertId()
- 顾名思义,它会从最后一个插入返回auto_increment - 但你不会做任何插入。
因此,您需要使用不同的方法检查是否成功,也许仅使用异常即可。