PHP simple : PHP tutorial, MySQL tutorial
PHP Tutorial - Free PHP Script

Home | Tutorials | PHP & MySQL Resources | Web Tools
Introduction & Get Start
Using Form
PHP
PHP Tags
PHP Variables
PHP Control Structures
MySQL
Create Database & Table
Database Connection
Insert Record
Select Record
Update Record
Delete Record

MySQL : Update Record

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'] <br/>";
echo "Name : $row['name'] <br/>";
echo "Email : $row['email'] <br/>";
echo "Tel : $row['tel'] <hr>";


// Add a link with a parameter(id) and it's value.
echo '<a href="update.php?id='.$row['id'].'">Update</a>';

}

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();
?>


<!-- END OF PHP CODES AND START HTML TAGS -->

<html>
<body>
<!-- set this form to POST method and target this form to itself ($PHP_SELF;)-->
<form id="form1" name="form1" method="post" action="<? echo $PHP_SELF; ?>">
<p>Name :
<!-- name of this text field is "name" -->
<input name="name" type="text" id="name" value="<? echo $row['name']; ?>"/>
<br />
Email :
<!-- name of this text field is "email" -->
<input name="email" type="text" id="email" value="<? echo $row['email']; ?>"/>
<br />
Tel :
<!-- name of this text field is "tel" -->
<input name="tel" type="text" id="tel" value="<? echo $row['tel']; ?>"/>
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
</body>
</html>

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.


Export MySQL to CSV (Excel)
You can export your MySQL database to .csv file format (Microsoft Excel file) easily.
Easy Counter with Images
Apply your basic counter with digit images. Short and easy codes. This counter plus by 1 every time you refresh your browser.


PHPsimple.net
info@phpsimple.net