Jump to content

Not able to update multiple columns with a single form


Greysoul

Recommended Posts

So this is my problem;

I'm starting to learn PHP and how it works with SQL.  I run into problems non stop, and most of the time when i'm at my breaking point i find a solution via searching around the internet.  I've found a lot of tutorials on Updating data in an SQL database.  I now know how to update many rows at once with a single form, which is cool.  My main problem is i'm utterly FAILING at updating two or more columns in a table ..with the same form.    Basically i can display everything i want, but when i hit update..it all goes down hill.  My second page Update query info is where i bomb out...no matter what variation i try it just doesn't work.  If anyone can point me in the right direction to finish the code, i'd greatly appreciate it. I'll post a picture of what i'm trying to accomplish.

 

first page

<?
// Connect database.
$host="localhost"; // Host name.
$db_user="test"; // MySQL username.
$db_password="test"; // MySQL password.
$database="test"; // Database name.
mysql_connect($host,$db_user,$db_password);
mysql_select_db($database);

// Select all data records in table "name_list" and put them into $result.
$result=mysql_query("select * from Players");
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="playerupdate.php">
<table border="1" cellpadding="3" cellspacing="0">

<td align="center" bgcolor="#FFCC00"><strong>Clan</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>Name</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>GP</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>Scores</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>Kills</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>Deaths</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>SPG</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>KPG</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>K/D</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>FA</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>FC</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>FP%</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>MVP</strong></td>
</tr>
<?
// Fetch record rows in $result by while loop and put them into $row.
while($row=mysql_fetch_assoc($result)){
?>
<tr>
<td bgcolor="#FFFFCC"><input name="<? echo $row['ClanName']; ?>" size="4" type="text" id="<? echo $row['ClanName']; ?>" value="<? echo $row['ClanName']; ?>" /></td>
<td bgcolor="#FFFFCC"><input name="<? echo $row['Player']; ?>" size="15" type="text" id="<? echo $row['Player']; ?>" value="<? echo $row['Player']; ?>" /></td>
<td bgcolor="#FFFFCC"><input name="<? echo $row['GP']; ?>" size="4" type="text" id="<? echo $row['GP']; ?>" value="<? echo $row['GP']; ?>" /></td>
<td bgcolor="#FFFFCC"><input name="<? echo $row['Scores']; ?>" size="4" type="text" id="<? echo $row['Scores']; ?>" value="<? echo $row['Scores']; ?>" /></td>
<td bgcolor="#FFFFCC"><input name="<? echo $row['Kills']; ?>" size="4" type="text" id="<? echo $row['Kills']; ?>" value="<? echo $row['Kills']; ?>" /></td>
<td bgcolor="#FFFFCC"><input name="<? echo $row['Deaths']; ?>" size="4" type="text" id="<? echo $row['Deaths']; ?>" value="<? echo $row['Deaths']; ?>" /></td>
<td bgcolor="#FFFFCC"><input name="<? echo $row['SPG']; ?>" size="4" type="text" id="<? echo $row['SPG']; ?>" value="<? echo $row['SPG']; ?>" /></td>
<td bgcolor="#FFFFCC"><input name="<? echo $row['KPG']; ?>" size="4" type="text" id="<? echo $row['KPG']; ?>" value="<? echo $row['KPG']; ?>" /></td>
<td bgcolor="#FFFFCC"><input name="<? echo $row['KD']; ?>" size="4" type="text" id="<? echo $row['KD']; ?>" value="<? echo $row['KD']; ?>" /></td>
<td bgcolor="#FFFFCC"><input name="<? echo $row['FA']; ?>" size="4" type="text" id="<? echo $row['FA']; ?>" value="<? echo $row['FA']; ?>" /></td>
<td bgcolor="#FFFFCC"><input name="<? echo $row['FC']; ?>" size="4" type="text" id="<? echo $row['FC']; ?>" value="<? echo $row['FC']; ?>" /></td>
<td bgcolor="#FFFFCC"><input name="<? echo $row['FP']; ?>" size="4" type="text" id="<? echo $row['FP']; ?>" value="<? echo $row['FP']; ?>" /></td>
<td bgcolor="#FFFFCC"><input name="<? echo $row['MVP']; ?>" size="4" type="text" id="<? echo $row['MVP']; ?>" value="<? echo $row['MVP']; ?>" /></td>


</tr>
<? } // End while loop. ?>
</table>
<input type="submit" name="Submit" value="Update" />
</form>
</body>
</html>

 

second page

<?
if($_POST['Submit']){ // If receive Submit button variable.

// Connect database.
$host="localhost"; // Host name.
$db_user="test"; // MySQL username.
$db_password="test"; // MySQL password.
$database="test"; // Database name.
mysql_connect($host,$db_user,$db_password);
mysql_select_db($database);

// Select all data records in table "name_list" and put them into $result.
$result=mysql_query("select Player from Players order by id asc");

// Fetch record rows in $result by while loop and put them into $row.
while($row=mysql_fetch_assoc($result)){
// Get the posted value "name_ID value" from form.php. This variable change it's value by while loop.
$name=$_POST[$row[Player]];

// Update field "name", matching with "id" value by while loop.
mysql_query("UPDATE Players SET Player='$name' WHERE Player='$row[Player]'");
}
echo "--- Update Complete ---";
}
?>

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

for whatever weird reason it won't let me edit again.  just wanted to say its safe to ignore most of the //'s...it was copied code where i changed variables.  also the code above is where only the player name column works..i left it at that so you could see how far i had really gotten. 

Link to comment
Share on other sites

try to cange form to

<?
// Connect database.
$host="localhost"; // Host name.
$db_user="test"; // MySQL username.
$db_password="test"; // MySQL password.
$database="test"; // Database name.
mysql_connect($host,$db_user,$db_password);
mysql_select_db($database);

// Select all data records in table "name_list" and put them into $result.
$result=mysql_query("select * from Players");
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="playerupdate.php">
<table border="1" cellpadding="3" cellspacing="0">

<td align="center" bgcolor="#FFCC00"><strong>Clan</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>Name</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>GP</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>Scores</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>Kills</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>Deaths</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>SPG</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>KPG</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>K/D</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>FA</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>FC</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>FP%</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>MVP</strong></td>
</tr>
<?
// Fetch record rows in $result by while loop and put them into $row.
while($row=mysql_fetch_assoc($result)){
$id = $row['Player']; // if 'Player' is primary key of your table
?>
<tr>
<td bgcolor="#FFFFCC"><input name="ClanName[<? echo $id; ?>"] size="4" type="text" id="<? echo $row['ClanName']; ?>" value="<? echo $row['ClanName']; ?>" /></td>
<td bgcolor="#FFFFCC"><input name="Player[<? echo $id; ?>"] size="15" type="text" id="<? echo $row['Player']; ?>" value="<? echo $row['Player']; ?>" /></td>
<td bgcolor="#FFFFCC"><input name="GP[<? echo $id; ?>"] size="4" type="text" id="<? echo $row['GP']; ?>" value="<? echo $row['GP']; ?>" /></td>
<td bgcolor="#FFFFCC"><input name="Scores[<? echo $id; ?>"] size="4" type="text" id="<? echo $row['Scores']; ?>" value="<? echo $row['Scores']; ?>" /></td>
<td bgcolor="#FFFFCC"><input name="Kills[<? echo $id; ?>"] size="4" type="text" id="<? echo $row['Kills']; ?>" value="<? echo $row['Kills']; ?>" /></td>
<td bgcolor="#FFFFCC"><input name="Deaths[<? echo $id; ?>"] size="4" type="text" id="<? echo $row['Deaths']; ?>" value="<? echo $row['Deaths']; ?>" /></td>
<td bgcolor="#FFFFCC"><input name="SPG[<? echo $id; ?>"] size="4" type="text" id="<? echo $row['SPG']; ?>" value="<? echo $row['SPG']; ?>" /></td>
<td bgcolor="#FFFFCC"><input name="KPG[<? echo $id; ?>"] size="4" type="text" id="<? echo $row['KPG']; ?>" value="<? echo $row['KPG']; ?>" /></td>
<td bgcolor="#FFFFCC"><input name="KD[<? echo $id; ?>"] size="4" type="text" id="<? echo $row['KD']; ?>" value="<? echo $row['KD']; ?>" /></td>
<td bgcolor="#FFFFCC"><input name="FA[<? echo $id; ?>"] size="4" type="text" id="<? echo $row['FA']; ?>" value="<? echo $row['FA']; ?>" /></td>
<td bgcolor="#FFFFCC"><input name="FC[<? echo $id; ?>"] size="4" type="text" id="<? echo $row['FC']; ?>" value="<? echo $row['FC']; ?>" /></td>
<td bgcolor="#FFFFCC"><input name="FP[<? echo $id; ?>"] size="4" type="text" id="<? echo $row['FP']; ?>" value="<? echo $row['FP']; ?>" /></td>
<td bgcolor="#FFFFCC"><input name="MVP[<? echo $id; ?>"] size="4" type="text" id="<? echo $row['MVP']; ?>" value="<? echo $row['MVP']; ?>" /></td>


</tr>
<? } // End while loop. ?>
</table>
<input type="submit" name="Submit" value="Update" />
</form>
</body>
</html>

and 2nd page to

<?
if($_POST['Submit']){ // If receive Submit button variable.
$host="localhost"; // Host name.
$db_user="test"; // MySQL username.
$db_password="test"; // MySQL password.
$database="test"; // Database name.
mysql_connect($host,$db_user,$db_password);
mysql_select_db($database);
foreach ($_POST['Player'] as $id => $Player){
$ClanName = $_POST['ClanName'][$id];
$GP = $_POST['GP'][$id];
// etc.

mysql_query("UPDATE Players SET ClanName='$ClanName', GP='$GP' WHERE Player='$id'");
}
echo "--- Update Complete ---";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.