PHP and MySQL Library Tutorial

General
Site Home
LAMP Home
Database Home
Conect to MySQL
MySQL_Login.php
nav.php
dump.php

Clean Up resets the
database to contain
a few sensible
records.

Download Tutorial

Database
Create
Drop

Person Table
Create
Drop

Insert
Delete
Update
Search
Date-Search

Row Dump
Better View 
Table View 
Form View

Book Table
Create
Drop

Insert
Delete
Update
Search




Table View 
Form View

Loan Table
Create
Drop

Insert
Delete

Search




Table View

Documentation
Data Requirements
User Interface Design
Entity Relationship Diagrams
Data Flow Diagrams
Data Dictionary
Implementation Notes
Table Designs
Goals, Testing and Appraisal

Note - Create, Drop and Clean Up would be hidden on a real life system.
Contact - nbauers at samphire dot demon dot co dot uk
Copyright © - You may use and reproduce this material in a "not for profit" setting.

SELECT * FROM book ORDER BY book_title

Accession Number

Book Title

Author

Publisher

ISBN

2 Chapter House Dune Frank Herbert New English Library 9780450058867
3 Chapter House Dune Frank Herbert New English Library 9780450058867
1 Jack Holborn Leon Garfield Penguin Books 9780140303189
4 The Hollow Hills Mary Stewart Coronet Books 9780340186119

Code Listing : bookDelete.php

<?php
  require "../MySQL_Login.php";    // Contains host, username, password, and database
  $link   = mysql_connect($host, $user, $password) or die(mysql_error());
  mysql_select_db ($database) or die(mysql_error());
  
  // ========================================================================
  // This loop visits each item in the $_POST array.
  // The array items are not numbered. They are named instead. 
  // This is called an associative array.
  // The names of the array items are stored into $key.
  // The item stored in the named position is copied into $value.
  // If $key is not "Submit" then $value will contain a primary key value.
  // This information was posted when the form on this page was submitted. 
  // ========================================================================
  foreach ($_POST as $key => $value) 
  {
    if ($key != "Submit")
    {
      $sql       = "DELETE FROM book WHERE accession_num = \"$value\""; 
      $sqls[]    = $sql;  // Array of all the sql statements 
      mysql_query($sql, $link);
      $allSqls[] = $sql;      // Add the SQL statement to an array of them.
    }	  
  }  

  $sql    = "SELECT * FROM book ORDER BY book_title";
  $sqls[] = $sql;  // Array of all the sql statements 
  $result = mysql_query($sql, $link) or die(mysql_error());
  mysql_close($link);
?>

<html>
<head>
<title>Delete a Book</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<?php 
  include "nav.php";

  for ($ii = 0;  $ii < count($sqls);  $ii++)
  {
    echo "<font size=\"+2\"><b><pre>" . $sqls[$ii] . "</pre></b></font>\n";
  }
?>

<form name="form1" method="post" action="bookDelete.php">
  <table border="1" cellspacing="0" cellpadding="4">
    <tr>
      <td><h3>Accession Number</h3></td>
      <td><h3>Book Title</h3></td>
      <td><h3>Author</h3></td>
      <td><h3>Publisher</h3></td>
      <td><h3>ISBN</h3></td>
    </tr>
    
  <?php 
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC))   
    {
      echo "<tr>\n";
      echo "<td>" . $row['accession_num'] . "
            <input name=\"" . $row['accession_num'] . "\" 
	    type=\"checkbox\" id=\"" . $row['accession_num'] . "\" 
	    value=\"" . $row['accession_num'] . "\"></td>\n";
      echo "<td>" . $row['book_title'] . "</td>\n";
      echo "<td>" . $row['author']     . "</td>\n";
      echo "<td>" . $row['publisher']  . "</td>\n";
      echo "<td>" . $row['isbn']       . "</td>\n";
      echo "</tr>\n";
    }
  ?>
    
    <tr>
      <td colspan="5" align="right"><input type="submit" name="Submit" value="Submit">
    </td>
    </tr>
  </table>
</form>
      
<?php 
  include "dump.php";
  dump_page(basename($_SERVER[SCRIPT_FILENAME]));  // Display the code of this page
?>

</body>
</html>