School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,158 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,067 people online right now. Registration is fast and FREE... Join Now!



Delete Data from Database Using Checkbox

Page 1 of 1

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

#1 noorahmad  Icon User is offline

  • Author
  • Icon
  • View blog
  • Group: Author w/DIC++
  • Posts: 2,198
  • Joined: 12-March 09


Dream Kudos: 1600

Posted 03 June 2009 - 09: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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month