School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,314 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,058 people online right now. Registration is fast and FREE... Join Now!




Delete Data from Database Using Checkbox

 
Reply to this topicStart new topic

> Delete Data from Database Using Checkbox

noorahmad
Group Icon



post 3 Jun, 2009 - 09:44 PM
Post #1


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
CODE
<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:
CODE

$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
CODE
<?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.

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


CODE
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
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.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/7/09 02:21PM

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