Jump to content

Recommended Posts

Hi all.

 

I have a page that has a list of data with checkboxes next to it. The idea is that when selected, the data is deleted from the table. It works in a way, but only one of the records are actually deleting.

 

The system consists of 2 pages, one with the listed data and checkboxes and when the form is submitted, taken to another page that has the sql in the background to perform the action.

 

This is the code I have;

 

//page 1

// if rows are returned, print them one after another
echo "<form action='delete.php' method='post'>";  
	echo "<table class='articlecreate'>";
	while($row = mysql_fetch_assoc($result)) {
		echo "<tr>";
			echo "<td><a href='edit.php?ArticleID=".$row['ArticleID']."'>".$row['Headline']."</a></td>";
// the line of code below is for the delete checkboxes
			echo "<td><input name='ArticleID' type='checkbox' value='".$row['ArticleID']."'></td>";
		echo "</tr>";
	}
		echo "<tr>";
			echo "<td><input type='submit' name='delete' value='Delete'></td>"; 
		echo "</tr>";
	echo "</table>";
echo "</form>";

//page 2

// variable names defined for delete query
$articleID=$_POST['ArticleID'];

// create and execute query. Query states the information received from the previous page needs to be deleted from the table
$query="DELETE FROM tblName WHERE ArticleID='$articleID'";
mysql_query($query);

// check for errors or process query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// state record has been deleted
echo "<td class='content'>Record deleted!</td>";

// close connection
mysql_close($connection);

 

What am I missing from this, can anyone help please?

 

Thanks

Change the HTML input name from ArticleID to ArticleID[] then PHP will see this as an array.

 

Then put this on the processing page

 

<?php
foreach ($_POST['ArticleID'] as $id){
   $query="DELETE FROM tblName WHERE ArticleID = $id";
   mysql_query($query);
}
?>

Thanks very much.

 

There was one problem with the code for anyone else experiencing an issue like this, the code should read;

 

foreach ($_POST['ArticleID'] as $id){
	$query="DELETE FROM tblArticle WHERE ArticleID = '$id'";
	mysql_query($query);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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