INTRODUCTION

PHP Web Home
Server Setup
nav.php
code.php
Download Tutorial

LESSONS

01 Hello World!
02 Text I/O
03 Arithmetic
04 Date and Time
05 Image Creation
06 Pie Chart
07 Array and Records
08 Delete a Record
09 Shopping
10 Shop Checkout
11 MM Bingo

MORE PHP

PHP Console Apps
PHP and MySQL

08 - Delete a Record Using PHP

Blank Last Name - Table not updated.

First Name:
Last Name:
Date of Birth yyyy-mm-dd:

Record count = 0

Delete Key =

Delete

Record #

First Name

Last Name

Date of Birth

Homework: For high fliers only: Upgrade this example to delete many records in one step. Use checkboxes instead of radio buttons.


<!-- Here is the page source code for 08delete.php -->

<?php
  include "code.php";    // Contains the function used to display
                         // the source code of web pages.

  session_start();
  header("Cache-control: private"); //IE 6 Fix
  session_register('myArray');
  $myArray = $_SESSION['myArray'];

  $fname = trim($_POST['textfield1']);    // First name
  $lname = trim($_POST['textfield2']);    // Last name
  $dob   = trim($_POST['textfield3']);    // Date Of Birth
  $rb    = trim($_POST['radiobutton']);   // Delete Key
  
  class Person
  {
    // CLASS DATA IS SPECIFIED HERE
    var $fname;    // First name
    var $lname;    // Last name
    var $dob;      // Date Of Birth

    // INPUT - SET THE FIRST NAME
    function setFirstName($newFirstName)
    { 
      $this->fname = trim($newFirstName);
    }

    // INPUT - SET THE LAST NAME
    function setLastName($newLastName)
    {
      $this->lname = trim($newLastName);
    }

    // INPUT - SET THE DOB
    function setDOB($newDOB)
    {
      $this->dob = trim($newDOB);
    }

    // OUTPUT FROM THE CLASS
    function displayData()
    {
      return "<td>$this->fname</td><td>$this->lname</td><td>$this->dob</td>";
    }
  }
  
  if ($lname == "")
  {
    $message =  "<p><strong>Blank Last Name - Table not updated.</strong></p>\n";
  }
  else
  {
    // CREATE A NEW Person OBJECT AND 
    $person = new Person();
    $person->setFirstName($fname);
    $person->setLastName($lname);
    $person->setDOB($dob);
    $message =  "<p><strong>Created a new Person.</strong></p>\n";
	
    // ADD THE NEW OBJECT TO THE NEXT FREE ARRAY LOCATION
    $myArray[] = $person;

    $_SESSION['myArray'] = $myArray;
  }
	
  // DELETE AN ARRAY ITEM IF A RADIOBUTTON WAS CHECKED
  if ($rb <> "")
  {
    unset($myArray[$rb]);              // This removes the item indexed by $rb
    $_SESSION['myArray'] = $myArray;   // Save new array into session variable.
  }

  $count = count($myArray);  // The number of items in the array

?>



<html>
<head>
<title>Delete a Record Using PHP</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" cellpadding="4" cellspacing="0">
<tr valign="top">
<td><?php include "nav.php"; ?>
</td>
<td>
      <h1>08 - Delete a Record Using PHP</h1>

      <?php echo $message; ?>
      
<form name="form1" method="post" action="08delete.php">
        <table border="1" cellspacing="0" cellpadding="4">
    <tr> 
      <td><div align="right">First&nbsp;Name:</div></td>
      <td> <input type="text" name="textfield1"> </td>
    </tr>
    <tr> 
      <td><div align="right">Last&nbsp;Name:</div></td>
      <td> <input type="text" name="textfield2"> </td>
    </tr>
    <tr> 
      <td><div align="right">Date&nbsp;of&nbsp;Birth yyyy-mm-dd:</div></td>
      <td> <input type="text" name="textfield3"> </td>
    </tr>
    <tr> 
      <td colspan="2"><div align="right"> 
        <input type="submit" name="Submit" value="Submit">
        </div>
      </td>
    </tr>
  </table>
</form>

<?php
  echo "<p>Record count = $count</p>\n";
  echo "<p>Delete Key = $rb</p>\n";

  // Display the data in a form with radio buttons used to select an item to be deleted.
  echo "<form name=\"form2\" method=\"post\" action=\"08delete.php\">\n";

  echo "<table border=\"1\" cellpadding=\"4\" border=\"1\" cellspacing=\"0\">\n";
    
  echo "<tr><td><h3>Delete</h3></td>\n";
  echo "<td><h3>Record #</h3></td>\n";
  echo "<td><h3>First Name</h3></td>\n";
  echo "<td><h3>Last Name</h3></td>\n";
  echo "<td><h3>Date of Birth</h3></td></tr>\n";
	
  // foreach iterates through every array element even if the elements are not numbered sequentially
  // on each iteration, $key is set to the array key
  // on each iteration, $value is set to the item stored at the key position
  if (isset($myArray) )
  {
    foreach($myArray as $key => $value)
    {
      // Display a table row
      echo "<tr><td><input name=\"radiobutton\" type=\"radio\" value=\"$key\"></td>\n";
      echo "<td align=\"center\">$key</td>".$value->displayData()."</tr>\n";
    }
  }

  // Display the delete button
  echo "<tr><td colspan=\"5\" align=\"right\">\n";
  echo "<input type=\"submit\" name=\"Delete\" value=\"Delete\">\n";
  echo "</td></tr>\n";
	
  echo "</table>\n";

  echo "</form>\n";
?>

      <p><strong><font color="#FF0000">Homework:</font></strong> For high fliers 
        only: Upgrade this example to delete many records in one step. Use checkboxes 
        instead of radio buttons.</p>

  <?php dump_page("08delete.php"); ?>
</td>
</tr>
</table>

</body>
</html>