Langkah untuk UPDATE data:
  1. masukkan update link pada viewlist.php
  2. buat satu form untuk user update data - updateform.php
  3. buat satu php file untuk proses update - updateprocess.php

 LANGKAH 1 - buka semula file vipewlist.php anda, masukkan hyperlink update pada column action spt berikut:

tambah code hyperlink spt di bawah

            <a href="viewuser.php?id= <?=$row['noic']?> "> View </a> &nbsp;
            <a href="updateform.php?id= <?=$row['noic']?> "> Update </a>

LANGKAH 2: updateform.php - buat satu form utk user kemaskini data. Just reuse the registerform.php design (INSERT Data) dengan menambah code utk retrieve data from database. Contoh interface spt berikut:

To retrieve data and display on the form, guna to display code spt dlm contoh PHP - view specific data.  The following codes show how to display data on the form:

    <td><div align="left"><strong>NAME</strong></div></td>
    <td><div align="left">
      <input name="name" type="text" id="name" size="20" maxlength="12"  value="<?=$row['name']?>" />

di dalam textbox, tambah attribute value="<?=$row['name']?>", digunakan utk memaparkan value di dalamnya.

Pass data ke page updateprocess.php iaitu:

<form method="post" action="updateform.php" name="uf">

Buat hidden id utk dihantar kepada updateform.php iaitu sebelum button submit:

<input type="hidden" name="id" value="<?=$row['noic']?>"

LANGKAH 3: updateprocess.php - digunakan untuk proses update. Di dalam file ini masukkan code utk update iaitu spt berikut:

//a)retrieve data form updateform.php then assign to variables

$noic = $_POST["noic"];
 $name = $_POST["name"];
 $age = $_POST["age"];
 $address = $_POST["address"];
 $program = $_POST["program"];
 $part = $_POST["part"];

//b) create connection to db

 $con = mysql_connect("localhost","root","");
 if (!$con)
   die('Could not connect: ' . mysql_error());

//c) select db

mysql_select_db("sampleIS", $con);

//d) execute update query - guna UPDATE query

 mysql_query("UPDATE user SET name = '$name', age = $age, address = '$address', program = '$program', part = '$part' WHERE noic = '$noic'");

//e) close the connection


//f) buat hyperlink atau redirect to page userlist.php:

<a href="userlist.php"> Back to View List </a>


header( 'Location: viewlist.php' )
Download code -  PHP UPDATE

No comments: