Update multiple rows in database from form input

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
kwdrysdale
Forum Newbie
Posts: 3
Joined: Wed Mar 16, 2011 11:45 am

Update multiple rows in database from form input

Post by kwdrysdale »

Hi. I have successfully selected the data I want, put it into a table and have a text box available on each row for data to be entered. What I am wondering is how to associate the text box on each row to the ID of each row?

Here is the code for the table output:

Code: Select all

<?
// this section will display the data in a table allowing scores to be entered in the text boxes
echo "<table border='1'>
<tr>
<th width='200'>Contestant</th>
<th width='250'>School</th>
<th width='75'>Level</th>
<th width='25'>Score</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  $level = $row['level'];
  if ($level == "1") $showlevel="Junior";
  elseif ($level == "2") $showlevel="Senior";
  echo "<tr>";
  echo "<td width='200'>" . $row['contestant'] . "</td>";
  echo "<td width='250' align='center'>" . $row['school'] . "</td>";
  echo "<td width='75' align='center'>" . $showlevel . "</td>";
  echo "<td width='25'><input type='text'></td>";
  echo "</tr>";
  }
echo "</table>";
?>
Contestant School Level Score
Fred SRMCS JR 80
Mary SRMCS JR 75
Sam SRMCS JR 85

The user would see a table with everything already displayed (except the score) and a text box for them to enter the score for each contestant. Then, have a submit button on the bottom and put those marks into the database with the appropriate name. Hopefully I am getting the "problem" across properly and it is possible to do this. Thanks for any help you can give.

Kevin
Last edited by kwdrysdale on Wed Mar 16, 2011 12:38 pm, edited 1 time in total.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Update multiple rows in database from form input

Post by AbraCadaver »

Assuming you gran the name and ID, then use an array of marks and have the ID as the array key:

Code: Select all

echo $row['Name'] . '<input type="text" name="mark[' . $row['ID'] . ']">';
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
kwdrysdale
Forum Newbie
Posts: 3
Joined: Wed Mar 16, 2011 11:45 am

Re: Update multiple rows in database from form input

Post by kwdrysdale »

Thanks for the reply. I added some code to my original post and will make the changes you suggested to my code. I understand what is happening here, but then how would I use the array key (the ID) to update the database? I apologize if this is a really simple thing...I am still learning some of the basic things in PHP. :) I just can't figure out how to use the array key as the row id.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Update multiple rows in database from form input

Post by AbraCadaver »

You would loop through the array and use the key in the WHERE clause of the update:

Code: Select all

foreach($_POST['mark'] as $id => $mark) {
   //UPDATE table SET Mark=$mark WHERE ID=$id
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
kwdrysdale
Forum Newbie
Posts: 3
Joined: Wed Mar 16, 2011 11:45 am

Re: Update multiple rows in database from form input

Post by kwdrysdale »

THANKS!!! I'm not great with OOP (really a newbie to all of this) but I could make it all work out.
Post Reply