PHP - 在填写表单i $后尝试为多个玩家执行SQL代码循环

Trying to do a loop in SQL code for multiple players after filling form i$.

Can't seem to find the answer online. THANK YOU for any help you can provide me.

<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}    
mysql_select_db("mbbcom1_rfhl", $con);

for ($i=1; $i<=6; $i++)
{       
    $sql="UPDATE `mbbcom1_rfhl`.`_statbu`
        SET gp= gp + '$_POST[gp$i]', g= g + '$_POST[g$i]', a= a + '$_POST[a$i]', shot=    shot + '$_POST[s$i]', pm= pm + '$_POST[pm$i]', ppg= ppg + '$_POST[ppg$i]', shg= shg + '$_POST[shg$i]', bs= bs + '$_POST[blk$i]', gwg= gwg + '$_POST[gwg$i]', sog= sog + '$_POST[sog$i]', soa= soa + '$_POST[soa$i]', pim= pim + '$_POST[pim$i]' WHERE `season` =9 AND `_statbu`.`player_id` = $i ORDER BY `_statbu`.`player_id` ASC";
}

if (!mysql_query($sql,$con))
{
    die('Error: ' . mysql_error());
}
mysql_close($con)
?> 

When you add some decent indenting to your code, it becomes clear that you are running the query outside of the for loop so it can only run the last update.

Considering you haven't actually mentioned what the problem is, I'm going to assume that your database does not actually get updated the way you want it too. I think its because in your for loop you dont actually execute your sql statement. If you look more closely, all you're doing is generating the sql string. You need to place the following statement inside your for loop, not outside of it as it is currently.

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

Your update query is wrong, it cannot have a order by. You need to do the execution of the query inside the loop:

for ($i=1; $i<=6; $i++)
{       
    $sql = "UPDATE `mbbcom1_rfhl`.`_statbu`
    SET gp= gp + '$_POST[gp$i]', g= g + '$_POST[g$i]', a= a + '$_POST[a$i]', shot= shot + '$_POST[s$i]', pm= pm + '$_POST[pm$i]', ppg= ppg + '$_POST[ppg$i]', shg= shg + '$_POST[shg$i]', bs= bs + '$_POST[blk$i]', gwg= gwg + '$_POST[gwg$i]', sog= sog + '$_POST[sog$i]', soa= soa + '$_POST[soa$i]', pim= pim + '$_POST[pim$i]' 
    WHERE `season` =9 AND `_statbu`.`player_id` = $i"; 
    // ORDER BY `_statbu`.`player_id` ASC";


  if (!mysql_query($sql,$con))
  {
      die('Error: ' . mysql_error());
  }
}

Futhermore:

  • Your code is wide open to SQL injection
  • You are using the deprecated mysql_* functions