# Updating multiple records from one form

**URL:** <https://www.phphelp.com/t/updating-multiple-records-from-one-form/17310>\
**Category:** The Occasional Tutorial\
**Created:** [June 12, 2012, 11:22am UTC](https://www.phphelp.com/t/updating-multiple-records-from-one-form/17310 "2012-06-12T11:22:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ojoshiro](https://www.phphelp.com/user_avatar/www.phphelp.com/ojoshiro/32/274_2.png) [@Ojoshiro](https://www.phphelp.com/u/Ojoshiro)\
**Post date:** [June 12, 2012, 11:22am UTC](https://www.phphelp.com/t/updating-multiple-records-from-one-form/17310/1 "2012-06-12T11:22:01Z")

</div>

Hello me, it’s me again,

I have hung around on this board for a while now and I have seen some problems pop up over and over again. I’d like to try and tackle one here: the problem of updating many records (in a database in this example) from one HTML form.

Imagine we have this table in the database:

```auto
Table: User
Id
Username
Password
First_name
Last_name
Birthdate
Email
Subscr_date
```

Imagine you’re writing something for the administrator so that they can reset all passwords from one form.  
( you need a reason, right? )  
For every User-record in the database we want a line with the username, first\_name/last\_name, subscription date and a ‘Reset password’ checkbox.  
[php]  
$dbh = mysql\_connect( $host, $user, $pass ); // Connect to a database  
if ( mysql\_error() )  
{ echo "Cannot connect to database on [$host] as [$user] : ".mysql\_error();  
exit;  
}  
mysql\_select\_db( $database ); // Select the correct database

$query = “select id, username, first\_name, last\_name, subscr\_date from user”;

$result = mysql\_query( $query );  
if ( mysql\_error() )  
{ echo "Cannot run query [$query] : ".mysql\_error();  
exit;  
}  
// Create the form  
echo ‘’;  
echo ‘

’;  
echo ‘’;  
while ( $row = mysql\_fetch\_assoc( $result ) )  
{ echo ‘’;  
echo ‘’;  
echo ‘’;  
echo ‘’;  
echo ‘’;  
echo ‘’;  
}  
echo ‘

| Reset | Username | Name | Join date |
| ’.  
‘ | ’. $row[‘username’]. ‘ | ’. $row[‘first\_name’]. ’ '. $row[‘last\_name’]. ‘ | ’. $row[‘subscr\_date’]. ‘ |

’;  
echo ‘’;  
echo ‘’;  
[/php]  
As you can see the 'id’s are passed as an array.

That done you need to write the PHP script that is going to process this form. We named it ‘sendnewpass.php’.  
[php]  
$id\_array = $\_POST[‘id’]; // First get all the id’s  
foreach( $id\_array as $id ) // Loop through them  
{ if ( array\_key\_exists( $id.’\_reset’, $\_POST ) && $\_POST[$id.’\_reset’] == ‘on’ )  
{ resetPasswordAndSendMail( $id ); // This is a function that resets the password and sends an email  
} // to the user.  
}  
…

function resetPasswordAndSendMail( $id )  
{ $password = “CatchATurian”;  
$query = “update User set password=’$password’ where id=’$id’”;  
$result = mysql\_query( $query );  
if ( mysql\_error() )  
{ echo "Cannot run query [$query] : ".mysql\_error();  
exit;  
}  
// and here some code that digs up the emailadres and sends a notification with the new password.  
// ( I leave that to you )  
}  
[/php]  
In this example I only access the checkbox via the id, but I hope you see how you can keep lots of form-fields together this way.  
I hope this is going to help someone. If you need more explanation, contact me.

Good luck! ;D  
O.
