topical media & game development

talk show tell print

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
  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\"<br>";
  
     if(dvdId ==  -1)
        error .= "Please select a \"DVD\"<br>";
  
     if((dateOut == null) || (strlen(dateOut) == 0))
        error .= "Please enter a \"Date Out\" Value<br>";
  
     if((dateDue == null) || (strlen(dateDue) == 0))
        error .= "Please enter a \"Date Due\" Value<br>";
  
     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(). "<br>".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 = "";
           }
        }
     }
  }
  ?>
  
  <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">&nbsp;</td>
  </tr>
  </table>
  <br>
  <?if(strlen(error) > 0){?>
  <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
     selectSql = "SELECT ".
                  "Orders.OrderID, ".
                  "Orders.CustID, ".
                  "Customers.CustFN, ".
                  "Customers.CustLN ".
                  "FROM Orders, Customers ".
                  "WHERE ".
                  "Customers.CustID = Orders.CustID ".
                  "ORDER BY Orders.OrderID DESC, ".
                  "Customers.CustLN ASC, Customers.CustFN ASC";
  
  // Execute the query
  result = mysql_query(selectSql, link);
  
  if (!result)
     die("Invalid query: " . mysql_error(). "<br>".selectSql);
  
  // Loop through the results
  while(row = mysql_fetch_array(result))
  {
  // Assign returned values to the variables
     orderId1 = row["OrderID"];
     custFirstName = row["CustFN"];
     custLastName = row["CustLN"];
  
  // Format the data for display
     customerName = "";
  
     if(custFirstName != null)
        customerName .= custFirstName . " ";
  
     if(custLastName != null)
        customerName .= custLastName;
  
  // If the order id matches the existing value, mark it as selected
     if(orderId1 != orderId)
     {
  ?>
        <option value="<?printf(orderId1)?>"><?printf(orderId1);?> - <?printf(customerName);?></option>
  <?
     }
     else
     {
  ?>
        <option selected value="<?printf(orderId1);?>"><?printf(orderId1)?> - <?printf(customerName);?></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";
  
  result = mysql_query(selectSql, link);
  
  if (!result)
     die("Invalid query: " . mysql_error(). "<br>".selectSql);
  
  while(row = mysql_fetch_array(result))
  {
     dvdId1 = row["DVDID"];
     dvdName = row["DVDName"];
  
     if(dvdName == null)
        dvdName = "";
  
     if(dvdId1 != dvdId)
     {
  ?>
           <option value="<?printf(dvdId1);?>"><?printf(dvdName);?></option>
  <?
     }
     else
     {
  ?>
           <option selected value="<?printf(dvdId1);?>"><?printf(dvdName);?></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(dateDue);?>" size="50">
     </td>
  </tr>
  <?if((strcmp("add", command) != 0) && (strcmp("savenew", command) != 0)){?>
  <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) || (strcmp("savenew", 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(link);
  ?>
  
  


(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.