Sabtu, 31 Maret 2012

Delete Data In MySql With Php

continue the tutorial yesterday, Update Data in MySql With PHP. now we will try to edit data in MySql with php. The database and the table same as with yesterday. 

Create Vew Data

Make a new HTML document with your text editor and create the HTML form as follows:

<html>
<head>
<title>View Data</title>
</head>
<body>
<p align="center"><b>View Data</b></p>
<table align="center" cellpadding="0" cellspacing="25" border="0">
    <tr>
        <td>No</td>
        <td>Name</td>
        <td>Email</td>
        <td>Comment</td>
        <td colspan="2">Delete Data</td>
    </tr>
<?php
//create connection to the database
$conn=mysql_connect("localhost", "root", "root");
mysql_select_db("db_contact_us");
//create query select
$sql="select * from tb_contact_us";
//reading data
$hasil=mysql_query($sql);
//display data
$no=1;
while($row=mysql_fetch_array($hasil)){
    echo "<tr>";
    echo "<td>".$no."</td>";
    //display field name
    echo "<td>".$row['Name']."</td>";
    //display field email
    echo "<td>".$row['Email']."</td>";
    //Display field Comment
    echo "<td>".$row['Comment']."</td>";
    //Display Operation Update
    echo "<td><a href='delete_data.php?no=".$row['number']."'>Delete</a></td>";
    echo "</tr>";
    $no++;
}
?>
</table>
</body>
</html>


Save the HTML document with the name of the directory view_data.html and put it on your web server. Then see the results on your web browser. If you have done it correctly then it will be as follows:


Create Delete Data

<html>
<head>
<title>Delete Data</title>
</head>
<body>
<?php
//connect to the database
$conn=mysql_connect("localhost", "root", "");
mysql_select_db("db_contact_us");
//create a variable for which data is transmitted
$no = $_GET["no"];
//create query to storing data
$sql="DELETE FROM tb_contact_us WHERE number = '$no'";
//saving to the database
mysql_query($sql);
echo "<h2>Data has been Delete</h2>";
?>
<a href="view_data.php">view the data</a>
</body>
</html>
 

Save the HTML document with the name of the directory delete_data.phpand put it on your web server. Then see the results on your web browser. If you have done it correctly then it will be as follows: 


if you clik view the data then you will be as follows : 


 Good Luck :-)

Tidak ada komentar:

Posting Komentar