473,378 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

how to delete selected records from database

254 100+
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
Nov 29 '07 #1
7 10049
arunj82
15
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.
Dec 1 '07 #2
mukeshrasm
254 100+
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..
Dec 3 '07 #3
arunj82
15
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>
Dec 3 '07 #4
kusang
1
thx dude.... your script very helpful for me.
Dec 18 '07 #5
mukeshrasm
254 100+
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!
Sep 7 '08 #6
bnashenas1984
258 100+
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
Sep 7 '08 #7
bnashenas1984
258 100+
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
Sep 7 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
6
by: Paul T. Rong | last post by:
Dear all, Here is my problem: There is a table "products" in my access database, since some of the products are out of date and stopped manufacture, I would like to delete those PRODUCTS from...
6
by: Bernd Smits | last post by:
Hi, I would like to delete a record (with commandbutton) of a table associated to a combobox, when I select a certain value in the combobox (the value I select is associated with the record that I...
8
by: John Baker | last post by:
Hi: Access 2000 W98! I have a table with numerous records in it, and am attempting to delete certain records that have been selected from it. These are selected based on the ID number in a...
4
by: Susan Bricker | last post by:
I have a command button on a form that is supposed to Delete the record being displayed. The record is displayed one to a form. The form is not a Pop-Up nor is it Modal. Tracing the btnDelete...
3
by: vcornjamb | last post by:
Hello, I am developing a web form that contains some buttons and a data grid which has as its last column link buttons that will delete the data associated with that row. Everything works fine,...
2
kcdoell
by: kcdoell | last post by:
Hello I have a code where I want to delete the records that are found in my DAO recordset. I took a stab at this for the first time and got it to work but it is only delete one record at a time. If...
6
by: scott.tang | last post by:
I'm experiencing a very strange problem. My application is MS Access front-end and MS SQL server back-end database. I have a SQL statement that deletes records from a table after an export...
0
by: Slickuser | last post by:
From my PHP page: Grab all data from the database. Go through a loop to generate the HTML. Client side: From the Color drop menu list, if a user change the value. It will grab that value &...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.