Hi, This is a code which shows all the records from database and also show EDIT and DELETE Button for each Record, Now I want only one DELETE Button for ALL Records and use check boxes to select and DELETE. Please Check this Code:

<?php
  $server="localhost";
  $user="root";
  $pass="root";
  $database="new";
  
   $cmd=$_POST["cmd"];
   $eno=$_POST["eno"];
   $ename=$_POST["ename"];
   $edate=$_POST["edate"];
    $Longitude=$_POST["Longitude"];
	$Lat=$_POST["Lat"];
	$Address=$_POST["Address"];
	$Region=$_POST["Region"];
	$City=$_POST["City"];
	$MaintainedBy=$_POST["MaintainedBy"];
	$StatusNopatPat=$_POST["StatusNopatPat"];
	$Priority=$_POST["Priority"];
    
   # Connect to database
   $cn=mysql_connect($server,$user,$pass);
   if(!$cn) {
       die ("<br/>DB not connnected : " . mysql_error()); 
    }
   # Select a database	
   $db=mysql_select_db($database,$cn);
	if(!$db)
	   die(mysql_error() . "  " . mysql_errno());

 if(isset($cmd,$eno))
     {
# --- When edit button press --------
         if($cmd=="Edit") {
	  
	  $res = mysql_query("SELECT * FROM Info
WHERE SiteId='$eno' ");
	  
	  $r=mysql_fetch_row($res);
print<<<TTT
<form method="post" action="editdelete.php">
<br/>SiteID <input type="text" name="eno"  value="$r[0]"  />
<br/>Alias <input type="text" name="ename" value="$r[1]"/>
<br/>Code <input type="text" name="edate" value="$r[2]"/>
<br/>Long <input type="text" name="Longitude" value="$r[3]"/>
<br/>Lat <input type="text" name="Lat" value="$r[4]"/>
<br/>Address <input type="text" name="Address" value="$r[5]"/>
<br/>Region <input type="text" name="Region" value="$r[6]"/>
<br/>City <input type="text" name="City" value="$r[7]"/>
<br/>Maintained By<input type="text" name="MaintainedBy" value="$r[9]"/>
<br/>PAT Status<input type="text" name="StatusNopatPat" value="$r[10]"/>
<br/>Priority<input type="text" name="Priority" value="$r[15]"/>
<br/><input type="submit" name="cmd" value="Update"/>
<input type="submit" name="cmd" value="Cancel"/>
<hr/>
</form>     
TTT;
}
 # -- Delete a record --
  if($cmd=="Delete") {
  $sql = mysql_query("delete FROM Info
WHERE SiteId='$eno' ");
     echo "Record deleted";
     }
 # -- Update a record --
  if($cmd=="Update") {
 /* $sql = mysql_query("UPDATE Info SET Alias='$ename',Code='$edate',Longitude='$Longitude',Lat='$Lat' where  
               SiteID='$eno' ");*/
$query=mysql_query("update Info set alias='$ename',code='$edate',Longitude='$Longitude',Lat='$Lat',Address='$Address',Region='$Region',City='$City',MaintainedBy='$MaintainedBy',StatusNopatPat='$StatusNopatPat',Priority='$Priority' where siteid='$eno'");

           echo "Record updated";
    }
}    
	   
   # -- Show the list  - Grid
	$sql="select * from Info";
	$res=mysql_query($sql,$cn);
	if(!$res)
	   die ("<br/>" . mysql_error());

	 print "<table cellpadding=1 cellspacing=1 width=100% style=font-size:12px>";
print "<tr><th bgcolor=#5D9BCC>SiteID</th bgcolor=#5D9BCC><th bgcolor=#5D9BCC>Alias</th><th bgcolor=#5D9BCC>Code</th><th bgcolor=#5D9BCC>Long</th><th bgcolor=#5D9BCC>Lat</th><th bgcolor=#5D9BCC>Address</th><th bgcolor=#5D9BCC>Region</th><th bgcolor=#5D9BCC>City</th><th bgcolor=#5D9BCC>Maintained By</th><th bgcolor=#5D9BCC>PAT Status</th><th bgcolor=#5D9BCC>Priority</th><th bgcolor=#5D9BCC>Action</th></tr>";		   
	 while($r=mysql_fetch_array($res))
	   {
	     print "<tr bgcolor=#FEE9A9><form method='post' action='editdelete.php'>";
	     print "<td>$r[0]</td><td>$r[1]</td><td>$r[2]</td><td>$r[3]</td><td>$r[4]</td><td>$r[5]</td><td>$r[6]</td><td>$r[7]</td><td>$r[9]</td><td>$r[10]</td><td>$r[15]</td><td>";
		 print "<input type='hidden' value='$r[0]' name='eno'/>";
		 print "<input type='submit' name='cmd' value='Edit'/>";
		 		 print "<input type='submit' name='cmd' value='Delete'/>";
      print "</td></form></tr>";				 
	   }
	 print "</table>";  
      mysql_close($cn);
?>

Please give me easiest way of doing this.
Like Hotmail Email Inbox when we checked many Emails we Simply press DELETE Button.

Recommended Answers

All 12 Replies

Add following after Line # 86

print "<input type='checkbox'
               name='delcheck[]'
               value='$r[0]'/>";

Write following at Line # 58

if($cmd=="Delete") 
   {  
     foreach($_POST["delcheck"] as $delid) {
       $sql ="delete FROM Info WHERE SiteId='$delid'");
       mysql_query($sql);
      }
    }
commented: Great Example +1

Thanks Adapost . Its Working.
Now Please Tell me How to remove DELETE Button from each Record. and Put DELETE Button on the Top of Records.
Thanks........

Do you want to put DELETE button at the top of each record?
Remove it from the current location and paste

print "<input type='submit' name='cmd' value='Delete'/>";

after the line 84

print "<tr><td colspan='5'><input type='submit' name='cmd' value='Delete'/></td></tr>";

No I need Just one Delete Button for Whole Form.

Adapost its showing Delete Button on the top of each Record.
i just need one DELETE Button above the Table Headers like Hotmail Email Account Inbox.
And When I selected 2 Records the DELETE Button only Delete the corresponding Record not Both.

Wrap up your code from line # 74 and onward with another html form tag and add the DELETE button.

Please tell me because its difficult to put Delete Button Inside the same form and When i put new Button in new Form It does not work so please please eidt this code for me. please . thanks. Adapost.

Please Adapost Tell me.

What I said earlier - Prepare your self for PHP. Don't waste your time to find code, good code, better code, and the best code.

I will learn with the passage of time. please do no be angry. thanks.
For all your Help.
Take Care.

nice reply

Hi,
his name is not

adapost

, rather his name is

ADATAPOST

.....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.