Page 1 of 1

Need help with looping

Posted: Sat Jan 07, 2006 11:56 pm
by Wobbly
Hey guys,

I'm working on a form to keep track of player stats. The problem I'm having is once the form is submitted; only the "lowest" number "user_id" stats are updated. I've been chasing my tail on this thing for two solid days. Any help would be greatly appreciated.

Thanks in advance,
Nick

Code: Select all

<?php
if ($_POST['action'] == "update") {

          $user_id = $_POST['user_id'];
          $total_wins = $_POST['total_wins'];
          $total_losses = $_POST['total_losses'];
          $total_score = $_POST['total_score'];      	
 	  
          $sql = mysql_query("update phpbb_pool set total_wins='$total_wins', total_losses='$total_losses', total_score='$total_score' where user_id = $user_id");
          echo ("$sql");
}
?>

<form action="<?php echo "{$_SERVER['PHP_SELF']}"; ?>" method="post">
<input type="hidden" name="action" value="update">
<table name='scores' width='50%' cellpadding='3' cellspacing='1' border='0' class='forumline' align='center'>
<tr> 
<th height='25' class='thCornerL' nowrap>Username</th>
<th class='thTop' nowrap>Wins</th>
<th class='thTop' nowrap>Losses</th>
<th class='thCornerR' nowrap>Total Score</th>
</tr>

<?php
$query = mysql_query("select phpbb_pool.user_id, phpbb_users.user_id, username, total_wins, total_losses, total_score from phpbb_pool, phpbb_users where (phpbb_pool.user_id=phpbb_users.user_id) order by username asc");

while ($row = mysql_fetch_array($query)) {

$user_id = $row["user_id"];
$username = $row["username"];
$total_wins = $row["total_wins"];
$total_losses = $row["total_losses"];
$total_score = $row["total_score"];

echo"<input type='hidden' name='user_id' value='$user_id'>";

echo"<tr>";
echo"<td class='row1' align='center'><span class='gen'>$username</span></td>";
echo"<td class='row1' align='center'><span class='gen'><input type='text' name='total_wins' size='3' value='$total_wins'></span></td>";
echo"<td class='row1' align='center'><span class='gen'><input type='text' name='total_losses' size='3' value='$total_losses'></span></td>";
echo"<td class='row1' align='center'><span class='gen'><input type='text' name='total_score' size='3' value='$total_score'></span></td>";
echo"</tr>";

}
?>

<tr><td class='row1' colspan='4' align='center'><br><input type="submit" name="update" class="liteoption" value="Update"></td></tr>
</table>
</form>
The following is a copy of the results. I have no idea where the "1" is coming from?

Code: Select all

user_id=3
total_wins=20
total_losses=5
total_score=15
1

Posted: Sun Jan 08, 2006 7:14 am
by shoebappa
view source the output and post the HTML...

Re: Need help with looping

Posted: Sun Jan 08, 2006 9:44 am
by trukfixer
Wobbly wrote:Hey guys,

Code: Select all

<?php
if ($_POST['action'] == "update") {

          $user_id = $_POST['user_id'];
          $total_wins = $_POST['total_wins'];
          $total_losses = $_POST['total_losses'];
          $total_score = $_POST['total_score'];      	
 	  
          $sql = mysql_query("update phpbb_pool set total_wins='$total_wins', total_losses='$total_losses', total_score='$total_score' where user_id = $user_id");
          echo ("$sql");
}
?>

<form action="<?php echo "{$_SERVER['PHP_SELF']}"; ?>" method="post">
<input type="hidden" name="action" value="update">
<table name='scores' width='50%' cellpadding='3' cellspacing='1' border='0' class='forumline' align='center'>
<tr> 
<th height='25' class='thCornerL' nowrap>Username</th>
<th class='thTop' nowrap>Wins</th>
<th class='thTop' nowrap>Losses</th>
<th class='thCornerR' nowrap>Total Score</th>
</tr>

<?php
$query = mysql_query("select phpbb_pool.user_id, phpbb_users.user_id, username, total_wins, total_losses, total_score from phpbb_pool, phpbb_users where (phpbb_pool.user_id=phpbb_users.user_id) order by username asc");

while ($row = mysql_fetch_array($query)) {

$user_id = $row["user_id"];
$username = $row["username"];
$total_wins = $row["total_wins"];
$total_losses = $row["total_losses"];
$total_score = $row["total_score"];

echo"<input type='hidden' name='user_id' value='$user_id'>";

echo"<tr>";
echo"<td class='row1' align='center'><span class='gen'>$username</span></td>";
echo"<td class='row1' align='center'><span class='gen'><input type='text' name='total_wins' size='3' value='$total_wins'></span></td>";
echo"<td class='row1' align='center'><span class='gen'><input type='text' name='total_losses' size='3' value='$total_losses'></span></td>";
echo"<td class='row1' align='center'><span class='gen'><input type='text' name='total_score' size='3' value='$total_score'></span></td>";
echo"</tr>";

}
?>

<tr><td class='row1' colspan='4' align='center'><br><input type="submit" name="update" class="liteoption" value="Update"></td></tr>
</table>
</form>
The following is a copy of the results. I have no idea where the "1" is coming from?

Code: Select all

user_id=3
total_wins=20
total_losses=5
total_score=15
1
the 1 comes from your echo of $sql - which is the return value of mysql_query (true) which only shows that the query succeeded.
what it appears to me, is you are posting to the query user data from a looped form ..

here is the problem : you are putting *EVERYTHING* into one form , so whenever you submit the form, it's gonna grab whatever values are *FIRST SET* , which means, the user_id is set to 1 in $_POST when you submit, and the other values , take teh values you entered in teh first text box because those are the first values that are not null..

If you want to be able to update a *SINGLE* user from that form, you need to make a single form for each user , so you have to put the <form> and </form> into the loop, so every iteration of a user will create a unique form and submit button

Otherwise, in order to update *multiple* users at one time, you need to set your form names to be arrays , and when posting, you have to iterate through the arrays to update the users whose stats have been set

So...

to update a single user, make your page build like this:

Code: Select all

/* ~~SNIP ~~*/

<table name='scores' width='50%' cellpadding='3' cellspacing='1' border='0' class='forumline' align='center'>
<tr> 
<th height='25' class='thCornerL' nowrap>Username</th>
<th class='thTop' nowrap>Wins</th>
<th class='thTop' nowrap>Losses</th>
<th class='thCornerR' nowrap>Total Score</th>
</tr>

<?php
$query = mysql_query("select phpbb_pool.user_id, phpbb_users.user_id, username, total_wins, total_losses, total_score from phpbb_pool, phpbb_users where (phpbb_pool.user_id=phpbb_users.user_id) order by username asc");

while ($row = mysql_fetch_array($query)) {
echo "<form action='".$_SERVER['PHP_SELF']."' method='post'>
<input type='hidden' name='action' value='update'>";

$user_id = $row["user_id"];
$username = $row["username"];
$total_wins = $row["total_wins"];
$total_losses = $row["total_losses"];
$total_score = $row["total_score"];

echo"<input type='hidden' name='user_id' value='$user_id'>";

echo"<tr>";
echo"<td class='row1' align='center'><span class='gen'>$username</span></td>";
echo"<td class='row1' align='center'><span class='gen'><input type='text' name='total_wins' size='3' value='$total_wins'></span></td>";
echo"<td class='row1' align='center'><span class='gen'><input type='text' name='total_losses' size='3' value='$total_losses'></span></td>";
echo"<td class='row1' align='center'><span class='gen'><input type='text' name='total_score' size='3' value='$total_score'></span></td>";
echo"</tr>";
echo "<tr><td class='row1' colspan='4' align='center'><br><input type='submit' name='update' class='liteoption' value='Update'></td></tr></form>";
}
?>
</table>
HTH
Bri!

Need help with looping [SOLVED]

Posted: Sun Jan 08, 2006 12:49 pm
by Wobbly
Perfect...nice work trukfixer! :D

I had originally planned to update multiple players, but your method is much cleaner. I just moved the update button to the end of each row and it works great!

Thanks again,
Nick