hi I'm trying to find a way to create a dynamic form from mysql database and update multiple rows from it.

For example let's say I have a database with email and the owner's name. How do I build the script to make php grab the data from the db and create a form that display the informations with something to diffrentiate the row's fields? Like id_1, email_1 then next row id_2, email_2 then next row id3, email_3 etc.

And after that when I hit sumbmit how do I put them into arrays and update the multiple rows in the database?

Can anyone help me? Thanks in advance

Recommended Answers

All 5 Replies

I'd tend to have

<input type='text' name='email[34]' value='tom@aol.com'>

or even

<input type='text' name='row[34][email]' value='tom@aol.com'>

and then work through the array to update the data.

I named the fields as you suggested and everything went on beautifully up until the script pull out the data from the form to submit. I checked the data from the arrays created the id comes out ok but the other variables are all wrong it always gives me 3 for all the fields from the first row then value of 4 for the fields of all of the subsequent row's array and 8 for the last one. I don't know if I'm creating the arrays wrong or something else. My codes are below does anyone see any mistakes? Thanks! The script goes from default case to the first case then first case to the second one. This is a script to update stats for the all the players from one given basket ball team.

include '../includes/statsdatabase.php';
include '../includes/session.php';
   // Modify article form
   case "stats_form":

//table top	
echo	'<table width="500" border="0">';
echo  '<form method="post" action="pstats.php"><tr>';
echo    '<td width="250" class="style3">Player Name </td>';
echo    '<td width="50" class="style3">GP</td>';
echo    '<td width="50" class="style3">2M</td>';
echo    '<td width="50" class="style3">3M</td>';
echo    '<td width="50" class="style3">FM</td>';
echo    '<td width="50" class="style3">TF</td>';
echo  '</tr>';
//form loop
      $sql = mysql_query("SELECT * FROM player WHERE team='$team'") or die (mysql_error());
      while($row = mysql_fetch_array($sql)){
echo  '<tr>';
echo    '<td><input type="hidden" name="p_id['.$player_id.']" value="'.$player_id.'">'.$name.'</td>';
echo    '<td><div align="center">';
echo      '<input name="ngp['.$player_id.']" type="text" id="ngp['.$player_id.']" size="5">';
echo    '</div></td>';
echo    '<td><div align="center">';
echo      '<input name="n2m['.$player_id.']" type="text" id="n2m['.$player_id.']" size="5">';
echo    '</div></td>';
echo    '<td><div align="center">';
echo      '<input name="n3m['.$player_id.']" type="text" id="n3m['.$player_id.']" size="5">';
echo    '</div></td>';
echo    '<td><div align="center">';
echo      '<input name="nfm['.$player_id.']" type="text" id="nfm['.$player_id.']" size="5">';
echo    '</div></td>';
echo    '<td><div align="center">';
echo      '<input name="tfouls['.$player_id.']" type="text" id="tfouls['.$player_id.']" size="5">';
echo    '</div></td>';
echo  '</tr>';
echo  '<tr>';
echo    '<td colspan="6"><div align="center">';
echo      '<input type="hidden" name="req" value="update_stats">';
echo      '<input type="submit" name="Submit" value="Update Player Statistics">';
echo    '</div></td>';
echo  '</tr></form>';	
echo	'</table>';
   case "update_stats":
     //creating array & update loop
foreach($_POST['p_id'] as $id =>$stats){

$sql=mysql_query("SELECT player_id FROM pstats WHERE player_id='$id'");
echo '<p>number of rows: '.$num_results.'</p>';

if ($num_results == 1) {

$sql2=mysql_query("SELECT * FROM pstats WHERE player_id='$id'");


$update=mysql_query("UPDATE pstats SET 2m='$t2m', 3m='$t3m', fm='$tfm', tfouls='$ttf', gplayed='$tgp', tp='$tp', ppg='$ppg' WHERE player_id='$id'");
$updateppg=mysql_query("UPDATE player SET ppg='$ppg' WHERE player_id='$id'");

if(!$update) { die("Could not save the record! Player ID: $player_id "); }	
	} else {


$insert=mysql_query("INSERT INTO pstats(2m, 3m, fm, tfouls, gplayed, tp, ppg, player_id) VALUES('$n2m','$n3m','$nfm','$ntf','$ngp','$tp','$ppg','$id')");
$updateppg=mysql_query("UPDATE player SET ppg='$ppg' WHERE player_id='$id'");
if(!$update) { die("Could not save the record! Player ID: $player_id "); 

   // Default case: choose a team
//teams dropdown menu
$sql="SELECT * FROM teams";

$num_results = mysql_num_rows($result);


for ($i=0; $i<$num_results; $i++)
$options.="<OPTION VALUE=\"$id\">".$n."</option>";

echo '<form name="team" method="post" action="pstats.php">';
echo  '<select name="team" id="team"><option value=0>Choose a Team';
echo	$options;
echo  '</select>';
echo  '<input type="hidden" name="req" value="stats_form">';
echo  '<input type="submit" name="Submit" value="Go">';
echo '</form>';

Sorry my initial post was so short, got called away and pressed submit in the hope you'd get it - and you did! Well done.

The only problem is pulling the data out.


foreach($_POST['p_id'] as $id =>$stats){ 


foreach($_POST['p_id'] as $id){ 

Thanks a lot! I'll go try it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.