Vinsanity Posted January 12, 2007 Share Posted January 12, 2007 I m new to php and i would like to know how to delete the rows using checkboxes... after checked on the checkbox and follow by a DELETE button then the certain row will be deleted in the database... i m using dreamweaver to come out with the UI... anyone can help me? Quote Link to comment https://forums.phpfreaks.com/topic/33863-how-to-do-the-deletion-using-checkbox/ Share on other sites More sharing options...
scotmcc Posted January 12, 2007 Share Posted January 12, 2007 In theory, without any code to go from, I would make the value of the check box the sequence for the record to be deleted. Then submitting the form (after checking the box next to the item you wish to delete) pass to SQL "delete from table where id = $_post[whatever was checked];".Scot Quote Link to comment https://forums.phpfreaks.com/topic/33863-how-to-do-the-deletion-using-checkbox/#findComment-158909 Share on other sites More sharing options...
Vinsanity Posted January 12, 2007 Author Share Posted January 12, 2007 how to identify the checkbox related with the row... any soft code can refer to???is array needed here??? Quote Link to comment https://forums.phpfreaks.com/topic/33863-how-to-do-the-deletion-using-checkbox/#findComment-158917 Share on other sites More sharing options...
matto Posted January 12, 2007 Share Posted January 12, 2007 This was covered off just the other day. Have a read through this, put something together and post back if you are still having problems:[url=http://www.phpfreaks.com/forums/index.php/topic,121091.0.html]http://www.phpfreaks.com/forums/index.php/topic,121091.0.html[/url] :) Quote Link to comment https://forums.phpfreaks.com/topic/33863-how-to-do-the-deletion-using-checkbox/#findComment-158954 Share on other sites More sharing options...
Barand Posted January 12, 2007 Share Posted January 12, 2007 I prefer this method which deletes all the selected records with a single query.Name checkboxes 'delid[]'. Name doesn't matter, only the '[]' on the end.Give each cbox a value of the id of the record on that row.Only checked values are sent on submission so just join the array of sent ids so they form a comma-delimited string EG "1,5,6 8,9" and use a query in the form "DELETE FROM table WHERE id IN (1,5,6 8,9)"Sample code[code]<?php include ('../test/db2.php');/*** Process submitted data*/if (isset($_GET['delid'])) { $deleteList = join (',', $_GET['delid']); $sql = "DELETE FROM customer WHERE cid IN ($deleteList)" ; echo "<p>$sql</p>"; // this just displays the query, call it with mysql_query($sql) to actually delete them}/*** Display records with deletion checkboxes*/$result = mysql_query("select cid, company from customer") or die(mysql_error());echo '<form><table>';while(list ($id, $name)=mysql_fetch_row($result)){ echo "<tr> <td>$name</td> <td><input type='checkbox' name='delid[]' value='$id'></td> </tr>"; }echo "<tr> <td> </td> <td><input type='submit' name='action' value='Delete selected'></td> </tr>";echo '</table></form>';?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33863-how-to-do-the-deletion-using-checkbox/#findComment-159003 Share on other sites More sharing options...
c_shelswell Posted January 12, 2007 Share Posted January 12, 2007 I did something like this just the other day.It was pretty simple just took the checkbox name in a seperate page using $_POST here's my code:[code]foreach ($_POST as $key => $value) { $mediaId[] = $key; } //GETS AND DISPLAYS TITLES THAT HAVE BEEN DELETED $titles = getSomeMedia($mediaId); $files = getFileMedia($mediaId); foreach ($titles as $key => $value) { echo $value."<br />\n"; } //DELETES ACTUAL FILES foreach ($files as $key => $file) { $myFile = '../bundles/'.$file; $fh = fopen($myFile, 'w') or die("can't open file"); fclose($fh); unlink($myFile); }[/code]perhaps that helps? Quote Link to comment https://forums.phpfreaks.com/topic/33863-how-to-do-the-deletion-using-checkbox/#findComment-159004 Share on other sites More sharing options...
taith Posted January 12, 2007 Share Posted January 12, 2007 for example...[code]for($i=0;$i<20;$i++) echo '<input type=checkbox name="name'.$i.'">';[/code][code]$result=mysql_query("SELECT MAX(id) from table");$row=mysql_fetch_array($result);for($i=0;$i<$row['MAX(id)'];$i++) if($_POST[name$i]=="on") mysql_query("DELETE FROM table WHERE id='$id'");[/code]not tested... but it should work :-) assuming the checkboxes are being created by the table in the first place of course lol :-) Quote Link to comment https://forums.phpfreaks.com/topic/33863-how-to-do-the-deletion-using-checkbox/#findComment-159009 Share on other sites More sharing options...
Barand Posted January 12, 2007 Share Posted January 12, 2007 @taithParse error: syntax error, unexpected T_VARIABLE, expecting ']' Needs to beif($_POST[[color=red]"[/color]name$i[color=red]"[/color]]=="on")And if my table has 2 rows like these, that's a lot of looping[pre] +------------+-------------+ | id | name | +------------+-------------+ | 12345678 | foo | | 12345699 | bar | +------------+-------------+[/pre] Quote Link to comment https://forums.phpfreaks.com/topic/33863-how-to-do-the-deletion-using-checkbox/#findComment-159033 Share on other sites More sharing options...
taith Posted January 12, 2007 Share Posted January 12, 2007 if you have high numbers you can speed it up a bit like this...[code]$result=mysql_query("SELECT MAX(id), MIN(id) from table");$row=mysql_fetch_array($result);for($i=$row['MIN(id)'];$i<$row['MAX(id)'];$i++) if($_POST[name$i]=="on") mysql_query("DELETE FROM table WHERE id='$id'");[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33863-how-to-do-the-deletion-using-checkbox/#findComment-159052 Share on other sites More sharing options...
Barand Posted January 12, 2007 Share Posted January 12, 2007 [pre] +------------+-------------+ | id | name | +------------+-------------+ | 1 | foo | | 12345699 | bar | +------------+-------------+[/pre]??? Quote Link to comment https://forums.phpfreaks.com/topic/33863-how-to-do-the-deletion-using-checkbox/#findComment-159145 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.