如何避免使用表单进行多次SQL更新?

时间:2022-03-13 00:07:37

I am trying to update mysql data by letting users submit a form.

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

The user could update the result of the game and the winning team get +1 win in mysql. The user can always see the game form and update/change the game.

用户可以更新游戏结果,获胜的团队在mysql中获得+1的胜利。用户总是可以看到游戏表单并更新/更改游戏。

My question is how to prevent second or third time +1 win to the existing winning team when the users update the form again.

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

ex: Team A beats Team B and the user submits the form. The mysql will +1 to win column for Team A. Later, the users find out Team C beats Team D and submit the form again, but Team A, Team B, Team C and Team D are all in the same form, so if the user submits the form again, Team A will get 2 wins in win column and Team C will get 1 win. I want to keep Team A with 1 win only. Not sure if it's possible. Thanks for any reply.

A队打败B队,用户提交表格。mysql会+ 1 A为团队赢得列后,用户发现团队C比团队D和再次提交表单,但是团队,团队B,D团队C和团队都在同一形式,如果用户提交表单,团队将获得2胜赢得列和团队C将得到1赢。我想让A队只赢1场。不确定是否可能。谢谢你的任何回复。

Form Html

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>

My result php

我的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'
}

I hope I explain my questions clearly.. thanks for any reply.

我希望能把我的问题解释清楚。谢谢你的任何回复。

1 个解决方案

#1


4  

Your update query only needs to be:

您的更新查询只需:

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

There's no need to make the trip to PHP and back to the database; it can be done in a single trip.

没有必要去访问PHP和返回到数据库;这可以在一次旅行中完成。

How to prevent duplicate submissions? Only display the games that have not had a winner specified. Additionally, check in the update statement that the winner hasn't already been specified.

如何防止重复提交?只显示没有指定获胜者的游戏。此外,请检查更新语句中尚未指定获胜者。

#1


4  

Your update query only needs to be:

您的更新查询只需:

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

There's no need to make the trip to PHP and back to the database; it can be done in a single trip.

没有必要去访问PHP和返回到数据库;这可以在一次旅行中完成。

How to prevent duplicate submissions? Only display the games that have not had a winner specified. Additionally, check in the update statement that the winner hasn't already been specified.

如何防止重复提交?只显示没有指定获胜者的游戏。此外,请检查更新语句中尚未指定获胜者。