Databases Reference
In-Depth Information
// Run the query through the connection
if (($result = @ mysqli_query($connection, $query))==FALSE)
showerror($connection);
// Get the matching gift row;
// (there's only one since the gift_id is the primary key)
// If we don't get exactly one answer, then we have a problem
for($matchedrows=0;($row = @ mysqli_fetch_array($result));$matchedrows++);
if($matchedrows!=1)
die("We've just experienced a technical problem - ".
"please notify the administrator.");
// Double-check they actually have this gift reserved
if (!empty($row["username"]) && $row["username"] != $_SESSION['username'])
// They don't, so record a message to show the user
$message = "That's not your gift, {$_SESSION['username']}!";
else
{
// They do have it reserved. Create a query to unreserve it.
$query = "UPDATE gifts SET username = NULL WHERE gift_id = {$gift_id}";
// Run the query through the connection
if (($result = @ mysqli_query($connection, $query))==FALSE)
showerror($connection);
// Create a message to show the user
if (mysqli_affected_rows($connection) == 1)
$message = "Removed the gift from your shopping list, ".
"{$_SESSION['username']}";
else
$message = "There was a problem updating. ".
"Please contact the administrator.";
}
}
}
// Redirect the browser back to list.php
header("Location: list.php?message=" . urlencode($message));
exit;
?>
Example A-2. db.php
<?php
// These are the DBMS credentials and the database name
$DB_hostname = "localhost";
$DB_username = "fred";
$DB_password = "shhh";
$DB_databasename = "wedding";
// Show an error and stop the script
function showerror($connection)
{
// Was there an error during connection?
if(mysqli_connect_errno())
 
Search WWH ::




Custom Search