Thursday, August 22, 2013

How To Delete Data Into Database







1.Make a File delete.php with the Code


<html>

<head>
<title>Delete</title>
</head>
<body>
<?php 
$link=mysql_connect("localhost","root","") or die("Cannot Connect to the database!");

mysql_select_db("table",$link) or die ("Cannot select the database!");
$query="SELECT * FROM student";

$resource=mysql_query($query,$link);
echo "
<table align=\"center\" border=\"0\" width=\"70%\">
<tr>
<td><b>name</b></td> <td><b>class</b></td><td><b>rollno</b></td><td><b>Action</b></td></tr> ";
while($result=mysql_fetch_array($resource))
{
echo "<tr><td>".$result[1]."</td><td>".$result[2]."</td><td>".$result[3]."</td><td>
<a href=\"delete2.php?id=".$result[0]."\">Delete</a>
</td></tr>";
} echo "</table>";
?>
</body>
</html>

2.Create a File delete2.php with the code:

<html>
<head>
<title>Delete panel</title>
</head>
<body>
<?php
$id=$_REQUEST['id'];

$link=mysql_connect("localhost","root","") or die("Cannot Connect to the database!");

mysql_select_db("table",$link) or die ("Cannot select the database!");
$query="DELETE FROM student WHERE id='".$id."'";

if(!mysql_query($query,$link))
{die ("An unexpected error occured while <b>deleting</b> the record, Please try again!");}
else
{
echo "Record ".$id." removed successfully!";}
?>

</body>
</html>

3. Create Database in Server:

Database name: table
table name : student
column name:id,name,class,rollno






No comments: