HOME and CODE

PHP & MySQL Home
secure.php
nav.php
notes.php
Download Tutorial

DATABASE

Create Database
Drop Database
Re-Create All

TABLE

Create Table
Drop Table
Add Record
Update Record
Delete Record
Search
View all Records

MORE PHP

PHP Console Apps
PHP Web Apps

PHP and MySQL - UPDATE customer

<!-- updateData.php -->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META name="description" content="Tutorial for Linux, Apache, MySQL, SQL and PHP - LAMP">
<META name="keywords" content="tutorial, linux, apache, mysql, php, code, sql, lamp, lesson, lessons, example, examples, beginner, beginners, beginners' beginner's">
<title>Neil's Address Book</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>
<table border="1" cellspacing="0" cellpadding="4">
  <tr valign="top"> 
    <td><?php include "nav.php"; ?></td>
    <td>
<h1>PHP and MySQL - UPDATE customer</h1>

<?php 
  require "../secure.php";    // Contains host, username and password
  include "updateCode.php";
  
  // TEST FOR THE PAGE LOADED WITHOUT ANY POST DATA
  if (count($_POST) == 0)
  {
    // DISPLAY ALL THE RECORDS FOR ALTER CHOICE TO BE MADE
    updateData();
  }
  // TEST FOR A RECORD SELECTED
  else if (isset($_POST[radiobutton]))
  {
    // SELECT AND DISPLAY SINGLE RECORD
    $sql  = "SELECT * FROM customer WHERE customer_id = \"$_POST[radiobutton]\"";
    $rows = mysql_query($sql, $link);  // Retrieve the matching row
    $row  = mysql_fetch_array($rows, MYSQL_ASSOC);  
    echo "<hr><p>$sql</p><hr>"; // ===================================================== ?>

<form name="form1" method="post" action="">
  <input name="customer_id" type="hidden" id="customer_id" value="<?php echo $row[customer_id];  ?>">
  <table border="1" cellspacing="0" cellpadding="2">
    <tr> 
      <td><div align="right">title</div></td>
      <td><input name="title" type="text" id="title" value="<?php echo $row[title];  ?>"></td>
    </tr>
    <tr> 
      <td><div align="right">last_name</div></td>
      <td><input name="last_name" type="text" id="last_name" value="<?php echo $row[last_name];  ?>"></td>
    </tr>
    <tr> 
      <td><div align="right">first_name</div></td>
      <td><input name="first_name" type="text" id="first_name" value="<?php echo $row[first_name];  ?>"></td>
    </tr>
    <tr> 
      <td><div align="right">date_of_birth</div></td>
      <td><input name="date_of_birth" type="text" id="date_of_birth" value="<?php echo $row[date_of_birth];  ?>"></td>
    </tr>
    <tr> 
      <td colspan="2"><div align="right"> 
        <input name="Update" type="submit" id="Update" value="Update Record"></div>
      </td>
    </tr>
  </table>
</form>

<?php // ===============================================================================
  }
  // TEST FOR THE Update BUTTON BEING PRESSED
  else if (isset($_POST[Update]))
  {
    // UPDATE RECORD
    $sql = "UPDATE customer SET 
              title         = \"$_POST[title]\", 
              last_name     = \"$_POST[last_name]\", 
              first_name    = \"$_POST[first_name]\", 
              date_of_birth = \"$_POST[date_of_birth]\"
            WHERE 
              customer_id = \"$_POST[customer_id]\"";
      mysql_query($sql, $link);
      echo "<hr><p>$sql</p><hr>";
      updateData();
  }

  dump_page(basename($_SERVER[SCRIPT_FILENAME]));  // Display the code of this page
  dump_page("updateCode.php");
?>

    </td>
  </tr>
</table>

</body>
</html>
    
        

<!-- updateCode.php -->

<?php
  // ========================================================================
  // ===== updateCode.php ===================================================
  // ========================================================================
  // The updateData() function displays the SQL query results in a table
  // This function extracts the column names and uses them as table headings
  // In addition to displaying all the rows, it displays a radio button with
  // a checked value the same as the record primary key value.
  // ========================================================================
  function updateData()
  {
    require "../secure.php";    // Contains host, username and password

    // CONNECT TO MySQL
    $link = mysql_connect($host,        // Host name
                          $user,        // User name 
                          $password)    // User password
            or die("<h2>Could not connect: </h2>" . mysql_error());

    // USE DATABASE $database
    if (mysql_select_db ($database))
    { 
      // SQL SELECT QUERY - RESULTS GO INTO $rows
      $sql = "SELECT * FROM customer ORDER BY last_name, first_name";
      $rows = mysql_query($sql, $link) or die(mysql_error());
      // Count the rows in the table.
      $numrows = mysql_num_rows($rows);

      if ($numrows > 0)
      {
        echo "<form name=\"form1\" method=\"post\" action=\"\">\n";
        echo "<table border=\"1\" cellpadding=\"2\" cellspacing=\"0\">\n";
			
        // DISPLAY $rows IN A TABLE WHILE THERE ARE ANY LEFT TO DO
        while ($row = mysql_fetch_array($rows, MYSQL_ASSOC)) 
        {
          // DISPLAY THE TABLE COLUMN HEADINGS (ONLY ONCE)
          if (! isset($keys))   // Test to see if it has been done already
          {
            // EXTRACT THE COLUMN NAMES FROM $row AND DISPLAY THEN IN A TABLE ROW
            $keys = array_keys($row);
            echo "<tr><td><strong>update</strong></td>\n";  // Additional Delete column
            foreach ($keys as $value) 
            {
              echo "<td><strong>$value</strong></td>\n";
            }
            echo "</tr>\n";
          }
		
          // DISPLAY THE ROW DATA FROM each $row IN A TABLE ROW		
          $ii = 0;
          foreach ($row as $value) 
          {
            // Additional Delete Column (only do it once - That is what $ii is doing.)
            if ($ii == 0)
            {
              $ii++;
	      echo "<tr><td align=\"center\"><input type=\"radio\" name=\"radiobutton\" value=\"$value\"></td>\n";
            }
		  
	    if ($value == '')
	    {
	      echo "<td><p>&nbsp;</p></td>\n";
	    }
	    else
	    {
	      echo "<td><p>$value</p></td>\n";
	    }
          }
          echo "</tr>\n";
        }
        // Display the Selecte button.
	// $n is used to control the colspan
        $n = count($keys) + 1;
	echo "<tr>\n";
        echo "<td align=\"right\" colspan=\"" . $n . "\"><input type=\"submit\"\n";
        echo "   name=\"Select\" value=\"Select\"></td>\n";
        echo "</tr>\n";
	echo "</table>";
	echo "</form>";
      }
    }
  
    mysql_close($link);
  }
  // ========================================================================
?>