Connecting Tech Pros Worldwide Forums | Help | Site Map

how to delete selected records from database

Familiar Sight
 
Join Date: Nov 2007
Posts: 244
#1: Nov 29 '07
hello every one

i want to delete the selected records from database. selection is based on check box. so when user will select any check box it will delete the corresponding records from database.

how can i do that using php. if any one havin code for that please help me

thanks

Newbie
 
Join Date: Mar 2007
Posts: 16
#2: Dec 1 '07

re: how to delete selected records from database


Hi,
You can delete selected records in php. For example,

If you are using a file called test.php,

<input type="checkbox" name="delete" value="<?=$result['id']?>" onClick="return DeleteThisData(this.value);">

This is the checkbox you are using while retrieving datas from the database.
I am calling a javascript function called DeleteThisData();

function DeleteThisData(value)
{
location.href="test.php?del="+value
}

This is client side Script which passes the selected record to the url.

In Serverside coding ,

<?php
if($_REQUEST['del'])
{
$sql="delete from tablename where id=".$_REQUEST['id'];
mysql_query($sql) or die(mysql_error());
}
?>

In this way you can delete a single record by record.
Familiar Sight
 
Join Date: Nov 2007
Posts: 244
#3: Dec 3 '07

re: how to delete selected records from database


Quote:

Originally Posted by arunj82

Hi,
You can delete selected records in php. For example,

If you are using a file called test.php,

<input type="checkbox" name="delete" value="<?=$result['id']?>" onClick="return DeleteThisData(this.value);">

This is the checkbox you are using while retrieving datas from the database.
I am calling a javascript function called DeleteThisData();

function DeleteThisData(value)
{
location.href="test.php?del="+value
}

This is client side Script which passes the selected record to the url.

In Serverside coding ,

<?php
if($_REQUEST['del'])
{
$sql="delete from tablename where id=".$_REQUEST['id'];
mysql_query($sql) or die(mysql_error());
}
?>

In this way you can delete a single record by record.



Thanks Arjun

Actually I want to delete the multiple record so please how can I do that..

Give the code for that as well

Thanks in Advance..
Newbie
 
Join Date: Mar 2007
Posts: 16
#4: Dec 3 '07

re: how to delete selected records from database


Hi,

Here i m giving a example for deleting all records and also u can delete a single record. U check it and if u have any doubt u pls message me. If you press all button in the headings, it will delete all records.


Sample Codings:

<?php
$con=mysql_connect("localhost","root","simply123") or die(mysql_error());
mysql_select_db("databasename",$con) or die(mysql_error());

// Give r connection details for database

if($_REQUEST['delete'])
{
$totalarray=sizeof($del);
for($i=0;$i<$totalarray;$i++)
{
if(!empty($del[$i])){
mysql_query("delete from tbl_sample where id=".$del[$i]) or die(mysql_error());
}
}
$msg="Values Deleted Successfully";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<script language="JavaScript">
<!--
function CheckAll()
{
for (var i=0;i<document.frmdeleteall.elements.length;i++)
{
var e = document.frmdeleteall.elements[i];
if (e.name != "allbox")
e.checked = document.frmdeleteall.allbox.checked;
}
}
//-->
</script>
</HEAD>
<BODY>
<form name="frmdeleteall" method="post" action="deleteall.php">
<table border=1 cellpadding=0 cellspacing=2 width="70%" align="center">
<?if($msg){?>
<tr>
<td colspan=5 align="center"><?=$msg?></td>
</tr>
<?}?>
<tr>
<td>S.No</td>
<td>Name</td>
<td>Address</td>
<td>City</td>
<td>All: <input name="allbox" type="checkbox" value="1" onClick="CheckAll();"></td>
</tr>
<?
$sql="select * from tbl_sample order by id limit 0,3";
$i=0;
$query=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>".$i++."</td>";
echo "<td>".$row['Name']."</td>";
echo "<td>".$row['Address']."</td>";
echo "<td>".$row['City']."</td>";
echo "<td><input type=\"checkbox\" name=\"del[]\" value=".$row['id']."></td>";
echo "</td>";
echo "</tr>";
}
?>
<tr>
<td colspan=5 align="center"><input type="submit" name="delete" value="Submit"></td>
</tr>
</table>
</BODY>
</HTML>
Newbie
 
Join Date: Dec 2007
Posts: 1
#5: Dec 18 '07

re: how to delete selected records from database


thx dude.... your script very helpful for me.
Familiar Sight
 
Join Date: Nov 2007
Posts: 244
#6: Sep 7 '08

re: how to delete selected records from database


Quote:

Originally Posted by arunj82

Hi,

Here i m giving a example for deleting all records and also u can delete a single record. U check it and if u have any doubt u pls message me. If you press all button in the headings, it will delete all records.


Sample Codings:

<?php
$con=mysql_connect("localhost","root","simply123") or die(mysql_error());
mysql_select_db("databasename",$con) or die(mysql_error());

// Give r connection details for database

if($_REQUEST['delete'])
{
$totalarray=sizeof($del);
for($i=0;$i<$totalarray;$i++)
{
if(!empty($del[$i])){
mysql_query("delete from tbl_sample where id=".$del[$i]) or die(mysql_error());
}
}
$msg="Values Deleted Successfully";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<script language="JavaScript">
<!--
function CheckAll()
{
for (var i=0;i<document.frmdeleteall.elements.length;i++)
{
var e = document.frmdeleteall.elements[i];
if (e.name != "allbox")
e.checked = document.frmdeleteall.allbox.checked;
}
}
//-->
</script>
</HEAD>
<BODY>
<form name="frmdeleteall" method="post" action="deleteall.php">
<table border=1 cellpadding=0 cellspacing=2 width="70%" align="center">
<?if($msg){?>
<tr>
<td colspan=5 align="center"><?=$msg?></td>
</tr>
<?}?>
<tr>
<td>S.No</td>
<td>Name</td>
<td>Address</td>
<td>City</td>
<td>All: <input name="allbox" type="checkbox" value="1" onClick="CheckAll();"></td>
</tr>
<?
$sql="select * from tbl_sample order by id limit 0,3";
$i=0;
$query=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>".$i++."</td>";
echo "<td>".$row['Name']."</td>";
echo "<td>".$row['Address']."</td>";
echo "<td>".$row['City']."</td>";
echo "<td><input type=\"checkbox\" name=\"del[]\" value=".$row['id']."></td>";
echo "</td>";
echo "</tr>";
}
?>
<tr>
<td colspan=5 align="center"><input type="submit" name="delete" value="Submit"></td>
</tr>
</table>
</BODY>
</HTML>


Hi

I am not getting this line

$totalarray=sizeof($del);

in the above code where you have assigned $del.

sorry if I asked something silly.

Thanks!
Familiar Sight
 
Join Date: Sep 2007
Posts: 211
#7: Sep 7 '08

re: how to delete selected records from database


Hi
I think I know what you mean by deleting multiple records at once by using checkboxes.
Actualy I'v done it in one of my websites.

in the form that user is able to check multiple records use this script

Expand|Select|Wrap|Line Numbers
  1. <?PHP 
  2.     $maximum = 10;  //10 is the number of results per page (could be any other number)
  3.     for ($i=1; $i <= $maximum; $i++) {
  4.         print "<input type=\"checkbox\" name=\"checkbox-$i\" value=\"$recordid[i]\" />";
  5.     }
  6. ?>
  7.  
As you can see $recordid is an array containing your record ids. and name of you checkboxes are ("checkbox-"+recordid)

Then this is what happens when user submits the form

Selected checkboxes return the value of the record ids that should be deleted

Now all you have to do is to control checkboxes to see if they have been checked or not
Like this:

Expand|Select|Wrap|Line Numbers
  1. <?PHP 
  2.     $maximum = 10;  //10 is the number of results per page (could be any other number)
  3.     for ($i=1; $i <= $maximum; $i++) {
  4.         if ($_REQUEST["checkbox-$i"]) {
  5.             $query = "DELETE FROM yourtable WHERE id='$_REQUEST[\"checkbox-$i\"]'";
  6.             mysql_query($query);
  7.         }
  8.     }
  9. ?>
  10.  
  11.  
Note that there are other options to make your quey variable. Like adding all of them in one query and do it at once instead of deleting them one by one


Hope this helps you
Familiar Sight
 
Join Date: Sep 2007
Posts: 211
#8: Sep 7 '08

re: how to delete selected records from database


Oh by the way
I forgot to say. The good thing about this kind of deleting form is that there is no client side script in it. It's all server side
Reply