Kategori
PHP

Delete Mysql Data Function with PHP

Case Study : Create function to delete data on mysql database.
Requirements : Webserver Packages, already installed.

Delete function will be done one by one, so, here it is.

Step 1 : Prepare the Database

  1. Create database named db_tutorial
  2. Prepare a table named tb_kota, with structure like picture below,
  3. Insert some sample data like below,
  4. Done!

Step 2 : Prepare the work directory

  1. Create folder named tutorphp in your document root
  2. Save all file in this tutorial in this folder.

Step 3 : Create a connection to DB script

  1. Type the following script,
    [sourcecode language=”php”]
    <?php
    $host = "localhost";
    $user = "root";//adjust according to your mysql setting
    $pass = ""; //adjust according to your mysql setting, i use no password here
    $dbName = "db_tutorial";
    mysql_connect($host, $user, $pass);
    mysql_select_db($dbName)
    or die ("Connect Failed !! : ".mysql_error());
    ?>
    [/sourcecode]
  2. Save with the name connect.php

Step 4 : Script to show the data

  1. Type the following script,
    [sourcecode language=”php”]
    <html><head>
    <title>Itx.Web.Id</title>
    <script type="text/javascript" src="warning.js"></script>
    </head>
    <body>
    <h2> Student’s Data <h2>
    <?php
    include ‘connect.php’;
    $query = "SELECT * FROM tb_student"; //the query for get all data in tb_student
    $result = mysql_query($query);

    echo "<table border=’0′ cellpadding=’4′ cellspacing=’4′>";
    echo "<tr bgcolor=’orange’ align=’center’>
    <td> <b> No </b> </td>
    <td> <b> Name </b> </td>
    <td> <b> Address </b> </td>
    <td> <b> Action </b> </td>
    </tr>";
    while ($data = mysql_fetch_array($result)) //mysql_fetch_array = get the query data into array
    {
    echo "<tr align=’center’>
    <td>".$data[‘no’]."</td>
    <td>".$data[‘name’]."</td>
    <td>".$data[‘address’]."</td> "; ?>
    <td> <i> <a href="prosesdelete.php?no=<?php echo $data[‘no’];?>" onClick="return warning();"> Del </a> </i></td>
    <?php
    }
    echo "</table>";
    ?>
    </body>

    </html>
    [/sourcecode]

  2. Save with the name student_data.php

Step 5 : Create javascript to show warning

  1. Type the following script,
    [sourcecode language=”javascript”]
    function warning() {
    return confirm(‘Are You Sure to Delete This Data?’);
    }
    [/sourcecode]
  2. Save with the name formupdate.php

Step 6 : Create script to process delete

  1. Type the following script,
    [sourcecode language=”php”]
    <?php
    include "connect.php";
    //get the value from form update
    $no = $_GET[‘no’]; //get the no which will deleted

    //query for update data in database
    $query = "DELETE from tb_student WHERE no = ‘$no’" ;
    $hasil = mysql_query($query);
    //see the result
    if ($hasil) {
    include "data.php";
    echo "<h4> delete data success </h4>";
    }
    ?>
    [/sourcecode]

  2. Save with the name prosesdelete.php

Step 7 : Testing Code

  1. Go to http://localhost/tutorphp/date.php. Click delete in one of the data showed.
  2. When confirmation window appear, choose OK.
  3. Thus the success conformation appear, and the data has been deleted. Check your database, too. Make sure that the data really deleted 😀

For the explanation of the script just see the comment script or ask here… and feel free to comment 😀
OK. Haaaaaaaapppy Coding! 😀

Oleh littleflow3r

obviously, a girl. with sweet smile, off course. turning her 21 years trapped on Informatics departement and just started to having some crush with it lately. she uses wordpress. she loves php. she loves to satisfied her shopping hobbies just via looking around in online shop. oh yeah, she got an A for her english subject^^

4 replies on “Delete Mysql Data Function with PHP”

function warning() {
return confirm(‘Are You Sure to Delete This Data?’);
}

*itu kan formupdate.php
tapi tidak pas di klik link nya tidak keluar informasi seperti itu!! gmna dong??

ada yg bingung nih pas
<?php
include "connect.php";
//get the value from form update
$no = $_GET['no']; //get the no which will deleted

//query for update data in database
$query = "DELETE from tb_student WHERE no = '$no'" ;
$hasil = mysql_query($query);
//see the result
if ($hasil) {
include "data.php";
echo " delete data success “;
}
?>

* include “data.php”;
nah tp di contoh diatas ga ada data.php?? maka nya pas buka prosesdelete.php ga bisa mulu nih???
gmna dong?

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

This site uses Akismet to reduce spam. Learn how your comment data is processed.