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,
    <?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());
    ?>
    
  2. Save with the name connect.php

Step 4 : Script to show the data

  1. Type the following script,
    <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> 
    
  2. Save with the name student_data.php

Step 5 : Create javascript to show warning

  1. Type the following script,
    function warning() {
    	return confirm('Are You Sure to Delete This Data?');
    }
    
  2. Save with the name formupdate.php

Step 6 : Create script to process delete

  1. Type the following script,
    <?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>";
    }
    ?>
    
  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 :D

For the explanation of the script just see the comment script or ask here… and feel free to comment :D
OK. Haaaaaaaapppy Coding! :D
Hal yang juga menarik:

Hak Cipta

Semua skrip dan teknik dalam artikel di itx.web.id boleh digunakan sebagaimana kehendakmu tanpa perlu mencantumkan sumber. Kamu tidak boleh mengkopi seluruh artikel, dalam Bahasa Indonesia ataupun diterjemahkan ke dalam bahasa lain.


Related Articles:

Artikel terkait:

: PHP

About the author

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 lo

4 Comments

  • yohan mengatakan:

    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?

  • yohan mengatakan:

    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??

Tinggalkan Balasan

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

x( X-( B-) ;-) :wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :D :?: :-| :-x :-o :-P :-D :-? :-)) :-) :-( :-& :) :( :!: 8-O 8-) 8) (Русский) (yahoo) (worship) (woot) (wave) (unsure) (tongue) (thinking) (tears) (taser) (smileydance) (sleeping) (sick) (scenic) (rofl) (rock) (party) (panic) (okok) (nottalking) (ninja) (music) (muscle) (muhaha) (money) (mmm) (lonely) (lol) (lmao) (idiot) (hungry) (highfive) (heart_beat) (heart) (headspin) (hassle) (haha) (gym) (griltongue) (goodluck) (girlkiss) (funkydance) (fish_hit) (eyeroll) (evilsmirk) (evil_grin) (drinking) (doh) (devil) (dance) (cry) (cozy) (coffee) (brokenheart) (bringit) (blush) (bigeyes) (beer) (banana_rock) (banana_ninja) (banana_cool) (applause) (annoyed) (angry) (K) (: