...

View Full Version : Help with Update script



stfc_boy
07-12-2007, 09:25 PM
Hi Guys,

Iím creating an update script, but am having one problem updating it, which is causing me a headache.

Hereís my database with five rows:

http://www.sloughtownfc.net/statistics.jpg

But when I try to update each row in the database via my php script below, it saves each record as the last entry entered (ie duplicates them):

Can anyone help?

Thanks

Chris



<?php

// Get the data from the league table
$databaseInfo = mysql_query("SELECT * From player_stats") or die(mysql_error());

// Update the new values in the database
if(isset($_POST['Submit']))
{

for($i = 0; $i < count($_REQUEST['nme']) ; $i++)
{
$result = mysql_query("Update player_stats set name ='".$_REQUEST['nme'][$i]."' WHERE match_id=".$_REQUEST['id'][$i]) OR DIE(mysql_error());
}

for($i = 0; $i < count($_REQUEST['gls']) ; $i++)
{
$result = mysql_query("Update player_stats set goals ='".$_REQUEST['gls'][$i]."' WHERE match_id=".$_REQUEST['id'][$i]) OR DIE(mysql_error());
header("Location: ../index.php");
}

} //end of post

?>
<form name="table" method="post" action="">
<table width="100%" cellpadding="1" cellspacing="1">
<tr>
<td width="39%">Position</td>
<td width="7%">Name</td>
<td width="9%">Goals</td>
</tr>
<?php while ($databaseArray = mysql_fetch_array($databaseInfo)){ ?>
<tr>
<td> <input type="hidden" name="id[]" value="<?php echo $databaseArray['name']; ?>">
<?php echo $databaseArray['position']; ?></td>
<td><input name="nme[]" type="text" id="nme" value="<?php echo $databaseArray['name']?>" size="36" /></td>
<td><input name="gls[]" type="text" id="gls" value="<?php echo $databaseArray['goals']?>" size="36" />
</td>
</tr>
<?php } // end of while loop ?>
</table>
<input type="submit" name="Submit" value="Submit" class="button">
</form>
</body>
</html>

Fumigator
07-12-2007, 11:41 PM
Two things...

First:


<input type="hidden" name="id[]" value="<?php echo $databaseArray['name']; ?>">


Do you really want to assign this hidden input field the value "name", and not "match_id"?

Second:


<?php while ($databaseArray = mysql_fetch_array($databaseInfo)){ ?>
<input name="nme[]" type="text" id="nme" value="<?php echo $databaseArray['name']?>" size="36" />
<?php } ?>


This is going to create multiple <input> tags with the same "id" value. This is not valid HTML; always makes sure your HTML validates to avoid problems.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum