| Site Map | WizTools.org | jCraze Blog |
![]() |
PHP DB Deletion/Approval ScriptOften one is asked to write code which is used to ``delete''/``approve'' a set of database entries. This is often done by showing the relevant colum in DB with a check box for selecting the colum. Then you have a ``Delete'' or ``Approve'' button for doing deletion or approval operation on the selected columns. A sample output from my Guestbook administrative script: Would you like to create something like this? Read on! Creating The Display TableFirst, the code to display the last 10 entries from the DB table. Create the database connection:
<?
mysql_connect($host,$uid,$pwd) or die("Unable to connect to DB!");
mysql_select_db($db) or die("Unable to select DB!");
?>
Write the function to display the table information:
<?
// Function to display the entries to be deleted
function dispTbl(){
$query = "select guest_id, guest_name, guest_url, guest_message from guests order by guest_id desc limit 10";
$results = mysql_query($query) or die("DB query failed!");
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<table border="1" cellpadding="5" cellspacing="2">
<tr>
<?
// Loop to create the Table heading from the DB column name (from select query)
$k = mysql_num_fields($results);
for($i=0;$i<$k;$i++){
echo "<th>".mysql_field_name($results,$i)."</th>";
}
echo "<th>Delete?</th>\n";
?>
</tr>
<?
// Loop to display the DB table content
while($result = mysql_fetch_array($results)){
echo "<tr>";
for($i=0;$i<$k;$i++){
echo "<td>".$result[$i]."</td>";
}
echo "<td><input type=\"checkbox\" name=\"del[]\" value=\"$result[0]\"></td>";
echo "</tr>\n";
}
mysql_free_result($results);
?>
</table>
<input type="submit" value="Delete Selected!">
</form>
<?
} // Close function dispTbl()
?>
The End...The code to process the server request and perform the requested operation:
<html>
<body>
<?
if($_SERVER['REQUEST_METHOD']=="GET"){
echo "<h2>Guest Book Delete!</h2>";
dispTbl();
}
else{
if(!empty($_POST['del'])){
$arr = $_POST['del'];
$query = "delete from guests where guest_id in (".implode($arr,", ").")";
mysql_query($query) or die("Deletion Failed!");
echo "<h2>Deleted id(s): ".implode($arr,", ")."</h2>";
}
else{
echo "<h2>No id(s) Selected For Deletion</h2>";
}
dispTbl();
}
?>
</body>
</html>
Wasn't that simple? Now customize the script for your requirement. Customization is simple: just change the SQL queries (there are 2 queries) in the above code, everything else will work fine! |
[Quick Stats: Number of main threads: 1, Number of sub-threads: 0]
Sign Guestbook
|
Who is Subhash?
The contents of this site are copyright© 2000-2008, indiWiz.com. All Rights Reserved.