updating multiple rows with one form

Reply

Join Date: Aug 2005
Posts: 17
Reputation: StrikeFreedom is an unknown quantity at this point 
Solved Threads: 0
StrikeFreedom StrikeFreedom is offline Offline
Newbie Poster

updating multiple rows with one form

 
0
  #1
Nov 9th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 138
Reputation: sarahk is an unknown quantity at this point 
Solved Threads: 1
sarahk's Avatar
sarahk sarahk is offline Offline
Junior Poster

Re: updating multiple rows with one form

 
0
  #2
Nov 10th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 17
Reputation: StrikeFreedom is an unknown quantity at this point 
Solved Threads: 0
StrikeFreedom StrikeFreedom is offline Offline
Newbie Poster

updating multiple rows with one form

 
0
  #3
Nov 10th, 2005
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]
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 138
Reputation: sarahk is an unknown quantity at this point 
Solved Threads: 1
sarahk's Avatar
sarahk sarahk is offline Offline
Junior Poster

Re: updating multiple rows with one form

 
0
  #4
Nov 10th, 2005
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
foreach($_POST['p_id'] as $id =>$stats){ 
$ngp=$stats['ngp']; 
$n2m=$stats['n2m']; 
$n3m=$stats['n3m']; 
$nfm=$stats['nfm']; 
$ntf=$stats['tfouls'];
to
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];
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 17
Reputation: StrikeFreedom is an unknown quantity at this point 
Solved Threads: 0
StrikeFreedom StrikeFreedom is offline Offline
Newbie Poster

updating multiple rows with one form

 
0
  #5
Nov 10th, 2005
Thanks a lot! I'll go try it
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 44
Reputation: guru12 is an unknown quantity at this point 
Solved Threads: 6
guru12's Avatar
guru12 guru12 is offline Offline
Light Poster

Re: updating multiple rows with one form

 
0
  #6
Sep 16th, 2009
Hello Dom

please try this http://www.phpeasystep.com/mysql/10.html

I hope this should solve your problem

Thanks and Regards
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC