Thursday 30 August 2012

Delete Record Using php Mysql with Javascript Confirm Box



*************************************************************************
Method 1 to Delete the Record
*************************************************************************



Display.php

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script language="javascript">

function del_rec(id)
{

    if(confirm("Do you wnat to delete this record...?"))
    {

        window.location="display.php?did="+id;
    }
}
</script>
</head>

<body>
<?php

$con=mysql_connect("localhost","root","");
mysql_select_db("test");

if(isset($_GET['did']))
{
mysql_query("delete from stud where id=".$_GET['did']."");
   
}



$sql="select * from stud";
$nsql=mysql_query($sql);
?>
<table border="1">
<tr>
    <td><b>Id</b></td>
    <td><b>Name</b></td>
    <td><b>Delete</b></td>
</tr>
<?php
while($row=mysql_fetch_array($nsql))
{
?>
<tr>
    <td><?php echo $row[0]; ?></td>
    <td><?php echo $row[1]; ?></td>
    <td><a href="#" onclick="del_rec(<?php echo $row[0]; ?>)">Delete</td>
</tr>

<?php
   
}

?>

</table>
</body>
</html>


Database










*********************************************************************
 Method 2 to Delete the Record
*********************************************************************








 ***************************************************************************
delete.php

*******************************************************************************

<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("test");
$sql=mysql_query("select * from stud");

if(isset($_REQUEST[did]))
{
    mysql_query("delete from stud where id=".$_REQUEST[did]);
    ?>
    <script language="javascript">
    window.location="delete.php";
    </script>
    <?php
}

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>

<table width="200" border="1">
<?php
while($row=mysql_fetch_array($sql))
{
?>


  <tr>
    <td rowspan="3">
    <img src="<?php echo "img/".$row["image"]; ?>" height="100px" width="180px" /></td>
    <td><?php echo $row["id"]; ?></td>
    <td rowspan="3"><a href="delete.php?did=<?php echo $row["id"]; ?>" onclick="return confirm('are sure to delete!')">Delete</a></td>
  </tr>
  <tr>
    <td><?php echo $row["name"]; ?></td>
  </tr>
  <tr>
    <td><?php echo $row["pincode"]; ?></td>
  </tr>
  <?php } ?>
</table>

</body>
</html>

 ****************************************************************************

Database
*************************************************************************



Share This
Previous Post
Next Post

1 comment:

  1. nice post...

    see below...

    http://cleartuts.blogspot.in/2014/12/delete-data-from-mysql-with-confirmation.html

    ReplyDelete