/*******************************************************************************
* Author: Geoff Moes and Robert Sheldon
* Project: Begining MySQL - Chapter 17
* Module: edit.php
* Date: 12/10/04
*
* Description:
*
* This page lists the individual record. It is called from the main page.
* If the page is called with an "add" command, it creates a blank record
* and populates it with default values. If it is called with an "edit"
* command, it displays an existing record.
*
*******************************************************************************/
?>
// Connect to server or return an error
$link = mysql_connect("localhost", "mysqlapp", "pw1")
or die("Could not connect: " . mysql_error());
// Select a database or return an error
mysql_select_db("DVDRentals", $link);
// Initialize an error-related variable
$error = "";
// Initialize variables with parameters retrieved from the posted form
if(isset($_POST["command"]))
$command = $_POST["command"];
if(isset($_POST["transaction_id"]))
$transactionId = $_POST["transaction_id"];
if(isset($_POST["date_due"]))
$dateDue = $_POST["date_due"];
if(isset($_POST["order_id"]))
$orderId = $_POST["order_id"];
if(isset($_POST["dvd_id"]))
$dvdId = $_POST["dvd_id"];
if(isset($_POST["date_out"]))
$dateOut = $_POST["date_out"];
if(isset($_POST["date_in"]))
$dateIn = $_POST["date_in"];
// Processes the save and savenew commands
if((strcmp("save", $command) == 0) || (strcmp("savenew", $command) == 0))
{
// Check for missing parameters
if($orderId == -1)
$error .= "Please select an \"Order\"
";
if($dvdId == -1)
$error .= "Please select a \"DVD\"
";
if(($dateOut == null) || (strlen($dateOut) == 0))
$error .= "Please enter a \"Date Out\" Value
";
if(($dateDue == null) || (strlen($dateDue) == 0))
$error .= "Please enter a \"Date Due\" Value
";
if(strlen($error) == 0)
{
// Reformat dates so that they are compatible with the MySQL format
if($dateOut != null)
$dateOut = substr($dateOut, 6, 4)."-".substr($dateOut, 0, 2)."-".substr($dateOut, 3, 2);
if($dateDue != null)
$dateDue = substr($dateDue, 6, 4)."-".substr($dateDue, 0, 2)."-".substr($dateDue, 3, 2);
if($dateIn != null)
$dateIn = substr($dateIn, 6, 4)."-".substr($dateIn, 0, 2)."-".substr($dateIn, 3, 2);
else
$dateIn ="0000-00-00";
if(strcmp("save", $command) == 0)
{
// Run the update in update.php
include "update.php";
}
else
{
// Run the insert in insert.php
include "insert.php";
}
// Redirect the application to the listing page
header("Location: index.php");
exit;
}
}
else
{
// If it is a new record, initialize the variables to default values
if(strcmp("add", $command) == 0)
{
$transactionId = 0;
$orderId = 0;
$dvdId = 0;
$dateOut = date("m-d-Y", time());
$dateDue = date("m-d-Y", time() + 3*24*60*60);
$dateIn = "";
}
else
{
// If it is an existing record, read from database
if($transactionId != null)
{
// Build query from transactionId value passed down from form
$selectSql = "SELECT ".
"OrderID, ".
"DVDID, ".
"DateOut, ".
"DateDue, ".
"DateIn ".
"FROM Transactions ".
"WHERE TransID = '$transactionId'";
// Execute query
$result = mysql_query($selectSql, $link);
if (!$result)
die("Invalid query: " . mysql_error(). "
".$selectSql);
// Populate the variables for display into the form
if($row = mysql_fetch_array($result))
{
$orderId = $row["OrderID"];
$dvdId = $row["DVDID"];
$dateOut = $row["DateOut"];
$dateDue = $row["DateDue"];
$dateIn = $row["DateIn"];
// Reformat the dates into a more readable form
if($dateOut != null)
$dateOut = substr($dateOut, 5, 2)."-".substr($dateOut, 8, 2)."-".substr($dateOut, 0, 4);
else
$dateOut = "";
if($dateDue != null)
$dateDue = substr($dateDue, 5, 2)."-".substr($dateDue, 8, 2)."-".substr($dateDue, 0, 4);
else
$dateDue = "";
if($dateIn != "0000-00-00")
$dateIn = substr($dateIn, 5, 2)."-".substr($dateIn, 8, 2)."-".substr($dateIn, 0, 4);
else
$dateIn = "";
}
}
}
}
?>