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.
form.php, this file shows data records in an update form.
update.php , this file is the updater.
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
<?
$host="localhost";
$db_user="";
$db_password="";
$database="tutorial";
mysql_connect($host,$db_user,$db_password);
mysql_select_db($database);
$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>
<?
while($row=mysql_fetch_assoc($result)){
?>
<tr>
<td bgcolor="#FFFFCC"><? echo $row['id']; ?> </td>
<td bgcolor="#FFFFCC"><input name="name_<? echo $row['id']; ?> " type="text" id="name_<? echo $row['id']; ?>" value="<? echo $row['name']; ?> " /></td>
</tr>
<? } ?>
</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']){
$host="localhost";
$db_user="";
$db_password="";
$database="tutorial";
mysql_connect($host,$db_user,$db_password);
mysql_select_db($database);
$result=mysql_query("select id from name_list order by id asc");
while($row=mysql_fetch_assoc($result)){
$name=$_POST["name_".$row[id]];
mysql_query("update name_list set name='$name' where id='$row[id]'");
}
echo "--- Update Complete ---";
}
?>