PHP PDO插入,更新成在平均时间插入查询到其他表的一个表太
这是表PHP PDO插入,更新成在平均时间插入查询到其他表的一个表太
tbl_one
id---int
name-var
tbl_tow
id----int
name--var
这是它如何与PHP PDO插入,
public function insert() {
$stmt9 = $this->conn->prepare("INSERT into tbl_one (name) VALUES (:name)");
$stmt9->bindParam(':name' ,$this->name);
$stmt9->execute();
if($stmt9){
echo "Added";
} else {
echo "error";
}
}
这里idia一次插入两个表中,并且一次从两个表中删除。
,才有可能在这里
注:我不能使用触发器,因为我有在其他情况下,已经设置因此MySQL不会在同一时间或行动支持多点触发。
关于
我假定您正在寻找一笔交易。
查询执行的所有更改只会在您提交事务时才会生效,如果某些操作失败,您可能会简单地执行回滚操作而根本没有任何操作。
如果您在多个表中插入并且希望确保只有在所有查询都成功时才进行更改,这非常有用!
实施例:
public function insert() {
//This will start a transaction and turn-off auto-commit of queries.
$this->conn->beginTransaction();
$stmt9 = $this->conn->prepare("INSERT into tbl_one (name) VALUES (:name)");
$stmt9->bindParam(':name' ,$this->name);
$stmt9->execute();
$stmt10 = $this->conn->prepare("INSERT into tbl_two (name) VALUES (:name)");
$stmt10->bindParam(':name' , $someOtherName);
$stmt10->execute();
if($stmt9 && $stmt10){
$this->conn->commit(); //This will save your changes
} else {
$this->conn->rollBack(); //This will undo your changes
}
}
这么简单。
它是$ dbh-> beginTransaction();或$ stmt9-> beginTransaction(); – SAR 2014-09-10 10:33:44
@up:更新了我的帖子。那么你需要使用你的“PDO”对象,但是它的调用我不知道,因为你在这里使用语句对象。我没有那么多的PDO扩展这是因为我不能确定,但我认为它存储为$ this-> conn在你的情况。请参阅:http://php.net/manual/de/pdo.begintransaction.php – Steini 2014-09-10 10:36:07
是的,这是$ this-> conn – SAR 2014-09-10 10:37:57
这是不是你所追求的?
public function insert() {
$stmt9 = $this->conn->prepare("INSERT into tbl_one (name) VALUES (:name)");
$stmt9->bindParam(':name' ,$this->name);
$stmt9->execute();
$stmt10 = $this->conn->prepare("INSERT into tbl_one (name) VALUES (:name)");
$stmt10->bindParam(':name' ,$this->name);
$stmt10->execute();
if($stmt9 && $stmt10){
echo "Added";
} else {
echo "error";
}
}
基本上你加倍查询你的函数里面,这样你就可以“手动”插入小径日志表。对不起,如果我误解了你。
你可以只是这样做:
public function insert() {
$stmt9 = $this->conn->prepare("INSERT into tbl_one (name) VALUES (:name); INSERT into tbl_two (name) VALUES (:name)");
$stmt9->bindParam(':name' ,$this->name);
$stmt9->execute();
if($stmt9){
echo "Added";
} else {
echo "error";
}
}
请将代码部分缩进4格来格式化您的代码。同时要注意,在单个字符串中维护多个查询是非常肮脏和困难的。还要考虑交易是完全针对所述类型的问题。 – Steini 2014-09-10 11:09:19
不一定是因为它会减少代码中的语句数量 – 2014-09-10 11:36:51
是的,它是一种历史的actully的IDIA是tbl_one会编辑和更新,但tbl_tow只有插入意味着每个动作或交易将被插入,因此将保持历史的任何交易。 – SAR 2014-09-10 10:28:33
如果您添加其他语句以插入其他表格,会出现什么问题! – 2014-09-10 10:29:57