| | |
updating multiple rows with one form
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Aug 2005
Posts: 17
Reputation:
Solved Threads: 0
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
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
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.
<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.
•
•
Join Date: Aug 2005
Posts: 17
Reputation:
Solved Threads: 0
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.
[php]include '../includes/statsdatabase.php';
include '../includes/session.php';
login_check();
admin_check();
switch($_REQUEST['req']){
// Modify article form
case "stats_form":
$team=$_POST['team'];
//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)){
$player_id=$row['player_id'];
$name=$row['name'];
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>';
break;
case "update_stats":
//creating array & update loop
foreach($_POST['p_id'] as $id =>$stats){
$ngp=$stats['ngp'];
$n2m=$stats['n2m'];
$n3m=$stats['n3m'];
$nfm=$stats['nfm'];
$ntf=$stats['tfouls'];
$sql=mysql_query("SELECT player_id FROM pstats WHERE player_id='$id'");
$num_results=mysql_num_rows($sql);
echo '<p>number of rows: '.$num_results.'</p>';
if ($num_results == 1) {
$sql2=mysql_query("SELECT * FROM pstats WHERE player_id='$id'");
$row=mysql_fetch_array($sql2);
$o2m=$row['2m'];
$o3m=$row['3m'];
$ofm=$row['fm'];
$otf=$row['tfouls'];
$ogp=$row['gplayed'];
$tgp=$ogp+$ngp;
$t2m=$o2m+$n2m;
$t3m=$o3m+$n3m;
$tfm=$ofm+$nfm;
$ttf=$otf+$ntf;
$tp=$t2m*2+$t3m*3+$tfm;
$ppg=$tp/$tgp;
$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 {
$tp=$n2m*2+$n3m*3+$nfm;
$ppg=$tp/$ngp;
$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 ");
}
}
}
break;
// Default case: choose a team
default:
//teams dropdown menu
$sql="SELECT * FROM teams";
$result=mysql_query($sql);
$num_results = mysql_num_rows($result);
$options="";
for ($i=0; $i<$num_results; $i++)
{
$row=mysql_fetch_array($result);
$id=$row["id"];
$n=$row["tname"];
$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>';
break;
break;
}
[/php]
[php]include '../includes/statsdatabase.php';
include '../includes/session.php';
login_check();
admin_check();
switch($_REQUEST['req']){
// Modify article form
case "stats_form":
$team=$_POST['team'];
//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)){
$player_id=$row['player_id'];
$name=$row['name'];
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>';
break;
case "update_stats":
//creating array & update loop
foreach($_POST['p_id'] as $id =>$stats){
$ngp=$stats['ngp'];
$n2m=$stats['n2m'];
$n3m=$stats['n3m'];
$nfm=$stats['nfm'];
$ntf=$stats['tfouls'];
$sql=mysql_query("SELECT player_id FROM pstats WHERE player_id='$id'");
$num_results=mysql_num_rows($sql);
echo '<p>number of rows: '.$num_results.'</p>';
if ($num_results == 1) {
$sql2=mysql_query("SELECT * FROM pstats WHERE player_id='$id'");
$row=mysql_fetch_array($sql2);
$o2m=$row['2m'];
$o3m=$row['3m'];
$ofm=$row['fm'];
$otf=$row['tfouls'];
$ogp=$row['gplayed'];
$tgp=$ogp+$ngp;
$t2m=$o2m+$n2m;
$t3m=$o3m+$n3m;
$tfm=$ofm+$nfm;
$ttf=$otf+$ntf;
$tp=$t2m*2+$t3m*3+$tfm;
$ppg=$tp/$tgp;
$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 {
$tp=$n2m*2+$n3m*3+$nfm;
$ppg=$tp/$ngp;
$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 ");
}
}
}
break;
// Default case: choose a team
default:
//teams dropdown menu
$sql="SELECT * FROM teams";
$result=mysql_query($sql);
$num_results = mysql_num_rows($result);
$options="";
for ($i=0; $i<$num_results; $i++)
{
$row=mysql_fetch_array($result);
$id=$row["id"];
$n=$row["tname"];
$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>';
break;
break;
}
[/php]
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.
change to
The only problem is pulling the data out.
change
foreach($_POST['p_id'] as $id =>$stats){ $ngp=$stats['ngp']; $n2m=$stats['n2m']; $n3m=$stats['n3m']; $nfm=$stats['nfm']; $ntf=$stats['tfouls'];
foreach($_POST['p_id'] as $id){ $ngp=$_POST['ngp'][$id]; $n2m=$_POST['n2m'][$id]; $n3m=$_POST['n3m'][$id]; $nfm=$_POST['nfm'][$id]; $ntf=$_POST['tfouls'][$id];
Hello Dom
please try this http://www.phpeasystep.com/mysql/10.html
I hope this should solve your problem
Thanks and Regards
please try this http://www.phpeasystep.com/mysql/10.html
I hope this should solve your problem
Thanks and Regards
![]() |
Similar Threads
- insert multiple rows in database (MS SQL)
- Store multiple selection from pagination info into single array (PHP)
- Storing dynamic form values in Arrays for display & insert (PHP)
- multiple recipients on FP form submission (HTML and CSS)
Other Threads in the PHP Forum
- Previous Thread: convert database into XML
- Next Thread: Itterating - "a" through "zzzz"
| Thread Tools | Search this Thread |
apache api array basic beginner binary body broken cache cakephp class cms code computing confirm cron curl customizableitems database date date/time delete display dynamic echo email error file filter folder form forms forum function functions gc_maxlifetime google headmethod howtowriteathesis href htaccess html iframe image include ip javascript joomla limit link list login malfunction memmory memory menu mlm msqli_multi_query multiple mycodeisbad mysql navigation oop parameter parsing paypal pdf php phpmysql query question random recourse recursion regex script search select seo server sessions snippet source space sql static system table thesishelp trouble tutorial update upload url variable video web webdesign xml youtube





