Index
    Preface
      What This Book Is About
      What You Need to Know
      How This Book Is Organized
      How to Use This Book
      Conventions Used in This Book
      Using Code Examples
      How to Contact Us
      Web Site and Code Examples
      Acknowledgments
      Chapter 1.  Database Applications and the Web
      Section 1.1.  The Web
      Section 1.2.  Three-Tier Architectures
      Chapter 2.  The PHP Scripting Language
      Section 2.1.  Introducing PHP
      Section 2.2.  Conditions and Branches
      Section 2.3.  Loops
      Section 2.4.  Functions
      Section 2.5.  Working with Types
      Section 2.6.  User-Defined Functions
      Section 2.7.  A Working Example
      Chapter 3.  Arrays, Strings, and Advanced Data Manipulation in PHP
      Section 3.1.  Arrays
      Section 3.2.  Strings
      Section 3.3.  Regular Expressions
      Section 3.4.  Dates and Times
      Section 3.5.  Integers and Floats
      Chapter 4.  Introduction to Object-Oriented Programming with PHP 5
      Section 4.1.  Classes and Objects
      Section 4.2.  Inheritance
      Section 4.3.  Throwing and Catching Exceptions
      Chapter 5.  SQL and MySQL
      Section 5.1.  Database Basics
      Section 5.2.  MySQL Command Interpreter
      Section 5.3.  Managing Databases and Tables
      Section 5.4.  Inserting, Updating, and Deleting Data
      Section 5.5.  Querying with SQL SELECT
      Section 5.6.  Join Queries
      Section 5.7.  Case Study: Adding a New Wine
      Chapter 6.  Querying Web Databases
      Section 6.1.  Querying a MySQL Database Using PHP
      Section 6.2.  Processing User Input
      Section 6.3.  MySQL Function Reference
      Chapter 7.  PEAR
      Section 7.1.  Overview
      Section 7.2.  Core Components
      Section 7.3.  Packages
      Chapter 8.  Writing to Web Databases
      Section 8.1.  Database Inserts, Updates, and Deletes
      Section 8.2.  Issues in Writing Data to Databases
      Chapter 9.  Validation with PHP and JavaScript
      Section 9.1.  Validation and Error Reporting Principles
      Section 9.2.  Server-Side Validation with PHP
      Section 9.3.  JavaScript and Client-Side Validation
      Chapter 10.  Sessions
      Section 10.1.  Introducing Session Management
      Section 10.2.  PHP Session Management
      Section 10.3.  Case Study: Using Sessions in Validation
      Section 10.4.  When to Use Sessions
      Section 10.5.  PHP Session API and Configuration
      Chapter 11.  Authentication and Security
      Section 11.1.  HTTP Authentication
      Section 11.2.  HTTP Authentication with PHP
      Section 11.3.  Form-Based Authentication
      Section 11.4.  Protecting Data on the Web
      Chapter 12.  Errors, Debugging, and Deployment
      Section 12.1.  Errors
      Section 12.2.  Common Programming Errors
      Section 12.3.  Custom Error Handlers
      Chapter 13.  Reporting
      Section 13.1.  Creating a Report
      Section 13.2.  Producing PDF
      Section 13.3.  PDF-PHP Reference
      Chapter 14.  Advanced Features of Object-Oriented Programming in PHP 5
      Section 14.1.  Working with Class Hierarchies
      Section 14.2.  Class Type Hints
      Section 14.3.  Abstract Classes and Interfaces
      Section 14.4.  Freight Calculator Example
      Chapter 15.  Advanced SQL
      Section 15.1.  Exploring with SHOW
      Section 15.2.  Advanced Querying
      Section 15.3.  Manipulating Data and Databases
      Section 15.4.  Functions
      Section 15.5.  Automating Querying
      Section 15.6.  Table Types
      Section 15.7.  Backup and Recovery
      Section 15.8.  Managing Users and Privileges
      Section 15.9.  Tuning MySQL
      Chapter 16.  Hugh and Dave's Online Wines:A Case Study
      Section 16.1.  Functional and System Requirements
      Section 16.2.  Application Overview
      Section 16.3.  Common Components
      Chapter 17.  Managing Customers
      Section 17.1.  Code Overview
      Section 17.2.  Customer Validation
      Section 17.3.  The Customer Form
      Chapter 18.  The Shopping Cart
      Section 18.1.  Code Overview
      Section 18.2.  The Winestore Home Page
      Section 18.3.  The Shopping Cart Implementation
      Chapter 19.  Ordering and Shipping at the Online Winestore
      Section 19.1.  Code Overview
      Section 19.2.  Credit Card and Shipping Instructions
      Section 19.3.  Finalizing Orders
      Section 19.4.  HTML and Email Receipts
      Chapter 20.  Searching and Authentication in the Online Winestore
      Section 20.1.  Code Overview
      Section 20.2.  Searching and Browsing
      Section 20.3.  Authentication
      Appendix A.  Linux Installation Guide
      Section A.1.  Finding Out What's Installed
      Section A.2.  Installation Overview
      Section A.3.  Installing MySQL
      Section A.4.  Installing Apache
      Section A.5.  Installing PHP
      Section A.6.  What's Needed for This Book
      Appendix B.  Microsoft Windows Installation Guide
      Section B.1.  Installation Overview
      Section B.2.  Installing with EasyPHP
      Section B.3.  What's Needed for This Book
      Appendix C.  Mac OS X Installation Guide
      Section C.1.  Getting Started
      Section C.2.  Installing MySQL
      Section C.3.  Setting Up Apache and PHP
      Section C.4.  What's Needed for This Book
      Appendix D.  Web Protocols
      Section D.1.  Network Basics
      Section D.2.  Hypertext Transfer Protocol
      Appendix E.  Modeling and Designing Relational Databases
      Section E.1.  The Relational Model
      Section E.2.  Entity-Relationship Modeling
      Appendix F.  Managing Sessions in theDatabase Tier
      Section F.1.  Using a Database to Keep State
      Section F.2.  PHP Session Management
      Section F.3.  MySQL Session Store
      Appendix G.  Resources
      Section G.1.  Client Tier Resources
      Section G.2.  Middle-Tier Resources
      Section G.3.  Database Tier Resources
      Section G.4.  Security and Cryptography Resources
      Appendix H.  The Improved MySQL Library
      Section H.1.  New Features
      Section H.2.  Getting Started
      Section H.3.  Using the New Features
    Colophon
    Copyright



 

Previous Section  < Day Day Up >  Next Section

5.7 Case Study: Adding a New Wine

In this section, we show you an example that combines some of the statements we've discussed in this chapter, and shows you the basics of writing data to databases.

In this example, let's insert a new wine into the database using the MySQL command-line interpreter. Let's suppose that 24 bottles of a new wine, a Curry Cabernet Merlot 1996 made by Rowley Brook Winery, have arrived, and you wish to add a row to the database for the new wine. This new wine costs $14.95 per bottle.

The addition has several steps, the first of which is to find out the next available wine_id. You need to do this because we're not using the MySQL-proprietary auto_increment feature in the winestore database. Here's the query:

SELECT max(wine_id) FROM wine;

This reports:

+--------------+

| max(wine_id) |

+--------------+

|         1048 |

+--------------+

1 row in set (0.00 sec)

Now, we can use an INSERT INTO statement to create the basic row for the wine in the wine table:

INSERT INTO wine SET wine_id=1049, wine_name='Curry Hill', year=1996,

  description='A beautiful mature wine. Ideal with red meat.';

This creates a new row and sets the basic attributes. The wine_id is set to the 1048 + 1 = 1049. The remaining attributes (the wine_type identifier, the winery_id identifier, and the varieties in the wine_variety table) require further querying and then subsequent updates.

The second step is to set the winery_id for the new wine. We need to search for the Rowley Brook Winery winery to identify the winery_id:

SELECT winery_id FROM winery WHERE winery_name='Rowley Brook Winery';

The result returned is:

+-----------+

| winery_id |

+-----------+

|       298 |

+-----------+

1 row in set (0.00 sec)

We can now update the new wine row to set the winery_id=298:

UPDATE wine SET winery_id = 298 WHERE wine_id = 1049;

The third step is similar to the second, and is to set the wine_type identifier in the wine table. You can discover the wine_type_id for a Red wine using:

SELECT wine_type_id FROM wine_type WHERE wine_type = "Red";

This reports that:

+--------------+

| wine_type_id |

+--------------+

|            6 |

+--------------+

1 row in set (0.01 sec)

Now, you can set the identifier in the wine table:

UPDATE wine SET wine_type = 6 WHERE wine_id = 1049;

The fourth step is to set the variety information for the new wine. We need the variety_id values for Cabernet and Merlot. These can be found with a simple query:

SELECT * FROM grape_variety;

In part, the following results are produced:

+------------+------------+

| variety_id | variety    |

+------------+------------+

|          1 | Riesling   |

|          2 | Chardonnay |

|          3 | Sauvignon  |

|          4 | Blanc      |

|          5 | Semillon   |

|          6 | Pinot      |

|          7 | Gris       |

|          8 | Verdelho   |

|          9 | Grenache   |

|         10 | Noir       |

|         11 | Cabernet   |

|         12 | Shiraz     |

|         13 | Merlot     |

Cabernet has variety_id=11 and Merlot variety_id=13. We can now insert two rows into the wine_variety table. Because Cabernet is the first variety, set its ID=1, and ID=2 for Merlot:

INSERT INTO wine_variety SET wine_id=1049, variety_id=11, id=1;

INSERT INTO wine_variety SET wine_id=1049, variety_id=13, id=2;

The final step is to insert the first inventory row into the inventory table for this wine. There are 24 bottles, with a per-bottle cost of $14.95:

INSERT INTO inventory SET wine_id=1049, inventory_id=1, on_hand=24, 

  cost=14.95, date_added="04/03/01";

We've finished inserting the wine into the database. Now, to conclude, let's retrieve the details of the wine to make sure everything is as it should be. We'll retrieve the wine name, its year, the winery, the varieties, the wine type, and its cost. Here's the query:

SELECT year, wine_name, winery_name, variety, wine_type.wine_type, cost

  FROM wine, winery, wine_variety, grape_variety, wine_type, inventory

  WHERE wine.wine_id = 1049 AND

  wine.wine_id = wine_variety.wine_id AND

  wine_variety.variety_id = grape_variety.variety_id AND

  wine.wine_type = wine_type.wine_type_id AND

  wine.winery_id = winery.winery_id AND

  wine.wine_id = inventory.wine_id 

  ORDER BY wine_variety.id;

The WHERE clause looks complicated, but it just joins together all of the tables in the FROM clause by matching up the identifier attributes and specifies we want for wine #1049. Here's the output:

+------+------------+---------------------+----------+-----------+-------+

| year | wine_name  | winery_name         | variety  | wine_type | cost  |

+------+------------+---------------------+----------+-----------+-------+

| 1996 | Curry Hill | Rowley Brook Winery | Cabernet | Red       | 14.95 |

| 1996 | Curry Hill | Rowley Brook Winery | Merlot   | Red       | 14.95 |

+------+------------+---------------------+----------+-----------+-------+

2 rows in set (0.01 sec)

Two rows are returned because there are two varieties for this wine in the wine_variety table.

We've now covered as much complex querying in SQL as we need for you to develop most web database applications. You'll find a discussion of advanced features you can use in Chapter 15. Beginning in the next chapter, we show you how to include SQL statements in PHP scripts to automate querying and build web database applications.

    Previous Section  < Day Day Up >  Next Section







    Copyright © 2010 | Domen maybe sale - bye this domen