Forum Moderators: coopster

Message Too Old, No Replies

Multiple updates

         

mooger35

7:28 pm on Sep 22, 2005 (gmt 0)

10+ Year Member



I'm very new to most things beyond "beginner level" and am trying to make the updating of my site easier. I have a stats database and a players database that I am pulling this info from. What I would like is to do multiple updates from this one form. Here's the form:

<form name="updatestats" method="post" action="testupdate.php">
<table>
<tr>
<td><b>Name</b></td>
<td><b>GP</b></td>
<td><b>G</b></td>
<td><b>A</b></td>
<td><b>Pts</b></td>
</tr>
<?php do {?>
<tr>
<td><?php echo $row_stats['firstname'] . " " . $row_stats['lastname'];?>
<input type="hidden" name="id" value="<?php echo $row_stats['playerid'];?>">
</td>
<td><input name="gp" size="1" value="<?php echo $row_stats['gp'];?>"></td>
<td><input name="goals" size="1" value="<?php echo $row_stats['goals'];?>"></td>
<td><input name="assists" size="1" value="<?php echo $row_stats['assists'];?>"></td>
<td><input name="pts" size="1" value="<?php echo $row_stats['pts'];?>"></td>
</tr>
<?php } while ($row_stats = mysql_fetch_assoc($stats));?>
</table>
<input type="submit" value="Submit">
</form>

I would like some clarification on how to us the update function for my testupdate.php page. I can display all info in the above form but I can't update it.

mcibor

8:56 pm on Sep 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can achieve that in few ways. I think that the below is quite appropriate.:

<form name="updatestats" method="post" action="testupdate.php"><table><tr><td><b>Name</b></td><td><b>GP</b></td><td><b>G</b></td><td><b>A</b></td><td><b>Pts</b></td></tr>
<?php while ($row_stats = mysql_fetch_assoc($stats)) {?>
<tr><td><?php echo $row_stats['firstname'] . " " . $row_stats['lastname'];?>
<input type="hidden" name="id[]" value="<?php echo $row_stats['playerid'];?>">
</td><td><input name="gp[]" size="1" value="<?php echo $row_stats['gp'];?>"></td>
<td><input name="goals[]" size="1" value="<?php echo $row_stats['goals'];?>"></td>
<td><input name="assists[]" size="1" value="<?php echo $row_stats['assists'];?>"></td><td><input name="pts[]" size="1" value="<?php echo $row_stats['pts'];?>"></td></tr>
<?php } ;?>
</table><input type="submit" name="submit" value="Submit">
</form>

testupdate.php:
<?php
if(!isset($_POST['submit']) ¦¦ $_POST['submit']!= "Submit") die("Wrong place");
foreach($_POST['id'] as $key => $id) {
$sql = "UPDATE table SET gp='".$_POST['gp'][$key]."', goals='".$_POST['goals'][$key]."', assists='".$_POST['assists'][$key]."', pts='".$_POST['pts'][$key]."' WHERE id='$id' LIMIT 1";
if(mysql_query($sql)) $count++;
else die("Error in query:<br>$sql<br>");
}
echo "<p>$count rows updated.</p>";
?>

Hope this helps
Michal Cibor

mooger35

9:10 pm on Sep 22, 2005 (gmt 0)

10+ Year Member



Thanks for you help Michael

One more question and I'll test it... on the testupdate.php page I just need the connection line at the top?

example:
<?php require_once('Connections/connectDB.php');?>
<?php mysql_select_db($database_connectDB, $connectDB);?>

mooger35

9:33 pm on Sep 22, 2005 (gmt 0)

10+ Year Member



Thanks, works great!

mcibor

9:13 am on Sep 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad it worked out for you!
The connection is always needed.

Best regards
Michal Cibor

And Welcome to Webmasterworld!