Mr Chris Posted July 12, 2006 Share Posted July 12, 2006 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?ThanksChris Quote Link to comment https://forums.phpfreaks.com/topic/14385-delete-with-checkbox/ Share on other sites More sharing options...
obsidian Posted July 12, 2006 Share Posted July 12, 2006 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]<?phpwhile ($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. Quote Link to comment https://forums.phpfreaks.com/topic/14385-delete-with-checkbox/#findComment-56756 Share on other sites More sharing options...
Mr Chris Posted July 12, 2006 Author Share Posted July 12, 2006 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 Nostory_id int(4) No auto_incrementI use story_id as a Foreign Key in my [b]cms_pictures[/b] table to create an association link between a story and a pictureSo 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=60and 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]<?phpinclude("**********");// 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 Quote Link to comment https://forums.phpfreaks.com/topic/14385-delete-with-checkbox/#findComment-56767 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.