9 - PHP:MySQL - The Update page
The update page is another "putter" page. The user should never see it. The form from our previous page sends the edited information to this page, which updates the table with the new information. A new record is not created in the table, an existing record is updated.
copy from just below this line....
<?
// This is the code to update a record
//Define the connection string, as usual
$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");
// Here we build our SQL statement that will update the
table. We have three SQL keywords to be aware of here.
// UPDATE tells the database what we want to happen.
// SET is the command you use to set a field to a new value.
// WHERE tells the database to SET those new values in the record WHERE the
id field is the same
// as the $id value we passed to this page from the previous page.
$query = "UPDATE people SET first='$firstName' WHERE id = '$id'";
// mysql_query takes our $query string and $conn string
and runs our query on the database.
mysql_query($query, $conn);
// We have two values that might need updating, so we
will run another SQL query now. There are
// more compact ways of doing this, but for clarity we'll do it this way.
$query2 = "UPDATE people SET last='$lastName' WHERE id = '$id'";
mysql_query($query2, $conn);
// header sends this page to our first page once the
update is done.
header("Location:01.php");
exit;
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
</body>
</html>
... to just above this line.