indiWiz.com
SiteSearch:
Jump:

Site Map | WizTools.org | jCraze Blog
Web Developer's Den!
Home : WDD : ServerSide
This Article...
Print Version
Add Comment
View Comments
ServerSide
Apache on Linux
Apache User Directory Configuration
PostgreSQL Configuration
CGI: What and How?
PHP Jump Start Guide
Arrays in PHP
PHP and HTML Forms
Variable Passing in PHP
PHP 'include'
Drop-down Menu Navigation in PHP
Edit Your Site Online Using PHP
PHP MySQL Polling System
Dynamic, Search Engine Friendly Pages
Quot Ticker Using SSI
Regular Expressions Reference
PHP File Browser
PHP DB Deletion/Approval Script
Sections

Commniquè
Sign Guestbook!
Read Guestbook!
MailMe!

PHP DB Deletion/Approval Script

Often 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:

guest_idguest_nameguest_urlguest_messageDelete?
0804homehttp://www.indiwiz.com/home
0803Sylvia-Sainthttp://www.indiwiz.com/I cant view your site on my SonyEricson P800 mobile phone. Seems there is a bug with styles or an GPRS connection problem.
0802asasasasas

Would you like to create something like this? Read on!

Creating The Display Table

First, 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!


User Comments

Add Comment

[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.