Rabu, 07 Januari 2009

Cara Update Record pada PHP

The update statement is used to change the value in a table.

Syntax :

update table_name set column_name1='value1', column_name2='value2', ... where column_name(key)='value';

This tutorial require 2 files.

1. select.php comes from Select Record tutorial. You will make some modify in this file.
2. update.php


• select.php

This file comes from Select Record tutorial. In this tutorial, you have to modify something by make a link including with a parameter and target to update.php (GET Method)
select.php
// Connect database
include("connectdb.php");

// Get all records in all columns from table and put it in $result.
$result=mysql_query("select * from phonebook");

/*Split records in $result by table rows and put them in $row.
Make it looping by while statement. */
while($row=mysql_fetch_assoc($result)){

// Output
echo "ID : $row['id']
";
echo "Name : $row['name']
";
echo "Email : $row['email']
";
echo "Tel : $row['tel']
";

// Add a link with a parameter(id) and it's value.
echo 'Update';

}

mysql_close();
?>

Browse "select.php" by go to http://localhost/select.php.

Result : (Links below are NOT real on this page)

ID : 1
Name : Jack
Email : jack@abcd.com
Tel : 12345678
Update <- when you mouse over this. It will show "http://localhost/update.php?id=1" in your browser's status bar.
ID : 2
Name : Joe
Email : joe@abcd.com
Tel : 87654321
Update<- when you mouse over this. It will show "http://localhost/update.php?id=2" in your browser's status bar.

...

The values are the same as ID parameters. When you click on these links, the page will change to update.php.
• update.php

This file will show you how you make a change in your database. Show data record in form fields and update it in only this file.
update.php
* Layout : All text fields in this file will show you with vulues immediately when you click "update" link from select.php. Below; This is an example form when I click "update" link from first record (ID=1).

Name :
Email :
Tel :

* Tags in this file.

// START PHP CODES. THIS PART MUST ON THE TOP OF THIS PAGE.

// Connect database.
include("connectdb.php");

// ***** This part will process when you Click on "Submit" button *****
// Check, if you clicked "Submit" button
if($_POST['Submit']){

// Get parameters from form.
$id=$_POST['id'];
$name=$_POST['name'];
$email=$_POST['email'];
$tel=$_POST['tel'];

// Do update statement.
mysql_query("update phonebook set name='$name', email='$email', tel='$tel' where id='$id'");

// Re-direct this page to select.php.
header("location:select.php");
exit;
}
// ************* End update part *************

// *** Select data to show on text fields in form. ***

// Get id parameter (GET method) from select.php
$id=$_GET['id'];

// Get records in all columns from table where column id equal in $id and put it in $result.
$result=mysql_query("select * from phonebook where id='$id'");

// Split records in $result by table rows and put them in $row.
$row=mysql_fetch_assoc($result);

// Close database connection.
mysql_close();
?>







Name :




Email :




Tel :










Test
Browse select.php on your web browser as http://localhost/select.php and click on "Update" link. When URL has change to http://localhost/update.php, change some data that show on text fields and then click on "Submit" button, this page will re-load and go back to http://localhost/select.php.

Tidak ada komentar:

Poskan Komentar