9 - PHP:MySQL - The "putter page"
This kind of page may be a new concept for you. The user never sees this page! Our insert page sends information to this page and this page acts on it, but it immediately sends the user back to the insert page. When they go back, they see the new state of the table information. So let's take this code, make a page from it, and upload to the server.
copy from just below this line....
<?
// This is the code to insert a new record via the form
on our insertData page.
// isset looks to see if there's anything in the Server Variable "QUERY_STRING",
which holds information
// from a form that is being sent to this page. If there is information in that
variable, it executes the code.
if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
//Define the connection string
$conn = mysql_connect("localhost", "stevex64_july","2004")
or die("Could not connect to database");
//Connect to "the database" with $theConnectionString
mysql_select_db("stevex64_test01") or
die("Could not select database");
// Now we build our INSERT statement, just like we did
at the command line.
$query = "INSERT INTO people VALUES ('','$firstName','$lastName','$age')";
// This runs the $query command and checks to make sure
everything worked right with it.
// If there was an error, it will tell us that.
if (mysql_query($query)) {
// Here's something worth noticing. The "header" statement writes
out new html headers to this page. The Location value
// gets set to the URL of the page you want to send the user to. This is one
way to do a re-direct in PHP.
header("Location:01.php");
exit;
}
else
{
echo("<P>Error adding new record: " .
mysql_error() . "</P>");
}
}
//Close your connection.
mysql_close($conn);
?>
...to just above this line.