mysql - PDO php two statements in one function, if both are true commit else rollback -


working php pdo, there 2 statements, idea if both stmt1 , stmt2 true should commit otherwise should roll back, see here it's not getting rollback , here if stmt1 true comment stmt2 false.

this function:

 public function insert() {              //  try { $stmt1 = $this->conn->setattribute( pdo::attr_errmode, pdo::errmode_exception );             $this->conn->begintransaction();             $stmt1 = $this->conn->prepare("insert table1 (item,itemname,price)values (:name, :itemname, :price)");              $stmt1->bindparam(':name' ,                  $this->name);             $stmt1->bindparam(':itemname' ,              $this->itemname);             $stmt1->bindparam(':price' ,                 $this->price);             $stmt1->execute();              $stmt2 = $this->conn->prepare("insert table2 (item,itemname,price) values (:name, :itemname, :price)");              $stmt2->bindparam(':name' ,                  $this->name);             $stmt2->bindparam(':itemname' ,              $this->itemname);             $stmt2->bindparam(':price' ,                 $this->price);             $stmt2->execute();              //} catch(pdoexception $r){ echo $r->__tostring();exit; }             if($stmt1 && $stmt2){                 $this->conn->commit(); //this save  changes             } else {                 $this->conn->rollback(); //this undo  changes             }          }      } 

here checked in function if stmt1 true run , insert data table 1 stmt2 false

question : how can keep should run stmt1 , stmt2 , if stmt1 false should not run stmt2 , if stmt2 false should rollback stmt1 also.

thanks in advance.

try this:

public function insert() {          //  try { $stmt1 = $this->conn->setattribute( pdo::attr_errmode, pdo::errmode_exception );         $this->conn->begintransaction();         $stmt1 = $this->conn->prepare("insert table1 (item,itemname,price)values (:name, :itemname, :price)");          $stmt1->bindparam(':name' ,                  $this->name);         $stmt1->bindparam(':itemname' ,              $this->itemname);         $stmt1->bindparam(':price' ,                 $this->price);         //$stmt1->execute();          $stmt2 = $this->conn->prepare("insert table2 (item,itemname,price) values (:name, :itemname, :price)");          $stmt2->bindparam(':name' ,                  $this->name);         $stmt2->bindparam(':itemname' ,              $this->itemname);         $stmt2->bindparam(':price' ,                 $this->price);         //$stmt2->execute();          //} catch(pdoexception $r){ echo $r->__tostring();exit; }         if($stmt1->execute() && $stmt2->execute()){             $this->conn->commit(); //this save  changes         } else {             $this->conn->rollback(); //this undo  changes         }      }  } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -