【问题标题】:How to avoid multiple SQL updates using a form?如何避免使用表单进行多次 SQL 更新?
【发布时间】:2012-05-02 00:10:11
【问题描述】:

我正在尝试通过让用户提交表单来更新 mysql 数据。

用户可以更新比赛结果,获胜队在mysql中获得+1胜利。用户可以随时查看游戏表格并更新/更改游戏。

我的问题是,当用户再次更新表单时,如何防止现有获胜团队第二次或第三次 +1 获胜。

例如:A 队击败 B 队,用户提交表单。 mysql会为A队+1获胜列。后来用户发现C队击败D队并再次提交表格,但A队,B队,C队和D队都是同一个表格,所以如果用户再次提交表格,A队将在获胜栏中获得2胜,C队将获得1胜。我只想让 A 队只赢 1 场。不确定是否可能。感谢您的回复。

表单HTML

<form>
//First time The user only select the winning team in winner1 and submit the form 
//Second time the user see the page, The Team A will be selected base on 1 win in mysql
//when user tried to submit again,
//Team A will get another 1 win in its record which is not true. 

<select name='winner1'>
<option>Select the winner</option>
<option>Team A</option>
<option>Team B</option>
</select>

//User didn't select the winning team first time
//User submits again with the selected winner team for winner2 . 
//database will be Team A => 2 wins, Team C =>1 win.

<select name='winner2'>
<option>Select the winner</option>
<option>Team C</option>
<option>Team D</option>
</select>
</form>

我的结果 php

for ($i=1; $i<3; $i++){

$winner=$_POST['winner'.$i]

//omit some php query code.......

select win from team where teamName ='$winner'

//get win and +1 to win
$win++

update team set win='$win' where teamName ='$winner'
}

我希望我能清楚地解释我的问题。感谢您的回复。

【问题讨论】:

    标签: php mysql sql sql-update


    【解决方案1】:

    您的更新查询只需:

    UPDATE TEAM 
       SET win = win+1 
     WHERE teamName = '$winner'
     --AND win = 0 
    

    无需访问 PHP 并返回数据库;它可以在一次旅行中完成。

    如何防止重复提交?仅显示未指定获胜者的游戏。此外,请检查尚未指定获胜者的更新声明。

    【讨论】:

    • 感谢您的回复。设置 win=win+1 很高兴知道。对于重复提交,我想让用户能够更新游戏,以防万一他们第一次做错了......无论如何要这样做? +1 虽然。
    • @Jerry:为了更正,在这些情况下,我会使用单独的屏幕和查询。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多