Jump to content

[SOLVED] Checkboxes not deleting all selected data


LiamH

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);
}
?>

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.