Description: this will grab/display data inside a SQL table and insert it into a form, with checkboxes for each row of data in the table, WHICH will turn the form into a array (for every checked item) and pass to another page for (in this example) processing and deleting (in thsi example, the table will have a id column which of course is auto_incremented). sorry for the big run-on sentence. Hope you find it handy.
First off, displaying the data and the form itself. This is located on index.php
Code: Select all
<?php
// index.php
$sql = "SELECT * FROM table_name ORDER BY id"; //get the data from the table
$result = mysql_query($sql);
//display all the data in the table placing a check box beside each row of data
while ($row = mysql_fetch_array($result)) {
$id = $row["id"]; // this is our/your auto_increment column
//The form:
echo "<form action=delete.php?$delete method=POST><input type=checkbox name=delete[] value=$id>"; //few things I will explain, notice the delete.php?$delete, this is sending the variable $delete to delete.php (i will get to that later)
//notice the name=delete[], the [] creates the array needed to pass multiple variables, and this will be the $delete var sent via the form action ?$delete. Also - the value is set as id. This will enable us to delete specifics rows as its called/displayed via the while loop.
} // end while loop
//Note that you can display the data inside a table and just make it so the checkbox is to the left, right, above, below, whatever you fancy
//Onto delete.php
<?
if(isset($delete)) { // was the delete variable sent from index.php? is it present
foreach($delete as $id) { //self explanatory
$del = "DELETE FROM table_name WHERE id=$id"; //now you can see why we set the column name id as a var and set the value of the input form as id.
mysql_query($del);
echo "msg(s) deleted";
}
}
?>