I am learning mySQL and PHP and am very appreciative of the help I have received here. As some of you know, I am working on a Hockey Pool site.
One of my first experiments was to build a static form on a php page that would take the input of the user and enter it into the database. I had success with that project.
Now, I want to build to run a query that will yield a table based on the query limitations. In this particular example I want players from a specific NHL team to show from my database. In my form, I want to show what the current value of the field is in the database and leave a input box to update that number. You can see my progess here:
http://www.cornwallfantasyhockey.com/data/mtl/Index.php
I can build the table and the input boxes, yet when I submit new data, it doesnt update the existing values. I think it is because it is not drawing an association from the PRIMARY KEY or in my case [playerID]. Can anybody please provide some guidance as to how to proceed?
Here is my code
<form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
<center>
<img src="/images/data_entry_mtl.png" width="625" height="100" border="0" alt="Data Entry Title">
<!--Team Roster table starts here (TMI) -->
<?php
// Make a MySQL Connection
mysql_connect("localhost", "user", "password") or die(mysql_error());
mysql_select_db("playerdb") or die(mysql_error());
// Get all the data from the "player" table
error_reporting (E_ERROR);
$result = mysql_query("SELECT * FROM stats1011 where Team = 'MTL' order by field(pos,'LW','C','RW','D','G'), last")
or die(mysql_error());
echo "<table width='870' border='1' cellspacing='0' cellpadding='2' bgcolor='ffffff'>";
echo "<tr>
<td colspan='3' align='center' bgcolor='000000'><font face='arial' size='2' color='ffffff'><b>Player Info</b></td>
<td colspan='4' align='center' bgcolor='CC0000'><font face='arial' size='2' color='ffffff'><b>Week #1</b></td>
<td colspan='4' align='center' bgcolor='FFFF00'><font face='arial' size='2' color='000000'><b>Week #2</b></td>
<td colspan='4' align='center' bgcolor='CC0000'><font face='arial' size='2' color='ffffff'><b>Week #3</b></td>
<td colspan='4' align='center' bgcolor='FFFF00'><font face='arial' size='2' color='000000'><b>Week #4</b></td>
<td colspan='3' align='center' bgcolor='000000'><font face='arial' size='2' color='ffffff'><b>TOTALS</b></td>
</tr>";
echo "<tr>
<td width='5' bgcolor='000000' align='left'><font face='arial' size='1' color='FFFFFF'><b>Pos</b></td>
<td width='10' bgcolor='000000' align='center'><font face='arial' size='1' color='FFFFFF'><b>#</b></td>
<td width='170' bgcolor='000000' align='center'><font face='arial' size='1' color='FFFFFF'><b>PLAYER</b></td>
<td width='20' bgcolor='000000' align='center'><font face='arial' size='1' color='FFFFFF'><b>GP</b></td>
<td width='2' bgcolor='000000' align='center'><font face='arial' size='1' color='FFFFFF'><b>GP</b></td>
<td width='20' bgcolor='000000' align='center'><font face='arial' size='1' color='FFFFFF'><b>PTS</b></td>
<td width='2' bgcolor='000000' align='center'><font face='arial' size='1' color='FFFFFF'><b>PTS</b></td>
<td width='20' bgcolor='000000' align='center'><font face='arial' size='1' color='FFFFFF'><b>GP</b></td>
<td width='2' bgcolor='000000' align='center'><font face='arial' size='1' color='FFFFFF'><b>GP</b></td>
<td width='20' bgcolor='000000' align='center'><font face='arial' size='1' color='FFFFFF'><b>PTS</b></td>
<td width='2' bgcolor='000000' align='center'><font face='arial' size='1' color='FFFFFF'><b>PTS</b></td>
<td width='20' bgcolor='000000' align='center'><font face='arial' size='1' color='FFFFFF'><b>GP</b></td>
<td width='2' bgcolor='000000' align='center'><font face='arial' size='1' color='FFFFFF'><b>GP</b></td>
<td width='20' bgcolor='000000' align='center'><font face='arial' size='1' color='FFFFFF'><b>PTS</b></td>
<td width='2' bgcolor='000000' align='center'><font face='arial' size='1' color='FFFFFF'><b>PTS</b></td>
<td width='20' bgcolor='000000' align='center'><font face='arial' size='1' color='FFFFFF'><b>GP</b></td>
<td width='2' bgcolor='000000' align='center'><font face='arial' size='1' color='FFFFFF'><b>GP</b></td>
<td width='20' bgcolor='000000' align='center'><font face='arial' size='1' color='FFFFFF'><b>PTS</b></td>
<td width='2' bgcolor='000000' align='center'><font face='arial' size='1' color='FFFFFF'><b>PTS</b></td>
<td width='30' bgcolor='000000' align='center'><font face='arial' size='1' color='FFFFFF'><b>GP</b></td>
<td width='30' bgcolor='000000' align='center'><font face='arial' size='1' color='FFFFFF'><b>PTS</b></td>
<td width='30' bgcolor='000000' align='center'><font face='arial' size='1' color='FFFFFF'><b>PPG</b></td>
</tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
$GP1011=$row['GP1011_wk1']+$row['GP1011_wk2']+$row['GP1011_wk3']+$row['GP1011_wk4']+$row['GP1011_wk5']+$row['GP1011_wk6']+$row['GP1011_wk7']+$row['GP1011_wk8']+$row['GP1011_wk9']+$row['GP1011_wk10']+$row['GP1011_wk11']+$row['GP1011_wk12']+$row['GP1011_wk13']+$row['GP1011_wk14']+$row['GP1011_wk15']+$row['GP1011_wk16']+$row['GP1011_wk17']+$row['GP1011_wk18']+$row['GP1011_wk19']+$row['GP1011_wk20']+$row['GP1011_wk21']+$row['GP1011_wk22']+$row['GP1011_wk23']+$row['GP1011_wk24']+$row['GP1011_wk25']+$row['GP1011_wk26'];
$PTS1011=$row['PTS1011_wk1']+$row['PTS1011_wk2']+$row['PTS1011_wk3']+$row['PTS1011_wk4']+$row['PTS1011_wk5']+$row['PTS1011_wk6']+$row['PTS1011_wk7']+$row['PTS1011_wk8']+$row['PTS1011_wk9']+$row['PTS1011_wk10']+$row['PTS1011_wk11']+$row['PTS1011_wk12']+$row['PTS1011_wk13']+$row['PTS1011_wk14']+$row['PTS1011_wk15']+$row['PTS1011_wk16']+$row['PTS1011_wk17']+$row['PTS1011_wk18']+$row['PTS1011_wk19']+$row['PTS1011_wk20']+$row['PTS1011_wk21']+$row['PTS1011_wk22']+$row['PTS1011_wk23']+$row['PTS1011_wk24']+$row['PTS1011_wk25']+$row['PTS1011_wk26'];
// Print out the contents of each row into a table
echo "<tr><td width='5' bgcolor='FFFFFF' align='center'><font face='arial' size='1' color='000000'>";
echo $row['Pos'];
echo "</td><td width='10' bgcolor='FFFFFF' align='center'><font face='arial' size='1' color='000000'>";
echo $row['No'];
echo "</td><td width='170' bgcolor='FFFFFF' align='left'><font face='arial' size='2' color='000000'>";
echo $row['Last'];
echo ", <font face='arial' size='1' color='000000'>";
echo $row['First'];
echo "</td><td width='20' bgcolor='CCCCCC' align='center'><font face='arial' size='2' color='000000'>";
echo $row['GP1011_wk1'];
echo "</td><td width='2' bgcolor='CCCCCC' align='center'><font face='arial' size='2' color='000000'>";
echo "<input type='text' size='2' name='PTS1011_wk1'>";
echo "</td><td width='20' bgcolor='CCCCCC' align='center'><font face='arial' size='2' color='000000'>";
echo $row['PTS1011_wk1'];
echo "</td><td width='2' bgcolor='CCCCCC' align='center'><font face='arial' size='2' color='000000'>";
echo "<input type='text' size='2' name='PTS1011_wk1'>";
echo "</td><td width='20' bgcolor='FFFF99' align='center'><font face='arial' size='2' color='000000'>";
echo $row['GP1011_wk2'];
echo "</td><td width='2' bgcolor='FFFF99' align='center'><font face='arial' size='2' color='000000'>";
echo "<input type='text' size='2' name='GP1011_wk2'>";
echo "</td><td width='20' bgcolor='FFFF99' align='center'><font face='arial' size='2' color='000000'>";
echo $row['PTS1011_wk2'];
echo "</td><td width='2' bgcolor='FFFF99' align='center'><font face='arial' size='2' color='000000'>";
echo "<input type='text' size='2' name='PTS1011_wk2'>";
echo "</td><td width='20' bgcolor='CCCCCC' align='center'><font face='arial' size='2' color='000000'>";
echo $row['GP1011_wk3'];
echo "</td><td width='2' bgcolor='CCCCCC' align='center'><font face='arial' size='2' color='000000'>";
echo "<input type='text' size='2' name='GP1011_wk3'>";
echo "</td><td width='20' bgcolor='CCCCCC' align='center'><font face='arial' size='2' color='000000'>";
echo $row['PTS1011_wk3'];
echo "</td><td width='2' bgcolor='CCCCCC' align='center'><font face='arial' size='2' color='000000'>";
echo "<input type='text' size='2' name='PTS1011_wk3'>";
echo "</td><td width='20' bgcolor='FFFF99' align='center'><font face='arial' size='2' color='000000'>";
echo $row['GP1011_wk4'];
echo "</td><td width='2' bgcolor='FFFF99' align='center'><font face='arial' size='2' color='000000'>";
echo "<input type='text' size='2' name='GP1011_wk4'>";
echo "</td><td width='20' bgcolor='FFFF99' align='center'><font face='arial' size='2' color='000000'>";
echo $row['PTS1011_wk4'];
echo "</td><td width='2' bgcolor='FFFF99' align='center'><font face='arial' size='2' color='000000'>";
echo "<input type='text' size='2' name='PTS1011_wk4'>";
echo "</td><td width='30' bgcolor='666666' align='center'><font face='arial' size='2' color='ffffff'><b>";
echo $GP1011;
echo "</td><td width='30' bgcolor='666666' align='center'><font face='arial' size='2' color='ffffff'><b>";
echo $PTS1011;
echo "</td><td width='30' bgcolor='666666' align='center'><font face='arial' size='2' color='ffffff'><b>";
echo number_format($PTS1011/$GP1011,2);
echo "</td></tr>";
}
echo "</table>";
?>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
<?php
mysql_connect ("localhost","user","password") or die ('Error: ' . mysql_error());
mysql_select_db ("playerdb")or die("cannot select DB");
$playerID=$_POST['playerID'];
$Pos=$_POST['Pos'];
$No=$_POST['No'];
$Last=$_POST['Last'];
$First=$_POST['First'];
$GP1011_wk1=$_POST['GP1011_wk1'];
$PTS1011_wk1=$_POST['PTS1011_wk1'];
$GP1011_wk2=$_POST['GP1011_wk2'];
$PTS1011_wk2=$_POST['PTS1011_wk2'];
$GP1011_wk3=$_POST['GP1011_wk3'];
$PTS1011_wk3=$_POST['PTS1011_wk3'];
$GP1011_wk4=$_POST['GP1011_wk4'];
$PTS1011_wk4=$_POST['PTS1011_wk4'];
if($_SERVER['REQUEST_METHOD']=='POST') {
$sql="INSERT INTO stats1010 (playerID, Pos, No, Last, First, GP1011_wk1, PTS1011_wk1, GP1011_wk2, PTS1011_wk2, GP1011_wk3, PTS1011_wk3, GP1011_wk4, PTS1011_wk4) VALUES ('".$playerID."','".$Pos."','".$No."','".$Last."','".$First."','".$GP1011_wk1."','".$PTS1011_wk1."','".$GP1011_wk2."','".$PTS1011_wk2."','".$GP1011_wk3."','".$PTS1011_wk3."','".$GP1011_wk4."','".$PTS1011_wk4."')";
$result=mysql_query($sql);
}
// if successfully insert data into database, displays message "Successful".
if($result){
echo "<font face='arial' size='2' color='ffffff'>Successful";
echo "<BR>";
echo "<font face='arial' size='2' color='ffffff'>Database updated with: ".$playerID ;
}
else {
echo "<font face='arial' size='2' color='ffffff'>ERROR";
}
// close connection
mysql_close();
?>
Hoping someone can help!
Cheers,
Chris