Forum Moderators: coopster

Message Too Old, No Replies

Dynamic Form and Form Processing

         

airpirate

11:10 pm on Nov 28, 2011 (gmt 0)

10+ Year Member



So I know a little about PHP but I am no expert by any means. But I have a project that I am working on for a fantasy football league and need some help. My users pick players from a list and then their selections are put into a database. So more than one user is likely to pick the same player. Then I need to score the players based off their games for the week. So I have code that gets the Distinct PlayerID and creates a form to update the player score (see code below), but I have no idea how to process the form. It's a little more complicated then the forms I've used before because the MySQL query would need to UPDATE all the rows for each individual PlayerID. Am I making any sense?

Anyway, here is the code. If anyone has suggestions on how to process this form or a better way of doing it then please let me know.

<?
print '<form id="form1" name="form1" method="post" action="update_player.php">';
// Connecting, selecting database
$link = mysql_connect('localhost','user','pass');
if (!$link) {
die('Could not connect: ' . mysql_error());
}

//Query
$query=mysql_query("select DISTINCT(PlayerID), PlayerName, Team From fantasy4.temp ORDER BY Team;") or die ('Could not connect: ' . mysql_error());
print'
<center>
<table align=center border=0 cellpadding=0 cellspacing=2 width=350>
<tr align=center>
<td width=50 align=center><b>Player ID </b></td>
<td width=50 align=center><b>Team</b></td>
<td width=200 align=center><b>Player Name</b></td>
<td width=50 align=center><b>Score</b></td>
<tr><td colspan="10" bgcolor="black" height="1"></td></tr>
';

while($row=mysql_fetch_array($query)){
if($color == 1)
{
print '<tr bgcolor=#dDdDdD>
<td align=center> ' . $row['PlayerID'] . ' </td>
<td align=center> ' . $row['Team'] . ' </td>
<td align=center> ' . $row['PlayerName'] . ' </td>
<td align=center> <input name="' . $row['PlayerID'] . '" type="text" id="' . $row['PlayerID'] . '" size="5" maxlength="5" /> </td>
</tr>';
$color=0;
}
else
{
print '<tr>
<td align=center> ' . $row['PlayerID'] . ' </td>
<td align=center> ' . $row['Team'] . ' </td>
<td align=center> ' . $row['PlayerName'] . ' </td>
<td align=center> <input name="' . $row['PlayerID'] . '" type="text" id="' . $row['PlayerID'] . '" size="5" maxlength="5" /> </td>
</tr>';
$color=1;

}
}
print '</table>';
print '<input type="submit" name="button" id="button" value="Update Player Scores" /></form>';
?>

airpirate

11:12 pm on Nov 28, 2011 (gmt 0)

10+ Year Member



This is what I tried that did not work

<?

// Connecting, selecting database
$link = mysql_connect('localhost','user','pass');
if (!$link) {
die('Could not connect: ' . mysql_error());
}

//Query
$query=mysql_query("select DISTINCT(PlayerID) From fantasy4.temp;") or die ('Could not connect: ' . mysql_error());


while($row=mysql_fetch_array($query)){
$PlayerID = $_POST[$row['PlayerID']];
}

while($score = array($_POST['$PlayerID'])){


//Insert Query

$query2=mysql_query("UPDATE fantasy4.temp set Score='$score' where PlayerID='$PlayerID'") or die ('Yikes could not connect: ' .mysql_error());

$result = @mysql_query($query2);
}
//Check whether the query was successful or not
if($result) {
header("location: register-success.php");
exit();
}else {
die("Query failed - " .mysql_error());
}

?>