Page 1 of 1

novice mySQL UPDATE of multiple records from form

Posted: Sun Oct 19, 2008 8:09 am
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);         
  }
  ?>

Re: novice mySQL UPDATE of multiple records from form

Posted: Sun Oct 19, 2008 9:42 am
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']) {

Re: novice mySQL UPDATE of multiple records from form

Posted: Sun Oct 19, 2008 7:43 pm
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.

Re: novice mySQL UPDATE of multiple records from form

Posted: Mon Oct 20, 2008 4:17 am
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.

Re: novice mySQL UPDATE of multiple records from form

Posted: Mon Oct 20, 2008 5:37 pm
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.

Re: novice mySQL UPDATE of multiple records from form

Posted: Tue Oct 21, 2008 3:24 am
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]
   }
}