novice mySQL UPDATE of multiple records from form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

novice mySQL UPDATE of multiple records from form

Post by rhecker »

To simplify, I have stripped my code example down to the essential issue. I have a form that is populated with a list of records from a database. An example would be a list of products and their assiciated prices. There is a auto_increment primary key. With all the product items on one screen, I want to change prices of some, then reupload the whole lot back to the database. I can populate my form just fine, but I can't get the edited data back into the database. I've tried to set up a foreach loop, but no changes were reflected. Any help appreciated.

Code: Select all

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data"> 
 <?php
 include("php_includes/localhost.php");
$result = mysql_query ("SELECT test1, id from test") or die ('Error: '.mysql_error ());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<p>
TEST1: <input name="test1" type="text" value="<?php echo ($row["test1"])?>"/>
<input name="id" type="text" value="<?php echo ($row["id"])?>"/>
<?php
;}
?>
 <input type="hidden" name="submitted" value="1" />
<input type="submit" value="Submit" />                                                         
</form>
 
<?php if ($_POST['submitted']) {
 
$test1 = $_POST['test1'];
$id = $_POST['id'];
 
 $sql = "UPDATE test SET
          test1='$test1',
          WHERE id='$id'";
$ok = @mysql_query($sql);         
  }
  ?>
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

Re: novice mySQL UPDATE of multiple records from form

Post by aditya2071990 »

I have made a small change to how your form is submitted on lines 13 and 15. Instead of using a hidden field, why not use the name of the submit button itself for the submit action? Maybe your stuff is not being submitted at all?

Code: Select all

<input type="submit" value="Submit" name="submit" />                                                         
</form>
 
<?php if ($_POST['submit']) {
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

Re: novice mySQL UPDATE of multiple records from form

Post by rhecker »

Thank you for the suggestion. But that isn't the issue. This method of submission does work. The question is how to update multiple records.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: novice mySQL UPDATE of multiple records from form

Post by aceconcepts »

In order to update multiple records, you'll need to pass you fields as arrays:

Code: Select all

 
<!--Notice the square brackets after name="id[]" - this defines this element as an array, when it is submitted.-->
<input name="id[]" type="text" value="<?php echo ($row["id"])?>
"/>
 
When you've done this to all of you form elements, you'll then need to use a for loop to process the data when the form has been submitted.
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

Re: novice mySQL UPDATE of multiple records from form

Post by rhecker »

Thanks, I see how you have modified the way the fields are populated. How to structure the for or foreach loop to send the data to the database has me stumped.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: novice mySQL UPDATE of multiple records from form

Post by aceconcepts »

The for loop will provide a unique identifier for each field submitted:

Code: Select all

 
if(isset($_POST['submit']))
{
   for($x=0; $x<count($_POST['id']); $x++)
   {
      //Reference each field by using the array index: $_POST['id'][$x]
   }
}
 
Post Reply