Jump to content

updating multiple rows with one form


sjohndro

Recommended Posts

I'm updating a ranking page and I want to do all 10 rows at once. I have the form showing the info from the table to edit . I go to submit the form and nothing. Can someone pls look and see what I'm doing wrong. I have read so many forums my head is spinning, if you can help or direct me to a posting that is a little more clear then the ones I have read. Here is my code:

<?php require_once('Connections/wss2.php'); ?><?phpif (!function_exists("GetSQLValueString")) {function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") {  if (PHP_VERSION < 6) {	$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;  }  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);  switch ($theType) {	case "text":	  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";	  break;		case "long":	case "int":	  $theValue = ($theValue != "") ? intval($theValue) : "NULL";	  break;	case "double":	  $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";	  break;	case "date":	  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";	  break;	case "defined":	  $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;	  break;  }  return $theValue;}}$editFormAction = $_SERVER['PHP_SELF'];if (isset($_SERVER['QUERY_STRING'])) {  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);}if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {  $updateSQL = sprintf("UPDATE ranking SET name='$name[$i]', points='$points[$i]', WHERE id='$id[$i]'",						GetSQLValueString($_POST['name'], "text"),					   GetSQLValueString($_POST['points'], "int"),					   GetSQLValueString($_POST['id'], "text"));  mysql_select_db($database_wss, $wss);  $Result1 = mysql_query($updateSQL, $wss) or die(mysql_error());  $updateGoTo = "homedynamic.php";  if (isset($_SERVER['QUERY_STRING'])) {	$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";	$updateGoTo .= $_SERVER['QUERY_STRING'];  }  header(sprintf("Location: %s", $updateGoTo));}mysql_select_db($database_wss, $wss);$query_rankup = "SELECT * FROM ranking";$rankup = mysql_query($query_rankup, $wss) or die(mysql_error());$count=mysql_num_rows($rankup);?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Untitled Document</title></head><body><form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">  <?phpwhile($rows=mysql_fetch_array($rankup)){?>  <table align="center">	<tr valign="baseline">	<td nowrap="nowrap" align="right">Id:</td>	  <td width="5"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td>	<td width="4">Name:</td>	  <td><input type="text" name="name" value="<?php echo htmlentities($rows['name'], ENT_COMPAT, 'UTF-8'); ?>" size="25" /></td>	<td nowrap="nowrap" align="right">Points:</td>	<td><input type="text" name="points" value="<?php echo htmlentities($rows['points'], ENT_COMPAT, 'UTF-8'); ?>" size="6" />	</td> <?php}?>   	</tr>	<tr valign="baseline">	  <td><input type="submit" value="Update record" /></td>	</tr>  </table>  <input type="hidden" name="MM_update" value="form1" />  <input type="hidden" name="id" value="<?php echo $rows_rankup['id']; ?>" /></form><p> </p></body></html><?phpmysql_free_result($rankup);?>

Link to comment
Share on other sites

One problem is that all of your form elements have the same name, so you'll only get the value of the last ones. If you use brackets with the name, you can create an array of values in PHP:<input type="text" name="name[]" ...Another problem is that you're not including the ID with each row, so it wouldn't know what row to update in the database. You print the ID, but don't put it in an input with a name. After the loop you have a hidden element called id, but you're writing a non-existent value to it.Another problem is that you're not using sprintf correctly. The string should have formatting characters and then you pass values to fill in for each position, but your string just includes (undefined) variables, so there aren't going to be any formatting characters to replace. Check the documentation for sprintf to see how it's used:http://www.php.net/manual/en/function.sprintf.phpLastly, you're not looping over the data to update. You select the data and loop over it to print the form, but the code to process the form only tries to run a single query, not one update query per row.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...