Jump to content

Need Help with a Delete Code


DavidPr

Recommended Posts

I want to query the database, display the results with a checkbox next to the entries so that I can tick the box next to each entry that I want to delete. The form will go to a page that will delete all the entries from the database that were ticked in the previous page.I don't know if I have the form setup correctly and I don't know how to write the script on the delete page to do this. Any help would be appreciated.Thanks.The Form:

<form action="delete_event.php" method="post"><table width="100%" border="0" cellspacing="0" cellpadding="5"><?phpinclude("includes/mysql_connect.php");if(!isset($cmd)) { $query = "select * from events where user_id = '".$_SESSION['user_id']."'";$result = @mysql_query ($query);$num = mysql_num_rows($result);if ($num >0)  {	echo "<tr><th width='58%' align='left'>Title</th><th width='42%'> </th></tr>";while ($row = mysql_fetch_array($result, MYSQL_ASSOC))	{ 	$event_title = stripslashes($row['event_title']);	$id = $row["id"];  	echo "	<tr>	<td width='58%'><font face=arial size=2>$event_title</a></font></td>	<td width='42%'><input type='checkbox' name='to_delete' value='$id'></td>	</tr>	<tr><td colspan='2' style='border-top: 1px solid #666'> </td></tr>	"; 	}  }  else  {  echo "<br><p><span style='color:red'><strong>There are no events.</strong></span></p>";  }}?></table></form>

The Delete Page:

<?php$id = $_GET["id"];include("includes/mysql_connect.php");	$sql = "DELETE FROM events WHERE id=$id";	$result = mysql_query($sql);echo "<br><br><p align='center'><span style='color:red'><b>Event Deleted!</b></p>";?>

Link to comment
Share on other sites

The form is set to use the post method and contains checkboxes called to_delete. The form processor checks inside $_GET for an item called id. I don't think these two scripts are related. Once you have the processing script looking in the right place for the right element, you need to put brackets after the checkbox names in the form to get all the different IDs inside an array, and then you can loop through the array in PHP and delete each ID.<input type='checkbox' name='to_delete[]' value='$id'>

Link to comment
Share on other sites

OK, so I change the form to the below, adding a submit button and the modified checkbox input. The name in the checkbox input can be changed to whatever, but how do we carry it over to the delete page?

<?phpinclude("includes/mysql_connect.php");if(!isset($cmd)) { $query = "select * from events where user_id = '".$_SESSION['user_id']."'";$result = @mysql_query ($query);$num = mysql_num_rows($result);if ($num >0)  {	echo "<tr><th width='58%' align='left'>Title</th><th width='42%'> </th></tr>";while ($row = mysql_fetch_array($result, MYSQL_ASSOC))	{ 	$event_title = stripslashes($row['event_title']);	$id = $row["id"];  	echo "	<tr>	<td width='58%'><font face=arial size=2>$event_title</a></font></td>	<td width='42%'><input type='checkbox' name='to_delete[]' value='$id'></td>	</tr>	<tr><td colspan='2' style='border-top: 1px solid #666'> </td></tr>	<input type='submit' name='submit' value='Submit'> 	"; 	}  }  else  {  echo "<br><p><span style='color:red'><strong>There are no events.</strong></span></p>";  }}?></table></form>

On the delete page I changed the GET method to POST:

<?phpif ($_POST["submit"]) {include("includes/mysql_connect.php");	$sql = "DELETE FROM events WHERE id=$id";	$result = mysql_query($sql);echo "<br><br><p align='center'><span style='color:red'><b>Event Deleted!</b></p>";?>

Would this work though, seeing how we may have several id's?

Link to comment
Share on other sites

You're never setting $id in the delete statement. You try to delete something with a certain ID that you're saying is in the $id variable, but you never set $id to anything. If you put the brackets on the name then you can get the array of IDs like this:$ids = $_POST['to_delete'];You need to put the same name there that you have in the form, so the name does matter. Once you get the array of IDs, you can loop through the array to delete each ID. You can see how to loop through an array here:http://www.php.net/manual/en/control-structures.foreach.php

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...