当我将新记录插入一个表(work_log
)时,我使用employers
中最后插入的记录更新另一个表(work_log
)中的记录。
但是如果更新employers
- 表不成功,在将新记录成功插入work_log
后,我需要将新添加的记录删除到work_log
,因为该条目将不再有效
到目前为止,这是我的脚本:
/**
* This first part has no direct affect on the question, but serves as additional information to understand the script better..
* - - -
* First, a new work session is inserted (this session has nothing to do with browser session)
* If this fails, the script does not continue, and the user is redirected back to the form with an error-message.
* otherwise, the script continues, and try to activate the session by adding a new work_log entry.
*/
$ins_session = $con['site']->prepare('INSERT INTO work_sessions (fields) VALUES (?)');
$ins_session->execute(array(values));
if($ins_session){
KD::notice('success','New work session is created.');
$session_id = $con['site']->lastInsertId();
} else {
KD::notice('error','Work session was not created.');
KD::redirect(); // stops the script, and redirects
}
/**
* This part affects my question
* - - -
* Add a new entry to the work log in order to automatically start the work session.
* If this entry is successfully inserted, then add an indicator to the corresponding employer, in the employers table, to indicate that this employer has an active session (and which one it is).
*/
$ins_work_log = $con['site']->prepare('INSERT INTO work_log (fields) VALUES (?)');
$ins_work_log->execute(array(values));
if($ins_work_log){
$upd_employer = $con['site']->prepare('UPDATE employers SET fk_work_sessions_id = ? WHERE id = ?');
$upd_employer->execute(array($session_id,$_POST['employer_id']));
if($upd_employer){
KD::notice('success','New session was created and started.');
KD::redirect();
} else {
// need to remove the entry from work_log.
KD::notice('Work session was created, but not started. Please start the session manually.');
}
}
根据我的理解,我必须删除work_log
- 表中最后插入的记录?
有没有其他方法可以做到这一点?比如,在另一个顺序中,或者如果这个(更新查询)失败,则自动从work_log
中删除该条目?
work_log
- 表格为innoDB
,行格式为compact
,如果知道这一点很重要...
更新
我把它设置成这样:
它似乎工作,但我有点不确定我是否正确使用if / else语句。
$con['site']->beginTransaction();
$ins_work_log = $con['site']->prepare('INSERT INTO work_log (fields) VALUES (?)');
$ins_work_log->execute(array(values));
if($ins_work_log){
# update employer
$upd_employer = $con['site']->prepare('UPDATE employers SET fk_work_sessions_id = ? WHERE id = ?');
$upd_employer->execute(array($session_id,$_POST['employer_id']));
if($upd_employer){
$con['site']->commit();
KD::notice('success','New session was created and started.');
} else {
$con['site']->rollBack();
KD::notice('error','Work session was created, but not started. Please start the session manually.');
}
//
} else {
$con['site']->rollBack();
KD::notice('error','');
}
KD::redirect();
如果尚未提交查询,if($ins_work_log)
和if($upd_employer)
会有什么影响吗?
答案 0 :(得分:2)
这是使用START TRANSACTION
,COMMIT
和ROLLBACK
的典型案例。
http://dev.mysql.com/doc/refman/5.0/en/commit.html
请确保您使用的是支持它的数据库引擎。
伪代码:
query("START TRANSACTION;");
query("INSERT INTO table1 ...");
if (query("INSERT INTO table2 ..."))
query("COMMIT;");
else
query("ROLLBACK;");