I am trying to update multiple records in one form. I have read the article here (Variable variables, PHP, and You) and thought that it would give me what I wanted. The code below gives the recordset that I want to update, but when I press the Submit Query button , the records are not updated, Can anyone look at this code and offer me a suggestion to fix it?
<?php require_once('Connections/stats.php'); ?>
<?php
$col_users = "1";
if (isset($HTTP_GET_VARS[col])) {
$col_users = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS[col] : addslashes($HTTP_GET_VARS[col]);
}
mysql_select_db($database_stats, $stats);
$query_users = sprintf("SELECT users_tbl.user_id_num, users_tbl.school_id_num, users_tbl.username, users_tbl.access, users_tbl.sport_id_num FROM users_tbl WHERE users_tbl.school_id_num=%s", $col_users);
$users = mysql_query($query_users, $stats) or die(mysql_error());
$row_users = mysql_fetch_assoc($users);
$totalRows_users = mysql_num_rows($users);
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
//Initialize counter variables
$index = 0;
$index_count = 0;
echo "<form method=post action=$PHP_SELF>\n";
echo "<table>\n";
echo "<tr><td><b>Username</b></td><td><b>School</b></td>".
"<td><b>Sport</b></td><td><b>Access</b></td></tr>\n";
/*
Assuming we already have retrieved the records from the database into an array setting
$row_users = mysql_fetch_array(). The do...while loop assigns a value to the $xstr variable
by taking the name and concatenating the value of $index to the end starting with 0. So
the first time through the loop $user_id_numStr would have a value of user_id_num0 the
next time through it would be user_id_num1 and so forth.
*/
do {
$user_id_numStr = user_id_num.$index;
$usernameStr = username.$index;
$school_id_numStr = school_id_num.$index;
$sport_id_numStr = sport_id_num.$index;
$accessStr = access.$index;
//This section would print the values onto the screen one record per row
printf("<tr><td><input type=hidden name=%s value=%s><input type=text name=%s value=%s></td>
<td><input type=text name=%s value=%s></td><td><input type=text name=%s value=%s></td>
<td><input type=text name=%s value=%s></td></tr>\n",
$user_id_numStr, $row_users["user_id_num"], $usernameStr, $row_users["username"], $school_id_numStr, $row_users["school_id_num"],
$sport_id_numStr, $row_users["sport_id_num"], $accessStr, $row_users["access"]);
//Increase counter values by 1 for each loop
$index++;
$index_count++;
} while ($row_users = mysql_fetch_array($users));
// I also had to create an index count to keep track of the total number of rows.
echo "<INPUT TYPE=hidden NAME=counter VALUE=$index_count>\n";
echo "<INPUT TYPE=submit></form>\n";
?>
<?php
echo "</table>";
?>
<?php
//This loops through all the records that have been displayed on the page.
for ($index = 0; $index <= $counter; $index++) {
/*
This part sets a variable with the names we created in the first section.
We start with 0 and go until the number saved in the $index_count variable.
*/
$varuser_id_num = 'user_id_num'.$index;
$varusername = 'username'.$index;
$varschool_id_num = 'school_id_num'.$index;
$varsport_id_num = 'sport_id_num'.$index;
$varaccess = 'access'.$index;
/*
This is the variable variable section. We take the value that was assigned
to each name variable. For example the first time through the loop we are
at the record assigned with SubmissionID0. The value given to SubmissionID0
is set from the first section. We access this value by taking the variable
variable of what SubmissionID0 is.
*/
$user_id_numvalue = $$varuser_id_num;
$usernamevalue = $$varusername;
$school_id_numvalue = $$varschool_id_num;
$sport_id_numvalue = $$varsport_id_num;
$accessvalue = $$varaccess;
//Update the database
$sql = "UPDATE users_tbl SET username='$usernamevalue',school_id_num='$school_id_numvalue',".
"sport_id_num='$sport_id_numvalue',access='$accessvalue' WHERE user_id_num=$user_id_numvalue'";
$result = mysql_query($sql);
}
?>
</body>
</html>
<?php
mysql_free_result($users);
?>