Jump to content


Photo

Getting No Errors But Its Still Not Working -- PHP Form & MySQL


  • Please log in to reply
2 replies to this topic

#1 j3rmain3

j3rmain3
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 26 September 2006 - 12:41 PM

I am trying to do something so simple but its not working and i have been staring at it for sooooooooooo long.

I created a form in php and want to display the data in another php site. This is the coding i have used.

php form
<html>
<head>
<title>Player Form</title>
<h3>Player Form</h3>
</head>
<body>

<?php

if (!isset($_POST['submit'])) {

?>

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
Player First Name: <input type="text" name='f_name'><p>
Player Last Name: <input type="text" name='l_name'><p>
Team: <input type="text" name='team'><p>
Position: <input type="text" name='position'><p>
Points: <input type="text" size=2 name='points'> <b> /5</b><p>
<input type="submit" name="submit" value="submit results">
</form>

<?php

} else {

$host = "localhost";
$user = "root";
$pass = "mysql";
$db = "player_of_year";

$f_name = empty($_POST['f_name']) ? die ("ERROR: Enter In First Name") : mysql_escape_string($_POST['f_name']);
$l_name = empty($_POST['l_name']) ? die ("ERROR: Enter In Last Name") : mysql_escape_string($_POST['l_name']);
$team = empty($_POST['team']) ? die ("ERROR: Enter In The Team") :
mysql_escape_string($_POST['team']);
$position = empty($_POST['position']) ? die ("ERROR: Enter In Position") :
mysql_escape_string($_POST['position']);
$points = empty($_POST['points']) ? die ("ERROR: Enter In Your Rating") :
mysql_escape_string($_POST['points']);

$connection = mysql_connect($host,$user,$pass) or die ("Unable To Connect");

mysql_select_db($db) or die ("Unable to select database!");

$query = "INSERT INTO players ('f_name', 'l_name', 'team', 'position', 'points') values ('$f_name','$l_name','$team','$position','$points')";

echo "Your Rating Has Been Recorded";
echo "<p>";

echo "<a href=http://localhost/playertable.php><b>View Table<b>";

}
?>

</body>
</html>

php table
<html>
<head>
<title>Player Of The Year</title>
<h3>Player Of The Year</h3>
</head>
<body>
<?php

$host = "localhost";
$user = "root";
$pass = "mysql";
$db = "player_of_year";

$connection = mysql_connect($host,$user,$pass) or die ("Unable to Connect!");

mysql_select_db($db) or die ("Unable to select database!");

$query = "SELECT * FROM players";

$result = mysql_query($query) or die ("ERROR in query: $query.".mysql_error());

if (mysql_num_rows($result) > 0) {

echo "<table cellpadding=10 border=1>";
echo "<tr>";
echo "<td><p align=center><b>First Name</td></b>";
echo "<td><p align=center><b>Second Name</td></b>";
echo "<td><p align=center><b>Team</td></b>";
echo "<td><p align=center><b>Position</td></b>";
echo "<td><p align-center><b>Points</td></b>";
echo "</tr>";

while ($row=mysql_fetch_row($result)) {
echo "<tr>";
echo "<td><p align=center>".$row[1]."</td>";
echo "<td><p align=center>". $row[2]."</td>";
echo "<td><p align=center>".$row[3]."</td>";
echo "<td><p align=center>".$row[4]."</td>";
echo "<td><p align=center>".$row[5]."</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "No rows found!";
}
mysql_free_result($result);

mysql_close($connection);

?>
<p>
<a href=http://localhost/playerform.php><b>Return To From</b>

</body>
</html>

If anyone can see why this would work can you please inform me because i have had enough of staring at it and not finding whats wrong with it.

Thanks
J3rmain3

#2 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 26 September 2006 - 12:59 PM

Well first thing is you did not run your query. You made your statement but did not run the query.

<?php
$query = "INSERT INTO players ('f_name', 'l_name', 'team', 'position', 'points') values ('$f_name','$l_name','$team','$position','$points')";
  $result = mysql_query($query) or die (mysql_error());

if(!result){
echo "Could not run query";
} else {
echo "Your Rating Has Been Recorded";
echo "<p>";

echo "<a href=http://localhost/playertable.php><b>View Table<b>";
}
?>

Ray

#3 j3rmain3

j3rmain3
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 26 September 2006 - 01:11 PM

Cheers Ray,
Its working now

I knew it would be something simple.

Your a legend !!

J3rmain3




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users