mrt003003 Posted May 3, 2011 Share Posted May 3, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/235464-update-multiple-records-with-a-single-hiddenfield/ Share on other sites More sharing options...
mrt003003 Posted May 4, 2011 Author Share Posted May 4, 2011 Any ideas anyone? Quote Link to comment https://forums.phpfreaks.com/topic/235464-update-multiple-records-with-a-single-hiddenfield/#findComment-1210448 Share on other sites More sharing options...
fugix Posted May 4, 2011 Share Posted May 4, 2011 have you tested that code to see what you receive? Quote Link to comment https://forums.phpfreaks.com/topic/235464-update-multiple-records-with-a-single-hiddenfield/#findComment-1210459 Share on other sites More sharing options...
mrt003003 Posted May 4, 2011 Author Share Posted May 4, 2011 Yes it only ever updates a single record. Not all of them. Quote Link to comment https://forums.phpfreaks.com/topic/235464-update-multiple-records-with-a-single-hiddenfield/#findComment-1210462 Share on other sites More sharing options...
fugix Posted May 4, 2011 Share Posted May 4, 2011 so are you trying to send the form multiple times using several values? Quote Link to comment https://forums.phpfreaks.com/topic/235464-update-multiple-records-with-a-single-hiddenfield/#findComment-1210470 Share on other sites More sharing options...
mrt003003 Posted May 4, 2011 Author Share Posted May 4, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/235464-update-multiple-records-with-a-single-hiddenfield/#findComment-1210483 Share on other sites More sharing options...
tomtimms Posted May 4, 2011 Share Posted May 4, 2011 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/235464-update-multiple-records-with-a-single-hiddenfield/#findComment-1210506 Share on other sites More sharing options...
fugix Posted May 4, 2011 Share Posted May 4, 2011 yeah, you would need to include your hidden input in your while loop to make it dynamic Quote Link to comment https://forums.phpfreaks.com/topic/235464-update-multiple-records-with-a-single-hiddenfield/#findComment-1210518 Share on other sites More sharing options...
mrt003003 Posted May 4, 2011 Author Share Posted May 4, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/235464-update-multiple-records-with-a-single-hiddenfield/#findComment-1210563 Share on other sites More sharing options...
mikosiko Posted May 4, 2011 Share Posted May 4, 2011 @mrt0030003: I didn't read all your code but my head is spinning already .. I see several loops on your code and UPDATE inside loops ... 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 Quote Link to comment https://forums.phpfreaks.com/topic/235464-update-multiple-records-with-a-single-hiddenfield/#findComment-1210576 Share on other sites More sharing options...
mrt003003 Posted May 4, 2011 Author Share Posted May 4, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235464-update-multiple-records-with-a-single-hiddenfield/#findComment-1210579 Share on other sites More sharing options...
mikosiko Posted May 4, 2011 Share Posted May 4, 2011 running late... but check your PM's Quote Link to comment https://forums.phpfreaks.com/topic/235464-update-multiple-records-with-a-single-hiddenfield/#findComment-1210580 Share on other sites More sharing options...
Drummin Posted May 4, 2011 Share Posted May 4, 2011 Maybe if you could show us a print of what's being posted it would help. print_r($_POST); Quote Link to comment https://forums.phpfreaks.com/topic/235464-update-multiple-records-with-a-single-hiddenfield/#findComment-1210590 Share on other sites More sharing options...
mrt003003 Posted May 4, 2011 Author Share Posted May 4, 2011 Thats interesting if i print_r($_POST); i get: Array ( ) Array ( ), So the array isnt forming? Quote Link to comment https://forums.phpfreaks.com/topic/235464-update-multiple-records-with-a-single-hiddenfield/#findComment-1210597 Share on other sites More sharing options...
Drummin Posted May 4, 2011 Share Posted May 4, 2011 I assumed the call to action was a POST. How are you triggering this? Quote Link to comment https://forums.phpfreaks.com/topic/235464-update-multiple-records-with-a-single-hiddenfield/#findComment-1210600 Share on other sites More sharing options...
mrt003003 Posted May 4, 2011 Author Share Posted May 4, 2011 Erm im not sure i follow you, it is a POST i thought? Oh man im sorry i feel stupid :| Quote Link to comment https://forums.phpfreaks.com/topic/235464-update-multiple-records-with-a-single-hiddenfield/#findComment-1210608 Share on other sites More sharing options...
mrt003003 Posted May 4, 2011 Author Share Posted May 4, 2011 I thought it was a POST.. Jeeez i feel stupid :| Quote Link to comment https://forums.phpfreaks.com/topic/235464-update-multiple-records-with-a-single-hiddenfield/#findComment-1210611 Share on other sites More sharing options...
Drummin Posted May 4, 2011 Share Posted May 4, 2011 Did you add the print_r($_POST); to the processing page? Quote Link to comment https://forums.phpfreaks.com/topic/235464-update-multiple-records-with-a-single-hiddenfield/#findComment-1210615 Share on other sites More sharing options...
mrt003003 Posted May 4, 2011 Author Share Posted May 4, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/235464-update-multiple-records-with-a-single-hiddenfield/#findComment-1210619 Share on other sites More sharing options...
Drummin Posted May 4, 2011 Share Posted May 4, 2011 Because the input names are not unique when you loop this in an array. Quote Link to comment https://forums.phpfreaks.com/topic/235464-update-multiple-records-with-a-single-hiddenfield/#findComment-1210622 Share on other sites More sharing options...
mrt003003 Posted May 4, 2011 Author Share Posted May 4, 2011 then please, how do i make them unique? Quote Link to comment https://forums.phpfreaks.com/topic/235464-update-multiple-records-with-a-single-hiddenfield/#findComment-1210623 Share on other sites More sharing options...
Drummin Posted May 4, 2011 Share Posted May 4, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235464-update-multiple-records-with-a-single-hiddenfield/#findComment-1210624 Share on other sites More sharing options...
Drummin Posted May 4, 2011 Share Posted May 4, 2011 Shouldn't that be echo $row['ShipName']; Quote Link to comment https://forums.phpfreaks.com/topic/235464-update-multiple-records-with-a-single-hiddenfield/#findComment-1210627 Share on other sites More sharing options...
mrt003003 Posted May 4, 2011 Author Share Posted May 4, 2011 How do you parse without a form? Quote Link to comment https://forums.phpfreaks.com/topic/235464-update-multiple-records-with-a-single-hiddenfield/#findComment-1210635 Share on other sites More sharing options...
Drummin Posted May 4, 2011 Share Posted May 4, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235464-update-multiple-records-with-a-single-hiddenfield/#findComment-1210639 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.