🙂 It was alot of help getting stated on this, Thanks a mil. The form is filling the data out correctly, but the insert is not posting the data, I am still having some problems. I have followed this fairly close to this, but there are 2 issues that I have not resolved?
1.) I need the form to retreive the data from Field ID1 to prefill out the form.
2.) The insert code is not posting to the SQL Server and does not return to the Update page.
Here is the code:
ScoreUpdate.php:
<html>
<!-- Created with the CoffeeCup HTML Editor -->
<!-- http://www.coffeecup.com/ -->
<!-- Brewed on 1/21/2003 6:55:33 AM -->
<head>
<title></title>
</head>
<body>
<html>
<head><title>Score Update</title></head>
<body>
<CENTER><H1>Score Update</H1></CENTER>
<CENTER>
<form method="POST" action="ScoreInsert.php">
<table cellpadding="0" cellspacing="0" width="300">
<tr>
<td width="50%">
Visitor Team
</td>
<td width="50%">
<input type="text" name="Vteam" size="20">
</td>
<td width="50%">
Score
</td>
<td width="50%">
<input type="text" name="Vscore" size="2">
</td>
</tr>
<td width="50%">
Visitor Team
</td>
<td width="50%">
<input type="text" name="Hteam" size="20">
</td>
<td width="50%">
Score
</td>
<td width="50%">
<input type="text" name="Hscore" size="2">
</td>
</tr>
<td width="50%">
Period
</td>
<td width="50%">
<input type="text" name="Period" size="20">
</td>
<td width="50%">
</td>
<td width="50%">
</td>
</tr>
<tr>
<td width="50%">
</td>
<td width="50%">
<input type="submit" value="Submit" name="btnSubmit">
</td>
</tr>
</table>
</form>
</body>
</html>
</body>
</html>
ScoreInsert.php:
<?php
// check to see if firstname is passed as part of the query string. If it doesn't exist, redirect to an error page and exit immediately
This could happen if some hacker or a user bookmarked this page and came into it by accident. You want to check to see if the variables exist
if (!array_key_exists("Vteam", $_POST)) {
$loc = "Error.php?e=1";
header("Location: " . $loc);
exit(1);
}
// check to see if firstname is passed as part of the query string. If it doesn't exist, redirect to an error page and exit immediately
if (!array_key_exists("Vscore", $_POST)) {
$loc = "Error.php?e=2";
header("Location: " . $loc);
exit(1);
}
if (!array_key_exists("Hteam", $_POST)) {
$loc = "Error.php?e=1";
header("Location: " . $loc);
exit(1);
}
// check to see if firstname is passed as part of the query string. If it doesn't exist, redirect to an error page and exit immediately
if (!array_key_exists("Hscore", $POST)) {
$loc = "http://www.yoursite.com/myError.php?e=2";
header("Location: " . $loc);
exit(1);
}
if (!array_key_exists("Period", $POST)) {
$loc = "http://www.yoursite.com/myError.php?e=1";
header("Location: " . $loc);
exit(1);
}
// At this point we know firstname and lastname exist so store them in variables
$Vteam = $POST['Vteam'];
$Vscore = $POST['Vscore'];
$Hteam = $POST['Hteam'];
$Hscore = $POST['Hscore'];
$Period = $_POST['Period'];
// Here you would do some validation and checking (like bad names, too long of a name, duplicate names, etc.). Assume DoValidation() accomplishes this and returns true if data is ok, false if it is bad. It is up to you to write this function.
if (DoValidation($Vteam, $Vscore, $Hteam, $Hscore, $Period ) == false) {
$loc = "yError.php"?e=3;
header("Location: " . $loc);
exit(1);
}
// ok now you have the variables stored and its time to persist the data in your mySQL database.
// First lets connect to mySQL. Here you will put in the host name of the server/IP address to the database, the administrator username and password
$dbLink = mysql_connect("localhost", "xxxx", "xxxx");
if ($dbLink == false) {
$loc = "myError.php?e=4";
header("Location: " . $loc);
exit(1);
}
// Second, you must select a database you want to work with. This is "myUser" database as we setup above in mySchema.sql
$result = mysql_select_db("LiveScore");
if ($result == false) {
$loc = "Error.php?e=5";
header("Location: " . $loc);
exit(1);
}
// Third, we prepare the SQL statement to insert into the database. Notice we do not specify the userId column as this is automatically generated by mySQL
$sql = "insert into score (VistorTeam, VisitorScore, HomeTeam, HomeScore, Period) values ('$Vteam', 'Vscore', '$Hteam', 'Hscore', '$Period')";
// Fourth, we execute the query
$result = mysql_query($sql, $dbLink);
if ($result == false) {
$loc = "Error.php?e=6";
header("Location: " . $loc);
exit(1);
}
// Fifth, close the database connection
$result = mysql_close($dbLink);
if ($dbLink == false) {
$loc = "Error.php?e=7";
header("Location: " . $loc);
exit(1);
}
// Finally, redirect to a success page
$loc = "http://sec.lavaa.net/Livescore/ScoreUpdate.php";
header("Location: " . $loc);
exit(0);
?>
Thanks for all the help, and PS, The first 3 lines were not boring to me at all! Everything very helpful.
🙂