Version: 1.0

Type: Sample Code (HOWTO)

Category: Other

License: GNU General Public License

Description: You are able to delete multiple entries in mysql database using checkboxes as an array. It is a plug and play kinda script, to edit the script properties for the mysql database, all you have to do is edit the config.php file variables that are commented for your ease of use. A link back to my site is requested but not required. If you are wishing for assistance in getting the script to work if you have problems, then a permanent link back is required as payment.



You are going to need to have a file called config.php, it should be
setup as follows:

START CONFIG.PHP FILE

<?
// $server is the location of the mysql server
$server = "localhost";

// mysql server username to log onto the mysql database
$db_user = "username";

// mysql server password to log onto the mysql database
$db_pass = "password";

// name of the mysql database
$database = "datbase_name";

// name of the table in the mysql database
$tablename = "table_name";

// what kind of query you want to perform
// as an example of a query name, you could have a delete from or update
// both have been specified so you will have to uncomment which one
// you want to use.
// to uncomment, just remove the // from infront of the $queryname variable

//$queryname = "DELETE FROM";
//$queryname = "UPDATE";

// The query has a "where" clause where it will look for certain information in a particular
// mysql column.  If you want to specify the ID column, then do not change this variable,
// if you want to use a different column name, then change the word inside the "" only
// for example: $wherevalue = "something_here_instead";
$wherevalue = "id";

// time to display the information in the page where you want the form to work
$displayinformationintable = "table_name_to_retrieve_information_from"

// the variable is where you want to use the "where" clause for the variable above
// the information below looks in a row called "tombr" for the "username"
// you will need to change this to reflect what you want 
$displaywhereclause = "`tombr`='$username'";

// define the order by - this can be anything, it is specified below to order by id
$orderby = "id";

// define here if you want your records to display in accending or descending order
// you are going to need to uncomment which one you want to use
//$ascdesc = "asc";
//$ascdesc = "desc";

// form action 
// for example: <form action="usermessages.php" method="post">
$formaction = "usermessages.php";

// select query, the variable below is set to select all entries "SELECT *"
$selectquery = "SELECT *";
?>

END CONFIG.PHP FILE

Then you are going to need to place the following code in your php file, do not change it
otherwise it will not work.

<?
// specify the variables in the config.php file
include("config.php");
// If the form was submitted, delete the items selected from the database 
$connection = mysql_connect("$server", "$db_user", "$db_pass");
$db = mysql_select_db("$database", $connection);
if($_POST["deleted_items"] != "") 
{ 
	$delItems = implode(",", $HTTP_POST_VARS['deleted_items']);
	for($x=0;$x<count($delItems);$x++)
	{
		$query = "$queryname $tablename WHERE $wherevalue IN ($delItems)";
		$result = mysql_query($query, $connection) or die(mysql_error()); 
	}
}
?>

The next part is that you are going to need to add a checkbox that will echo for all of your
entries that you want to $queryname in the mysql database.  You are not going to need to 
change the following checkbox code, because the value will look for what ever you have 
specified in the $wherevalue variable and will automatically insert that value for you for each
of the checkboxes that are generated.

<input type=\"checkbox\" name=\"deleted_items[]\" value=\"".$rows['$wherevalue']."\">

You will also need to make sure that the checkboxes are contained in a form, otherwise it will
not work.

as an example:

<form action="$formaction" method="post">
<table>
<tr>
<td><input type=\"checkbox\" name=\"deleted_items[]\" value=\"".$rows['$wherevalue']."\"></td>
</tr>
</form>

My display table looks like the following:

<?php
$connection = mysql_connect("$server", "$db_user", "$db_pass");
$db = mysql_select_db("$database", $connection);
$query = "$selectquery FROM $tablename where $displaywhereclause order by $orderby $ascdesc";
$result = mysql_query($query, $connection);
echo "<form action=\"$formaction\" method=\"post\"><table width=\"100%\" border=\"0\">";
echo "<tr><td><input type=\"image\" src=\"images/inbox/trash.gif\" alt=\"Delete Selected Messages\" accesskey=\"d\"></td><td><b>$lang[neworread]</b></td><td><b>From</b></td><td><b>$lang[messagedate]</b></td><td><b>$lang[inboxtitle]</b>
</td></tr>";
while ($rows = mysql_fetch_array($result))
{
$new = "$rows[new]";
if($new=='yes')
{
  echo "<tr><td><input type=\"checkbox\" name=\"deleted_items[]\" value=\"".$rows['$wherevalue']."\" title=\"Select this message for deletion: Unread message from $rows[frommbr] with the subject of $rows[title] which was sent on $rows[date] PST\"></td><td><img src=\"images/inbox/unopened.gif\" alt=\"Unread Message\"></td><td><b><a href=\"userprofile.php?user=$rows[frommbr]\" accesskey=\"p\">$rows[frommbr]</a></b></td><td><b>$rows[date] PST</b></td><td><b><a href=\"viewmessage.php?id=$rows[nomer]\" accesskey=\"o\">$rows[title]</a></b></td></tr>";
}
elseif($new=='ne')
{
  echo "<tr><td><input type=\"checkbox\" name=\"deleted_items[]\" value=\"".$rows['$wherevalue']."\" title=\"Select this message for deletion: Read message from $rows[frommbr] with the subject of $rows[title] which was sent on $rows[date] PST\"></td><td><img src=\"images/inbox/opened.gif\" alt=\"Read Message\"></td><td><a href=\"userprofile.php?user=$rows[frommbr]\" accesskey=\"p\">$rows[frommbr]</a></td><td>$rows[date] PST</td><td><a href=\"viewmessage.php?id=$rows[nomer]\" accesskey=\"o\">$rows[title]</a></td></tr>";
}
}
?>
<?
$num_rows = mysql_num_rows($result);
if($num_rows == 0) {
echo "<tr><td colspan=\"5\" class=\"center\">There are currently no messages in your inbox folder.</td></tr>";
}
?>
<?
echo "<tr><td><input type=\"image\" src=\"images/inbox/trash.gif\" alt=\"Delete Selected Messages\" accesskey=\"d\"></td><td colspan=4>&nbsp;</td></tr></table>";
?>
</form>

I know this works as it works on my website www.brailleschool.com
I hope you enjoy using this code.  A link back to my site is requested but not required.
No assistance is given for the use of this code unless a permanent link back is given in exchange