HOME and CODE

Site Home
LAMP Home
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 - DELETE FROM customer WHERE

SELECT * FROM customer ORDER BY last_name, first_name

delete
customer_id
title
last_name
first_name
date_of_birth

1

Mr

Bloggs

Fred

1999-11-11

4

Mr

Doe

John

1982-08-02

5

Mr

Jane

Anne

1982-07-24

2

Ms

Jane

Mary

1986-04-21

3

Mr

Pompies

Piet

1966-01-31

<!-- deleteData.php -->

<?php 
  require "../secure.php";    // Contains host, username and password
	
  // Code to delete selected records ...
  $link = mysql_connect($host,        // Host name
                        $user,        // User name 
                        $password)    // User password
    or die("<h5>Could not connect: </h5>" . mysql_error());
  
  if (mysql_select_db ($database))
  {
    foreach ($_POST as $key => $value) 
    {
      if ($key != "Submit")
      {
        $sql = "DELETE FROM customer WHERE customer_id = \"$value\""; 
	mysql_query($sql, $link);
	$allSqls[] = $sql;      // Add the SQL statement to an array of them.
      }	  
    }
  }
  mysql_close($link);
?>  

<!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>PHP and MySQL - DELETE FROM customer WHERE</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 - DELETE FROM customer WHERE</h1>

<?php 
  if (isset($allSqls)) 
  {
    foreach ($allSqls as $value) 
    {
      echo "<p>$value</p>\n";
    }
  }  

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

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

</body>
</html>
    
        

<!-- deleteCode.php -->

<?php
  // ========================================================================
  // ===== deleteCode.php ===================================================
  // ========================================================================
  // The deleteData() function displays the SQL query results in a table
  // This function extracts the column names and uses them as table headings
  // It should work for any number of rows or columns
  // It will not be efficient if there are many rows or columns
  // In addition to displaying all the rows, it displays a check box with a
  // checked value the same as the record primary key value.
  // ========================================================================
  function deleteData()
  {
    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());
      echo "<p>".$sql."</p>\n";

      // 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><h5>delete</h5></td>\n";  // Additional Delete column
            foreach ($keys as $value) 
            {
              echo "<td><h5>$value</h5></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=\"checkbox\" name=\"$value\"\n";
              echo "  value=\"$value\"></td>\n";
            }
		  
            echo "<td><p>$value</p></td>\n";
          }
          echo "</tr>\n";
        }
        // Display the Delete Selected submit 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=\"Submit\" value=\"Delete Selected\"></td>\n";
        echo "</tr>\n";
	echo "</table>";
	echo "</form>";
      }
    }
  
    mysql_close($link);
  }
  // ========================================================================
?>