I WanT To Code PHP Posted April 2, 2013 Share Posted April 2, 2013 Hello, I have this piece of code it it currently only updates the last value in the databse. I need all of the fields to update there value. Can anyone help me out ? <fieldset> <legend>Ticket Categorys</legend> <form method='post' action='admin.php?do=ticketCats'> <table width='100%' style='background: #dddddd; padding:2px;'> <tr style='background: #C4C4C4;'> <td style='font-weight: bold;'><center>ID</center></td> <td style='font-weight: bold;'>Category</td> <td style='font-weight: bold;'><center>Action</center></td> </tr> <? $color="1"; $query = mysql_query(" SELECT * FROM ticket_cats ORDER BY name ASC "); $level2 = array(); while ($row = mysql_fetch_assoc($query)) { $level2[] = array('id'=>$row['id'],'name'=>$row['name']); if($color==1) { ?> <tr style='background: #fff;'> <? $color="2"; } else { // Set $color back to 1 $color="1"; } ?> <td style='width:15px;'><input type='hidden' value='<?echo $id;?>' name='id[]'><center><?echo $id;?></center></td> <td><input type='text' name='catname' style='width:400px;' value='<?echo $name;?>'/></td> <td><center><input type='submit' name='deleteCat' value='Delete ID:(<?echo $id;?>)' /></center></td> </tr> <? } ?> <tr> <td> <input type='submit' name='updateAllCats' value='Update Categorys' /> </td> </tr> </table> </form> </fieldset> <? if(!empty($_POST['updateAllCats'])) { $catname = $_POST['catname']; $ida = $_POST['id']; mysql_query("UPDATE ticket_cats SET name='$catname' WHERE id='$ida'"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/276411-help-update-database-from-while-loop/ Share on other sites More sharing options...
abrahamgarcia27 Posted April 2, 2013 Share Posted April 2, 2013 (edited) Use something like this $ida = $_POST['id']; foreach($ida as $id){ $catname = $_POST['catname']; $id1 = $id; mysql_query("UPDATE ticket_cats SET name='$catname' WHERE id='$id1'"); } Edited April 2, 2013 by abrahamgarcia27 Quote Link to comment https://forums.phpfreaks.com/topic/276411-help-update-database-from-while-loop/#findComment-1422398 Share on other sites More sharing options...
DaveyK Posted April 2, 2013 Share Posted April 2, 2013 Ofc that works, but I dont think running a query on every single ID doesnt seem very efficient. Look into this http://www.karlrixon.co.uk/writing/update-multiple-rows-with-different-values-and-a-single-sql-query/ Quote Link to comment https://forums.phpfreaks.com/topic/276411-help-update-database-from-while-loop/#findComment-1422408 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.