This one is driving me mad... I've got an UNDO button following an INSERT request (which goes ahead and DELETEs the last insert ID).
It's not playing at the moment and throwing this error message.
db.class.php
<?php
/////////////////////////////////////////////
// EXTEND THE PDO CLASS WITH CONNX DETAILS //
/////////////////////////////////////////////
class Database extends PDO {
private $engine;
private $host;
private $database;
private $user;
private $pass;
private $dbh;
public function __construct(){
$this->engine = 'sqlite';
$this->host = '';
$this->database = 'database.sqlite';
$this->user = '';
$this->pass = '';
// Construct the SQLite database PDO connection
$this->dbh = parent::__construct($this->engine . ":" . $this->database);
// Turn ON exception mode
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// LOCK timeout
$this->setAttribute(PDO::ATTR_TIMEOUT, 0);
}
public function __destruct() {
$this->dbh = null;
}
}
?>
my.model.php
<?php
class Interaction_Model extends Database {
private $sql;
private $sth;
private $dbh;
///
// CONSTRUCT THE PARENT CLASS
///
public function __construct() {
$this->dbh = parent::__construct();
}
///
// DESTRUCT
///
public function __destruct() {
$this->dbh = null;
}
///
// SAVE ENTRY
///
public function save_entry($fish, $swim, $in, $water) {
$this->sql = "
INSERT INTO table(fish, swim, in, water)
VALUES( :fish, :swim, :in, :water )
";
try {
$this->beginTransaction();
$this->sth = $this->prepare($this->sql);
$this->sth->execute(array( "fish" => $fish
, "swim" => $swim
, "in" => $in
, "water" => $water ));
// Retrieve the last insert ID
$last_insert_id = $this->lastInsertId();
if (isset($last_insert_id)) {
$this->commit();
return $last_insert_id;
} else {
$this->rollback();
return false;
}
} catch (Exception $error) {
$this->rollback();
$this->handle_errors("my.model.php", "public function save_entry()", $error);
return false;
}
}
///
// ERROR HANDLING
///
private function handle_errors($title, $message, $error) {
require_once("error.class.php");
$system = "fish";
Error::logToFile($system, $title, $message, $error->getMessage());
}
}
///
// DELETE AN ENTRY
///
public function delete_entry($id) {
$this->sql = "
DELETE
FROM table
WHERE id = :id
";
try {
$this->beginTransaction();
$this->sth = $this->prepare($this->sql);
$this->sth->execute(array("id"=>$id));
$this->commit();
return true;
} catch (Exception $error) {
$this->handle_errors("my.model.php", "public function delete_entry($id)", $error);
$this->rollBack();
return false;
}
}
?>
My scripts are handlers for inserting and deleting are separate PHP files and are called via a jQuery $.post request. Each script includes the db.class and model independently.
I can INSERT the values with no problem - DELETING them on the UNDO though gives me the Locked error - I'm not sure where I am going wrong.
Thanks
Aucun commentaire:
Enregistrer un commentaire