basic-mysql-17-PHPApp-edit.php / php
<?
***************************************************************************** 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 // Select a database or return an error mysql_select_db("DVDRentals", link); // Initialize an error-related variable // Initialize variables with parameters retrieved from the posted form if(isset(_POST["command"])) _POST["command"]; if(isset(transactionId = _POST["date_due"])) _POST["date_due"]; if(isset(orderId = _POST["dvd_id"])) _POST["dvd_id"]; if(isset(dateOut = _POST["date_in"])) _POST["date_in"]; // Processes the save and savenew commands if((strcmp("save", command) == 0)) { // Check for missing parameters if(error .= "Please select an \"Order\"<br>"; if(error .= "Please select a \"DVD\"<br>"; if((dateOut) == 0)) <br>"; if((dateDue == null) || (strlen(error .= "Please enter a \"Date Due\" Value<br>"; if(strlen(// Reformat dates so that they are compatible with the MySQL format if(dateOut != null) dateOut, 6, 4)."-".substr(dateOut, 3, 2); if(dateDue = substr(dateDue, 0, 2)."-".substr(dateIn != null) dateIn, 6, 4)."-".substr(dateIn, 3, 2); else 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", transactionId = 0; dvdId = 0; dateDue = date("m-d-Y", time() + 3*24*60*60); // If it is an existing record, read from database if(transactionId != null) { // Build query from transactionId value passed down from form transactionId'"; // Execute query selectSql, result) die("Invalid query: " . mysql_error(). "<br>".// Populate the variables for display into the form if(row = mysql_fetch_array(orderId = dvdId = dateOut = dateDue = dateIn = // Reformat the dates into a more readable form if(dateOut != null) dateOut, 5, 2)."-".substr(dateOut, 0, 4); else dateDue != null) dateDue, 5, 2)."-".substr(dateDue, 0, 4); else dateIn != "0000-00-00") dateIn, 5, 2)."-".substr(dateIn, 0, 4); else <html> <head> <title>DVD - Listing</title> <link rel="stylesheet" href="dvdstyle.css" type="text/css"> <script language="JavaScript" src="dvdrentals.js"></script> </head> <body> <form name="mainForm" method="post" action=<edit.php>> <input type="hidden" name="command" value="view"> <input type="hidden" name="transaction_id" value="<?printf(transactionId);?>" size="50"> <p></p> <table cellspacing="0" cellPadding="0" width="619" border="0"> <tr> <td> <table height="20" cellspacing="0" cellPadding="0" width="619" bgcolor="#bed8e1" border="0"> <tr align=left> <td valign="bottom" width="400" class="title"> DVD Transaction </td> <td align="right" width="219" class="title"> </td> </tr> </table> <br> <?if(strlen(<table cellspacing="2" cellPadding="2" width="619" border="0"> <tr> <td width="619" class="error"> <?printf(error);?> </td> </tr> </table> <?}?> <table cellspacing="2" cellPadding="2" width="619" border="0"> <tr> <td width="250" class="heading">Order</td> <td class="item"> <select name="order_id"> <option value="-1">Select Order</option> <? // Retrieve data to populate drop-down list // Execute the query result = mysql_query(link); if (!<br>".selectSql); // Loop through the results while(result)) { // Assign returned values to the variables row["OrderID"]; row["CustFN"]; row["CustLN"]; // Format the data for display custFirstName != null) custFirstName . " "; if(customerName .= // If the order id matches the existing value, mark it as selected if(orderId1 != <option value="<?printf(orderId1)?>"><?printf(<?printf(customerName);?></option> <? } else { ?> <option selected value="<?printf(<?printf(orderId1)?> - <?printf(</option> <? } } ?> </select> </td> </tr> <tr> <td class="heading">DVD</td> <td class="item"> <select name="dvd_id"> <option value="-1">Select DVD</option> <? // Retrieve data to populate drop-down list selectSql = "SELECT DVDID, DVDName FROM DVDs ORDER BY DVDName"; selectSql, result) die("Invalid query: " . mysql_error(). "<br>".row = mysql_fetch_array(dvdId1 = dvdName = dvdName == null) dvdId1 != <option value="<?printf(dvdId1);?>"><?printf(</option> <? } else { ?> <option selected value="<?printf(dvdId1);?>"><?printf(</option> <? } } ?> </select> </td> </tr> <tr> <td class="heading">Date Out</td> <td class="item"> <input type="text" name="date_out" value="<?printf(dateOut);?>" size="50"> </td> </tr> <tr> <td class="heading">Date Due</td> <td class="item"> <input type="text" name="date_due" value="<?printf(</td> </tr> <?if((strcmp("add", command) != 0) && (strcmp("savenew", <tr> <td class="heading">Date In</td> <td class="item"> <input type="text" name="date_in" value="<?printf(dateIn);?>" size="50"> </td> </tr> <?}?> <tr> <td colspan="2" class="item" align="center"> <table cellspacing="2" cellPadding="2" width="619" border="0"> <tr> <td align="center"> <?if((strcmp("add", command) == 0)){?> <input type="button" value="Save" class="add" onclick="doSave(this, 'savenew')"> <?}else{?> <input type="button" value="Save" class="add" onclick="doSave(this, 'save')"> <?}?> </td> <td align="center"> <input type="button" value="Cancel" class="add" onclick="doCancel(this)"> </td> </tr> </table> </td> </tr> </table> </form> </body> </html> <? // Close connection mysql_close(
(C) Æliens 20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.