Case Study : Create update function to update student data in database
Requirements : Webserver Packages, already installed.

Ok, Here we go.

Step 1 : Prepare Database

  1. Create database named db_tutorial
  2. Prepare table named tb_student, with structure as 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,
    <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='2' cellspacing='2'>";
    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='formupdate.php?no=".$data['no']."'> Update </a> </i></td>
           </tr>";
    }
    echo "</table>";
    ?>
    
  2. Save with the name data.php

Step 5 : Create script to show update form

  1. Type the following script,
    <?php
    include 'connect.php';
    $no = $_GET['no']; //get the no which will updated
    $query = "SELECT * FROM tb_student WHERE no = $no"; //get the data that will be updated
    $hasil = mysql_query($query);
    $data  = mysql_fetch_array($hasil);
    ?>
    <form method="post" action="prosesupdate.php">
    <table width="500" border="0" cellpadding="2" cellspacing="2">
      <tr bgcolor="orange">
    	<td height="21" colspan="3">
         <strong> Form Update</strong>
        </td>
    	
      <tr>
      	<td width="23%"> No </td>
        <td width="2%">:</td>
        <td width="75%"> <input type="text" name="no" disabled='disable' value="<?php echo $data['no']; ?>"> </td>
      </tr>
    
      <tr>
      	<td>Name</td>
        <td>:</td>
        <td> <input type="text" name="name" value="<?php echo $data['name']; ?>"> </td>
      </tr>
    
      <tr>
      	<td>Address</td>
        <td>:</td>
        <td> <textarea name="address"><?php echo $data['address']; ?></textarea></td>
      </tr>
    <tr>
      	<td></td> <td></td> <td> <input type="hidden" name="no" value="<?php echo $data['no']; ?>">
      	<input type="SUBMIT" name="SUBMIT" value="Save"></td>
      </tr>
    </table>
    </form>
    
  2. Save with the name formupdate.php

Step 6 : Script to update data

  1. Type the following script,
    <?php
    include "connect.php";
    //get the value from form update
     $no = $_POST['no'];
     $name = $_POST['name'];
     $address = $_POST['address'];
     
    //query for update data in database
     $query = "UPDATE tb_student SET name = '$name', address = '$address' WHERE no = '$no'" ;
     $hasil = mysql_query($query);
     //see the result
     if ($hasil) {
        include "data.php";
    	echo "<h4> update data success </h4>";
    }
    ?>
    
  2. Save with the name prosesupdate.php

Step 7 : Testing Code

  1. Go to http://localhost/tutorphp/date.php. Click update in one of the data.
  2. You will see the form filled with the data. See that all date changeable, except field ‘no’ is inactive (because it is an id). Fill with new data, and then save.
  3. You will see the data has been updated :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

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) (: