开发者

How to avoid multiple SQL updates using a form?

开发者 https://www.devze.com 2023-01-01 10:51 出处:网络
I am trying to update mysql data by letting users submit a form. 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 upda

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

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.

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

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.

Form 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

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.


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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号