Jump to content

Recommended Posts

Hi is it possible to update multiple records of the same table using a single hidden field with a while loop??? For example:

 

<?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")); ?>

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

 

If its not possible what else could i do to achieve this please?

 

Thank you

Hey fugix thanks for staying with me bruv!

 

Yes im trying to send multiple IDs to the hidden field and an attribute in the table that i want to update.  I have a query before that selects all the relevant records and so i want it to update all of these in one form. Im not even sure cif its possible.. Sorry im fairly new to php.

 

Thanks :)

You need to use your while loop on the entire hidden tag.  Then I assume you just lump all of them into an array and do a foreach on your Update query.

 

<?php 
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
    foreach($shipid AS $id){
  $updateSQL = sprintf("UPDATE ships SET HealthA=%s WHERE ShipID=%s",
                       GetSQLValueString($_POST['healtha'], "int"),
                       GetSQLValueString($_POST['shipid'], "text")); //add your ship id somewhere after this or on this line?
  }
  }
                       ?>

<input name="healtha" type="hidden" id="healtha" value="<?php echo $row_Ships['HealthA'] + 1; ?>" />

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


?>

Hi there thanks for the assistance, ive done what you suggested:

 

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

 

This way only the first record is updated.

 

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

 

This way i leave the healtha hidden field outside the while loop and nothing is updated at all.

<?php
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
foreach($shipid AS $id){
  $updateSQL = sprintf("UPDATE ships SET HealthA=%s WHERE ShipID=%s",
                       GetSQLValueString($_POST['healtha'], "int"),
                       GetSQLValueString($_POST['shipid'], "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 id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">
<input name="Submit" type="submit" id="Submit" value="Repair Ships" />

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

 

What can you suggest? :)

@mrt0030003: I didn't read all your code but my head is spinning already  :shrug:  .. I see several loops on your code and UPDATE inside loops ...  :shrug:  :shrug:

 

could you explain what exactly are you trying to accomplish ?  ... is not more easy to put in an array (using checkboxes maybe) all the ID's that you want to update and execute ONLY ONE update like this (imploding the array obviously):

 

  $updateSQL = "UPDATE ships SET HealthA=HealthA + 1 WHERE ShipID IN ( <your list of ids> )";

 

or maybe I misread your objectives

 

My objectives are to update a single field (health of ship +1) over multiple ship records using the ShipID.

A previous query that selects all the appropriate ships  (e.g the ships that HealthA field - MaxHealthA), puts the ships and health into the hidden fields. Then when submitting the form i wanted ALL the HealthA's to be updated where the ShipIDs = appropriate ships (previous query).

Do see what i mean??

 

I See what your doing with the sql HealthA = Health+1

I wasnt sure if that could be done and so put the HealthA into a hidden field to be updated.

 

Oh man ive been stuck on this for ages now and its well frustrating.

 

Please help. :)

 

 

The processing is done on the same page, so yes. Also interestingly if i  echo $row_Ships['ShipName']; in the while loop:

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

 

All i get outputted is duplication of the Shipname: ship1 ship1

 

 

I assume you're using some sort of checkbox to make the selection of records passing a record id.  Seems that the query should be done in the processing side grabbing each of these id's and updating the DB, instead of attempting to pass all values with the form.  Just pass id's.

Hey I may be way off of what you're trying to do but how about adding this to your form having this as the only value being passed.

while ($row = mysql_fetch_assoc($Ships)){
$shipid=$row['ShipID']; 
  echo '<input name="$shipid" type="checkbox" value="change"/>'; 
}

Then for processing try this. ( Hope I got it right)

IF ($_POST['MM_update']){
foreach($_POST as $key => $val){
//IF you need to grab and modify info you'd do it here.  For example
$getvalues = mysql_query("SELECT * from ships WHERE ShipID=$key");
WHILE ($getvalues = mysql_fetch_array($getvalues)) {
$newvalue = ($getvalues['HealthA'])+1;
}
mysql_query("UPDATE ships SET HealthA='$newvalue' WHERE ShipID=$key");
}
}//end post

I'll be offline after this.  Hope you get it sorted out.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.