LiamH Posted March 3, 2009 Share Posted March 3, 2009 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 Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted March 3, 2009 Share Posted March 3, 2009 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); } ?> Quote Link to comment Share on other sites More sharing options...
LiamH Posted March 3, 2009 Author Share Posted March 3, 2009 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); Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted March 3, 2009 Share Posted March 3, 2009 Sorry, I assumed that ArticleID was an integer. Quote Link to comment 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.