Jump to content

[SOLVED] Updating multiple forms at once


Brandon Jaeger

Recommended Posts

I want to generate the same form inputs for everyone in the database and have one Submit button and have it update them all at once. Am I on the right track?

 

$query = "SELECT * FROM table";
$result = mysql_query($query) or die(mysql_error));

echo "<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"POST\">\n";

$something_array1 = array();
$something_array2 = array();
$something_array3 = array();
$ids = array();

while($row = mysql_fetch_array($result))
{
  $something1 = $row['something1'];
  $something2 = $row['something2'];
  $something3 = $row['something3'];
  $id = $row['id'];

  echo "<input type=\"text\" name=\"something_array_1[]\" value=" . $something1 . "><br \>\n";
  echo "<input type=\"text\" name=\"something_array_2[]\" value=" . $something2 . "><br \>\n";
  echo "<input type=\"text\" name=\"something_array_3[]\" value=" . $something3 . "><br \>\n";
  echo "<input type=\"hidden\" name=\"ids[]\" value=\"" . $id . "\">\n";
}

echo "<input type=\"submit\" name=\"Update\" value=\"Update\">\n";
echo "</form>\n";

if(isset($_POST['Update']))
{
  $something_array1 = $_POST['something_array_1'];
  $something_array2 = $_POST['something_array_2'];
  $something_array3 = $_POST['something_array_3'];

  foreach($something_array1 as $val)
  {
    $result = mysql_query("UPDATE table SET column1 = " . $val . " WHERE id = " . $id);
  }

  foreach($something_array2 as $val)
  {
    $result = mysql_query("UPDATE table SET column2 = " . $val . " WHERE id = " . $id);
  }

  foreach($something_array3 as $val)
  {
    $result = mysql_query("UPDATE table SET column3 = " . $val . " WHERE id = " . $id);
  }
}

 

That's totally off the top of my head but I'm not sure if I'm heading the right way. If someone could either verify my method or correct me, I would greatly appreciate it!

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/123175-solved-updating-multiple-forms-at-once/
Share on other sites

Almost correct, but you need to somehow get the ID into the foreach loops.  I'd suggest echoing the ID as the array key (in the form name), and just adjusting the foreach accordingly.

 

P.S: It's fine to PM me, that's why I have it in my sig.  Don't worry about it. =)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.