Jump to content

Delete with checkbox?


Mr Chris

Recommended Posts

i'm not aware of one, but here's the short of it: if, when you list your records, display your checkboxes for deletion as such:
[code]
<?php
while ($x = mysql_fetch_array($sql)) {
  // as part of your row, here's my checkboxes:
  echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$x[id]\" />";
}
?>
[/code]

notice that the [b]name[/b] of the input fields will all be "delete[]." this will create an array in your $_POST['delete'] variable on submission that will contain only those records you have checked for deletion, so you can handle them like this:
[code]
<?php

// if form has been submitted, run this:
if (count($_POST['delete']) > 0) {
  foreach ($_POST['delete'] as $val) {
    $sql = "DELETE FROM tableName WHERE id = '$val'";
    mysql_query($sql);
  }
}

?>
[/code]

that's really all there is to it.
Link to comment
https://forums.phpfreaks.com/topic/14385-delete-with-checkbox/#findComment-56756
Share on other sites

Thanks – have had a look, but am stuck and would appreciate help,

Here’s what I am trying to do:

my two tables:

[b]cms_stories[/b]
story_id int(4) No auto_increment
headline varchar(128) latin1_swedish_ci No
body_text text latin1_swedish_ci No

[b]cms_pictures[/b]
pic_id int(3) No auto_increment
date_uploaded varchar(200) latin1_swedish_ci No
main_dir varchar(200) latin1_swedish_ci No
main_name varchar(200) latin1_swedish_ci No
thumb_dir varchar(200) latin1_swedish_ci No
thumb_name varchar(200) latin1_swedish_ci No
story_id int(4) No auto_increment

I use story_id as a Foreign Key in my [b]cms_pictures[/b] table to create an association link between a story and a picture

So Say I clicked on a link that I wanted to delete a picture associated with a story:

http://www.site.com/stories/delete.php?story_id=60

and this link looks for the story_id in the cms_stories table, but returned the image details from the cms_pictures.

So then I just want to be able to view the image from the [b]cms_stories[/b] table click a checkbox and hit submit and that row in the [b] cms_pictures[/b] table is deleted.

A bit like
[img]http://www.slougheaz.org/image.jpg[/img]

Here's my attempt, but miserably failed :-(
[code=php:0]
<?php
include("**********");
// if form has been submitted, run this:
$story_id = (int)$_GET['story_id'];
if (count($_POST['delete']) > 0) {
  foreach ($_POST['delete'] as $val) {
    $sql = "DELETE FROM cms_pictures WHERE story_id = " . $story_id;
    mysql_query($sql);
header("Location: http://www.site.com/stories/upload_new_picture.php?story_id=" . $story_id);
  }
}

?>
<HTML>
<HEAD>
<TITLE>Delete test</TITLE> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
</HEAD>
<FORM ACTION="".php" method="post" NAME="delete_form"> <TABLE WIDTH="100%" BORDER="0" CELLSPACING="1" CELLPADDING="1">
<TR><TD WIDTH="11%" HEIGHT="24">
<?php
$story_id = (int)$_GET['story_id'];
while ($x = mysql_fetch_array($sql)) {
// as part of your row, here's my checkboxes:
echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$x[story_id]\" />";
}
?>
</TD><TD WIDTH="89%" HEIGHT="24">
<?php
$story_id = (int)$_GET['story_id'];
$SQL = "select thumb_name from cms_pictures where story_id = $story_id";
$result = mysql_query($SQL) OR die(mysql_error());
$row = mysql_fetch_row($result); 
echo '<img src="new_site/pictures/big/'. $row[0] .'">';
?> </TD></TR> </TABLE><P>
<INPUT TYPE="Submit" NAME="Submit" VALUE="Submit""></P>
</FORM>
</BODY>
</HTML> [/code]

Thanks
Link to comment
https://forums.phpfreaks.com/topic/14385-delete-with-checkbox/#findComment-56767
Share on other sites

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.