Update multiple rows of a table

Hi there is it possible to update multiple rows of a table using a while loop inside a hidden field???

[code]<?php if ((isset($_POST[“MM_update”])) && ($_POST[“MM_update”] == “form1”)) {
$updateSQL = sprintf(“UPDATE ships SET HealthA=%s WHERE ShipID=%s”,
GetSQLValueString($_POST[‘healtha’], “int”),
GetSQLValueString($_POST[‘shipid’], “text”)); ?>

[/code]

If not how can i do this???

Thank You

Only the first record ever updates :frowning:

Having php loop within HTML form hidden field make no sense. After this page loaded in your browser, view the HTML source of this page, and you will see your php code produced weird value for your shipid form field. If you want to submit array of ID’s, you need to produce array of form fields:
[php]<?php
while($row = mysql_fetch_assoc($Ships)){
echo ‘’;
}
?>[/php]

Then in your php code, you need to update each record either in loop, or update all records by one update query using IN (…) sql statement, like this:

UPDATE ships SET HealthA=%s WHERE ShipID IN (1,2,3)

Thank you so much for helping me, like i say im fairly new to php.

After the: echo '<input name="shipid[]" type="hidden" value="'.$row['ShipID'].'" />';

Would i need to do the same for the healtha hiddenfield in the while loop?? Also on the Update what does the (1,2,3) mean?? Is that the fields that are to be updated?

Thanks

Heres what i have so far, but it doesnt appear to be updating even the first record anymore:

<?php 
$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 ships SET HealthA=%s WHERE ShipID IN(ShipID)",                     
                       GetSQLValueString($_POST['healtha'], "text"));

  mysql_select_db($database_swb, $swb);
  $Result1 = mysql_query($updateSQL, $swb) or die(mysql_error());

  $updateGoTo = "index.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}


<form name="form1"action="<?php echo $editFormAction; ?>" method="POST">
<input type="hidden" name="MM_update">
<input name="Submit" type="submit" id="Submit" value="Repair Ships" />
<input name="healtha" type="hidden" id="healtha" value="<?php echo $row_Ships['HealthA']; ?>" />

<?php while ($row = mysql_fetch_assoc($Ships)){ 
	  echo '<input name="shipid[]" type="hidden" value="'.$row_Ships['ShipID'].'"/>';
	  } ?>

</form>

Can you spot whats up?

Thanks :expressionless:

Since shipid is submitted by form as an array, you need to process it as array in your php:
[php]
$str_shipids = ‘’;
if(is_array($_POST[“shipid”])) foreach($_POST[“shipid”] as $value){
$str_shipids.=($str_shipids==’’?’’:’,’).$value;
}[/php]

then, use this in your sql query:
[php]$updateSQL = sprintf(“UPDATE ships SET HealthA=%s WHERE ShipID IN(”.$str_shipids.")", GetSQLValueString($_POST[‘healtha’], “text”));[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service