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

Home | Tutorials | Php vs Node.Js | Web Tools

Update Mutiple Records in 1 Form


Tutorials > Update Mutiple Records in 1 Form

Update your multiple data records in 1 submit form. When you have a list and need to update them in one click.

This tutorial require 2 files and 1 table of mySQL database.

  1. form.php, this file shows data records in an update form.
  2. update.php, this file is the updater.
  3. Database "tutorial" and table "name_list" with 2 fields: id(auto_increment), name(varchar, 50) and put some records about 20 - 30 records into this table. (directly by phpMyAdmin)

Learn how to create MySQL database. Click here.


form.php

This file including with a form contains with a table of data records listing in text fields. Set the form action to update.php with POST method.

All text fields should be generated with ID records at the end of their names. Sush as name_1, name_2, name_3, ....

Source Code

<?
// Connect database.
$host="localhost"; // Host name.
$db_user=""; // MySQL username.
$db_password=""; // MySQL password.
$database="tutorial"; // Database name.
mysql_connect($host,$db_user,$db_password);
mysql_select_db($database);

// Select all data records in table "name_list" and put them into $result.
$result=mysql_query("select * from name_list order by id asc");
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="update.php">
<table border="1" cellpadding="3" cellspacing="0">
<tr>
<td align="center" bgcolor="#FFCC00"><strong>ID</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>Name</strong></td>
</tr>
<?
// Fetch record rows in $result by while loop and put them into $row.
while($row=mysql_fetch_assoc($result)){
?>

<tr>
<td bgcolor="#FFFFCC"><? echo $row['id']; // Show record's ID ?></td>
<td bgcolor="#FFFFCC"><input name="name_<? echo $row['id']; ?>" type="text" id="name_<? echo $row['id']; ?>" value="<? echo $row['name']; ?>" /></td>
</tr>
<? } // End while loop. ?>
</table>
<input type="submit" name="Submit" value="Update" />
</form>
</body>
</html>

 


update.php

This file is the updater and re-direct to form.php when it finished.

Source Code

<?
if($_POST['Submit']){ // If receive Submit button variable.

// Connect database.
$host="localhost"; // Host name.
$db_user=""; // MySQL username.
$db_password=""; // MySQL password.
$database="tutorial"; // Database name.
mysql_connect($host,$db_user,$db_password);
mysql_select_db($database);

// Select all data records in table "name_list" and put them into $result.
$result=mysql_query("select id from name_list order by id asc");

// Fetch record rows in $result by while loop and put them into $row.
while($row=mysql_fetch_assoc($result)){
// Get the posted value "name_ID value" from form.php. This variable change it's value by while loop.
$name=$_POST["name_".$row[id]];

// Update field "name", matching with "id" value by while loop.
mysql_query("update name_list set name='$name' where id='$row[id]'");
}
echo "--- Update Complete ---";
}
?>

 

Advertise
Put Ebay RSS Feeds onto your website
SEO Elite Software
Domain Dashboard CPanel & Seo Manager
Work at Home Ideas and Opportunities
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

PHPsimple.net
info@phpsimple.net