Page 1 of 1

Delete Data from Database Using Checkbox Rate Topic: -----

#1 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Posted 03 June 2009 - 10:44 PM

Delete Data from Database Using Checkbox


In this tutorial Iím going to show how to delete records from MySQL Database using Checkboxes.

for getting data from a Executed Query we are using mysql_fetch_assoc() or mysql_fetch_array() Different between these function is not to much but here I am using mysql_fetch_assoc() and Loop i am using a loop to show data from Arrays. Iím using While Loop

Ok letís start:
For Understanding and Example Iím Using a Message Table (This could be a part of a project
First create a database and name that chkboxes and on table name that tbl1 and columns:
Msgid, From, To, Subject, Message and insert some data

our main point in Message page or index page is our checkboxes so we are chaning the value and the name of checkboxes
 <input name="<?php echo $result['Msgid']?>" type="checkbox" id="<?php echo $result['Msgid']?>" value="<?php echo $result['Msgid']?>">

name="<?php echo $result['Msgid']?> through the name of check box we can delete the record, and we are assigning the Msgid to the name of Check box
Here is The Connection:
$con = mysql_connect('localhost','root','')or die(mysql_error());
$db = mysql_select_db('chkboxes',$con)or die(mysql_error());
$query = mysql_query("SELECT * FROM tbl1")or die(mysql_error());


and here is the code of getting data and showing data from database.table
 <?php  
while($result = mysql_fetch_assoc($query))
{
$i++;
?>
	  <tr>
		<td width="20%" align="center">
		<input name="<?php echo $result['Msgid']?>" type="checkbox" id="<?php echo $result['Msgid']?>" value="<?php echo $result['Msgid']?>">
		</label></td>
		<td width="20%" align="center"><?php echo $result['From']?></td>
		<td width="20%" align="center"><?php echo $result['To']?></td>
		<td width="20%" align="center"><?php echo $result['Subject']?></td>
		<td width="20%" align="center"><?php echo $result['Message']?></td>
	  </tr>
<?php
}
?>
	  <tr>
		<td align="center"><input type="submit" name="Submit" value="Submit" /></td>
		<td align="center"> </td>
		<td align="center"> </td>
		<td align="center"> </td>
		<td align="center"> </td>
	  </tr>
	</table>
  </form>


After it done create a getvalue.php page as on Action of form we typed the name of this page

$i this is variable and we are storing the number of deleted records in this
$r = mysql_fetch_assoc($query) we are storing all records from an executed query to an array as $r by using this function.

 while($r = mysql_fetch_assoc($query))
{
$msg = $r['Msgid']; // we are assigning id of message to this variable
}



 while($r = mysql_fetch_assoc($query))
{
$msg = $r['Msgid'];
if(isset($_POST[$msg])!=NULL) // we are using and if condition <-- if Posted Value or checked is Equal to Msgid of Table then 
{
mysql_query("delete from tbl1 Where Msgid=$msg")or die(mysql_error()); // if it is true then delete the record 
$i++; // if it equals then store the deleted record number.
}
}


all code
<?php
$con = mysql_connect('localhost','root','')or die(mysql_error());
$db = mysql_select_db('chkboxes',$con)or die(mysql_error());
$query = mysql_query("select * from tbl1");

$i=0;
while($r = mysql_fetch_assoc($query))
{
$msg = $r['Msgid'];
if(isset($_POST[$msg])!=NULL)
{
$i++;
mysql_query("delete from tbl1 Where Msgid=$msg")or die(mysql_error());
}
}
echo "$i Records Deleted";
?> 


thanks for reading.

Is This A Good Question/Topic? 3
  • +

Page 1 of 1