iceangel89 Posted July 11, 2008 Share Posted July 11, 2008 how can i implement something like in gmail where i can select multiple records and delete them for example? or update all with a specific field value eg. update all selected records' "STATUS" to "Closed" [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/114200-how-do-i-do-a-multiple-record-update/ Share on other sites More sharing options...
DarkWater Posted July 11, 2008 Share Posted July 11, 2008 Create an array of checkboxes and iterate through it or use implode() and IN (in the query) to update a bunch at time, or delete a bunch at a time. Quote Link to comment https://forums.phpfreaks.com/topic/114200-how-do-i-do-a-multiple-record-update/#findComment-587172 Share on other sites More sharing options...
Lamez Posted July 11, 2008 Share Posted July 11, 2008 here is my paid system, it will kinda help you out with check boxs and what not: paid.php <?php include ("../../style/include/session.php"); include ("../../style/include/cons/head_2.php"); $num = 0; if($session->isAdmin()){ print '<div class="box"><h2>Select Paid</h2>'; ?> <form action="pay_do.php" method="post"> <table width="87%" border="0"> <tr> <td width="42"><strong>#</strong></td> <td width="127"><strong>First Name </strong></td> <td width="148"><strong>Last Name </strong></td> <td width="203"><strong>Status (username) </strong></td> </tr> <?php $paid = "select * from users order by paid"; $q = mysql_query($paid) or die('Error: ' . mysql_error()); while ($rs=mysql_fetch_array($q)) { $id = $rs['username']; ?> <tr> <td><?php echo $num+=1; ?></td> <td><? echo $rs['first'];?> </td> <td><? echo $rs['last'];?></td> <td> <?php If($rs['paid'] == 0 ){ echo '<input type="checkbox" name="checkbox[]" value="'.$rs['username'].'" />'; } elseif($rs['paid'] == 1 ){ echo "<a href='pay_dele.php?cmd=delete&id=$id'>Paid</a>"; } echo " (".$rs['username'].")"; } ?> </td> </tr> </table> <br /> <input name="Submit" type="Submit" value="Update" /> </form> <?php }else{ header("Location: ../../index.php"); } print '</div>'; include ("../../style/include/cons/foot.php"); ?> pay_do.php <?php include ("../../style/include/session.php"); include ("../../style/include/cons/head_2.php"); print '<div class="box">'; if($session->isAdmin()){ if(isset($_POST['Submit'])){ $boxes = $_POST['checkbox']; foreach($boxes as &$value){ if($value!=='') { $q = "UPDATE `users` SET `paid`=1 WHERE `username`='$value'"; echo $q.'<br />'; mysql_query($q); } } echo "<h2>Processed</h2>"; echo 'Selected users have been processed<br><a href="paid.php">Select Paid/Non-Paid</a>'; }else{ echo "<h2>Error!</h2>"; echo 'No data to process<br><a href="paid.php">Select Paid/Non-Paid</a>'; } }else{ header("Location: ../../index.php"); } print '</div>'; include ("../../style/include/cons/foot.php"); ?> pay_dele.php <?php include ("../../style/include/session.php"); include ("../../style/include/cons/head_2.php"); if($session->isAdmin()){ if(!isset($cmd)) { $result = mysql_query("select * from users order by paid"); while($r=mysql_fetch_array($result)) { $id=$r["username"];//take out the id //make the title a link echo "<a href='?cmd=delete&id=$id'>$id - Delete</a>"; echo "<br>"; } } if($_GET["cmd"]=="delete") { print '<div class="box"><h2>'.$id.' marked as not paid</h2>'; $q = "UPDATE `users` SET `paid`=0 WHERE `username`='$id'"; echo $q.'<br />'; mysql_query($q); print '<br><a href="paid.php">Back to paid</a>'; } }else{ header("Location: ../../index.php"); } print '</div>'; include ("../../style/include/cons/foot.php"); ?> play around with those, they will help Quote Link to comment https://forums.phpfreaks.com/topic/114200-how-do-i-do-a-multiple-record-update/#findComment-587177 Share on other sites More sharing options...
DarkWater Posted July 11, 2008 Share Posted July 11, 2008 One of the "easy" ways to do mass updates is: Let's say you have $_POST['checked'] as a multi-dimensional array of things that should be updated. $query = sprintf('UPDATE users SET paid = 1 WHERE userid IN (%s)', join(',', $_POST['checked'])); Quote Link to comment https://forums.phpfreaks.com/topic/114200-how-do-i-do-a-multiple-record-update/#findComment-587179 Share on other sites More sharing options...
iceangel89 Posted July 11, 2008 Author Share Posted July 11, 2008 thanks all, wow so many solutions in so little time nice i'll try it when i get home Thanks Quote Link to comment https://forums.phpfreaks.com/topic/114200-how-do-i-do-a-multiple-record-update/#findComment-587308 Share on other sites More sharing options...
cooldude832 Posted July 11, 2008 Share Posted July 11, 2008 Update can affect 0-infinity rows for say you just need to be specific in your where clause. If u are trying to update a series of items in a database that have no mysql correlation to them (meaning like fields you can generate a valid where clause for) then checkboxes are your answer. Simply use the method above. However lets say you want to delete all old photo galleries that have less than 10 pictures in them and more than 50 days since last upload to them. MySQL can handle all that (its just an example I thought of) you can simple use a switch for all your "targeted" updates like the one I gave to execute the query desired instead of poking around with checkboxes. Quote Link to comment https://forums.phpfreaks.com/topic/114200-how-do-i-do-a-multiple-record-update/#findComment-587315 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.