View Full Version : Help with Update script

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:


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?




// 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

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">
<td width="39%">Position</td>
<td width="7%">Name</td>
<td width="9%">Goals</td>
<?php while ($databaseArray = mysql_fetch_array($databaseInfo)){ ?>
<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" />
<?php } // end of while loop ?>
<input type="submit" name="Submit" value="Submit" class="button">

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


<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"?


<?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.