Kategori
PHP

Create UPDATE Database Function with PHP

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,
    [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”]
    <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>";
    ?>
    [/sourcecode]

  2. Save with the name data.php

Step 5 : Create script to show update form

  1. Type the following script,
    [sourcecode language=”php”]
    <?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>
    [/sourcecode]

  2. Save with the name formupdate.php

Step 6 : Script to update data

  1. Type the following script,
    [sourcecode language=”php”]
    <?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>";
    }
    ?>
    [/sourcecode]

  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 😀

OK. Haaaaaaaapppy Coding! 😀