Dallas
Dallas

Reputation: 11

PDO MysQL "Update" statement not saving to DB

Very new to programming myself so excuse silly mistakes .. i have a PDF MySQL statement that refuses to work. I can successfully pass it data via Ajax / POST but it will not apply to the DB.

  1. I have prepared a "Database" class based on the following guidance PDO Class and have extended that to an "Email" class with the following function included:

    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';
        }
    }
    
    1. I can verify that it does receive the variable parameters by "Echoing" the data when the function runs.

When it runs though i simply get "There was an Error" and nothing has been updated in the DB ... Any ideas?

  1. As requested I've added the based Database class code as well

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();
}


}

  1. And here is the full extended class code

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';
}}

	
	
	
	
	
	
	
}

Upvotes: 1

Views: 333

Answers (1)

Eugen Rieck
Eugen Rieck

Reputation: 65264

The culprit is lastInsertId() - as the name suggests, it returns the auto_increment from the last insert - but you don't do any insert.

So you need to check for success with a different method, maybe just using exceptions would suffice.

Upvotes: 1

Related Questions