我正在设计此页面,用户将在其中看到当天的变更单。我只希望一个人一次编辑一个变更单。而其他用户仍可以编辑其他变更单。我要粘贴的代码用于最终页面,在单击特定变更单后,用户将被重定向。请举一个例子帮助我,因为我对php和mysql非常陌生。提前致谢。 `
<?php
$change = $_GET['value'];
mysqli_query('BEGIN');
$sql="SELECT * FROM cat_firewall USE INDEX(id) WHERE chg_id='$change' FOR UPDATE";
$result=mysqli_query($con,$sql);
// Count table rows
$count=mysqli_num_rows($result);
?>
---MORE CODE----
while ($row = mysqli_fetch_array($result)){ ?>
<tr>
---MORE CODE----
<?php
$source_FW = $_POST['source_FW'];
$source_ACL = $_POST['source_ACL'];
$destination_FW = $_POST['destination_FW'];
$destination_ACL = $_POST['destination_ACL'];
if(isset($_POST['Submit'])){
foreach($source_FW as $key=>$value){
$sql1="UPDATE cat_firewall SET source_FW='$value' WHERE entry_id='$key' AND chg_id='$change' ";
$result=mysqli_query($con,$sql1);
}
foreach($source_ACL as $key1=>$value1){
$sql2="UPDATE cat_firewall SET source_ACL='$value1' WHERE entry_id='$key1' AND chg_id='$change' ";
mysqli_query($con,$sql2);
}
foreach($destination_FW as $key2=>$value2){
$sql3="UPDATE cat_firewall SET destination_FW='$value2' WHERE entry_id='$key2' AND chg_id='$change' ";
mysqli_query($con,$sql3);
}
foreach($destination_ACL as $key3=>$value3){
$sql4="UPDATE cat_firewall SET destination_ACL='$value3' WHERE entry_id='$key3' AND chg_id='$change' ";
mysqli_query($con,$sql4);
}
echo "update complete";
}
sleep(120);
mysqli_query($con,'COMMIT');
?>'
`
答案 0 :(得分:0)
我想在用户单击变更单后立即锁定它,并转到此页面以编辑行,并在单击“提交”按钮后释放该行的锁定。
那是一个很大的危险信号。您不应该期望InnoDB锁定能够像人类一样快速运行。
乐观版本:
以下内容:
BEGIN;
SELECT seq_num ... FOR UPDATE;
if the seq_num has changed, ROLLBACK this transaction and tell the user to start over.
else...
do the UPDATEs
update seq_num to a new number
COMMIT;
悲观方法...
BEGIN;
SELECT who_is_editing FROM ... FOR UPDATE;
if someone else is editing, ROLLBACK and tell user to "try again later".
else...
UPDATE ... who_is_editing = $this_user;
COMMIT;
现在继续让用户做他想做的事。但是,最后清除who_is_editing
。
注意:如果用户只是消失了,则此“锁”可能永远不会释放。所以...
who_is_editing
匹配的UPDATE事务中验证 。如果不给他打耳光,告诉他“你花了太长时间!”。(注意:这两种方法都是仓促输入的;请仔细研究,以寻找逻辑上的空白。)