PDO php 一个函数中的两个语句,如果都为真,则提交,否则回滚
PDO php two statements in one function, if both are true commit else rollback
使用PHP PDO,有两个语句,想法是如果两个stmt1
和 stmt2 是真的,它应该提交,否则它应该回滚,但正如我在这里看到的,它没有回滚,如果 stmt1 是真的,它会评论,即使 stmt2 是假的。
这是函数:
public function insert() {
// try { $stmt1 = $this->conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$this->conn->beginTransaction();
$stmt1 = $this->conn->prepare("INSERT into 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 into 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 will save changes
} else {
$this->conn->rollBack(); //This will undo changes
}
}
}
我在这里检查了这个函数,如果 stmt1 为真,它将 运行 并将数据插入 table 中,即使 stmt2 为假
问题:我如何保持它应该的状态 运行 stmt1,然后是 stmt2,如果 stmt1 为假,它不应该 运行 stmt2,如果 stmt2 为假,它也应该回滚 stmt1。
提前致谢。
试试这个:
public function insert() {
// try { $stmt1 = $this->conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$this->conn->beginTransaction();
$stmt1 = $this->conn->prepare("INSERT into 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 into 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 will save changes
} else {
$this->conn->rollBack(); //This will undo changes
}
}
}
使用PHP PDO,有两个语句,想法是如果两个stmt1 和 stmt2 是真的,它应该提交,否则它应该回滚,但正如我在这里看到的,它没有回滚,如果 stmt1 是真的,它会评论,即使 stmt2 是假的。
这是函数:
public function insert() {
// try { $stmt1 = $this->conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$this->conn->beginTransaction();
$stmt1 = $this->conn->prepare("INSERT into 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 into 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 will save changes
} else {
$this->conn->rollBack(); //This will undo changes
}
}
}
我在这里检查了这个函数,如果 stmt1 为真,它将 运行 并将数据插入 table 中,即使 stmt2 为假
问题:我如何保持它应该的状态 运行 stmt1,然后是 stmt2,如果 stmt1 为假,它不应该 运行 stmt2,如果 stmt2 为假,它也应该回滚 stmt1。
提前致谢。
试试这个:
public function insert() {
// try { $stmt1 = $this->conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$this->conn->beginTransaction();
$stmt1 = $this->conn->prepare("INSERT into 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 into 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 will save changes
} else {
$this->conn->rollBack(); //This will undo changes
}
}
}