Jump to content


Photo

Delete with checkbox?


  • Please log in to reply
2 replies to this topic

#1 Mr Chris

Mr Chris
  • Members
  • PipPipPip
  • Advanced Member
  • 336 posts

Posted 12 July 2006 - 01:28 PM

Hi Guys,

Can someone point me in the direction of a php tutorial that will get the row details from one specified mysql database table.  Click a checkbox and then when submit is hit it deletes the row from the mysql database?

Thanks

Chris


#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 12 July 2006 - 01:39 PM

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:
<?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]\" />";
}
?>

notice that the name 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:
<?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);
  }
} 

?>

that's really all there is to it.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 Mr Chris

Mr Chris
  • Members
  • PipPipPip
  • Advanced Member
  • 336 posts

Posted 12 July 2006 - 02:25 PM

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

Here’s what I am trying to do:

my two tables:

cms_stories
story_id int(4) No auto_increment
headline varchar(128) latin1_swedish_ci No
body_text text latin1_swedish_ci No

cms_pictures
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 cms_pictures 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/...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 cms_stories table click a checkbox and hit submit and that row in the cms_pictures table is deleted.

A bit like
Posted Image

Here's my attempt, but miserably failed :-(
<?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>

Thanks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users