Jump to content

How to do the deletion using checkbox...


Vinsanity

Recommended Posts

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
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]

:)
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>&nbsp;</td> <td><input type='submit' name='action' value='Delete selected'></td> </tr>";
echo '</table></form>';
?>
[/code]
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?
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 :-)
@taith

Parse error: syntax error, unexpected T_VARIABLE, expecting ']'

Needs to be

if($_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]
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]
[pre]
  +------------+-------------+
  |    id      |    name    |
  +------------+-------------+
  |  1        |    foo      |
  |  12345699  |    bar      |
  +------------+-------------+
[/pre]

???

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.