this print for content only—size & color not accurate spine = 1.383" 736 page count Books for professionals By professionals ® Beginning PHP and MySQL E-Commerce: From Novice to Professional, SECOND EDITION Dear Reader, The PHP language and the MySQL database have long offered an ideal blend of practicality and power for the novice and experienced programmer alike. This book shows you how to take advantage of this powerful duo to build a fully featured, modern, search engine–optimized web site. We guide you through the entire design and build process, so you’ll create a professionally developed application that allows for the ongoing integration of new features in an orga- nized manner. With each chapter, you’ll learn how to develop and deploy an online product catalog complete with a shopping cart, checkout mechanism, product search, dynamic product recommendations, administrative features, AJAX and search engine optimization features, customer accounts, product reviews, online order management system, and much more. You’ll also learn how to process electronic payments by integrating popular payment services such as PayPal, DataCash, and Authorize.net. With each new feature, you’ll be introduced to fresh challenges and theoretical concepts, which are all thoroughly explained. Along the way, you will gain an intimate understanding of every piece of code you write, which will enable you to build your own powerful and flexible web sites efficiently and rapidly with PHP and MySQL. Have fun reading our book! Cristian and Emilian Cristian Darie, author of Professional Search Engine Optimization with PHP AJAX and PHP: Building Responsive Web Applications US $46.99 Shelve in PHP/E-Commerce/ Web Development User level: Beginner–Intermediate SECOND EDITION The eXperT’s Voice ® in open source Beginning PHP and MySQL E-Commerce From Novice to Professional SECOND EDITION cyan MaGenTa yelloW Black panTone 123 c Cristian Darie and Emilian Balanescu Companion eBook Available THE APRESS ROADMAP The Definitive Guide to MySQL 5, Third Edition Beginning PHP and MySQL, Third Edition Beginning PHP and MySQL E-Commerce, Second Edition Beginning Ajax with PHP Practical Web 2.0 Applications with PHP Pro MySQL Pro PHP Security www.apress.com SOURCE CODE ONLINE Companion eBook See last page for details on $10 eBook version ISBN-13: 978-1-59059-864-1 ISBN-10: 1-59059-864-4 9 781590 598641 5 4 6 9 9 This practical PHP and MySQL tutorial will teach you how to successfully design and build fully featured e-commerce web sites. Emilian Balanescu, author of Beginning PHP and PostgreSQL E-Commerce: From Novice to Professional Beginning PHP and MySQL E-Commerce Darie, Balanescu www.it-ebooks.info www.it-ebooks.info Cristian Darie and Emilian Balanescu Beginning PHP and MySQL E-Commerce From Novice to Professional SECOND EDITION 8644ch00FMFINAL.qxd 1/30/08 12:35 PM Page i www.it-ebooks.info Beginning PHP and MySQL E-Commerce: From Novice to Professional, Second Edition Copyright © 2008 by Cristian Darie and Emilian Balanescu All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-59059-864-1 ISBN-10 (pbk): 1-59059-864-4 ISBN-13 (electronic): 978-1-4302-0291-2 ISBN-10 (electronic): 1-4302-0291-2 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Java™ and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc., in the US and other countries. Apress, Inc., is not affiliated with Sun Microsystems, Inc., and this book was writ- ten without endorsement from Sun Microsystems, Inc. Lead Editors: Jason Gilmore and Tom Welsh Technical Reviewers: Bogdan Brinzarea-Iamandi, Sharon Dempsey, Audra Hendrix Editorial Board: Clay Andres, Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Kevin Goff, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Senior Project Manager: Tracy Brown Collins Copy Editors: Heather Lang, Kim Wimpsett Associate Production Director: Kari Brooks-Copony Production Editor: Laura Esterman Compositor: Kinetic Publishing Services, LLC Proofreader: Liz Welch Indexer: Broccoli Information Management Artists: April Milne, Kinetic Publishing Services, LLC Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com. 8644ch00FMFINAL.qxd 1/30/08 12:35 PM Page ii www.it-ebooks.info 8644ch00FMFINAL.qxd 1/30/08 12:35 PM Page iii www.it-ebooks.info Contents at a Glance About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Technical Reviewers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi PART 1 ■ ■ ■ Phase I of Development ■CHAPTER 1 Starting an E-Commerce Site . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 ■CHAPTER 2 Laying Out the Foundations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 ■CHAPTER 3 Starting the TShirtShop Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 ■CHAPTER 4 Creating the Product Catalog: Part 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 ■CHAPTER 5 Creating the Product Catalog: Part 2. . . . . . . . . . . . . . . . . . . . . . . . . . . 113 ■CHAPTER 6 Product Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 ■CHAPTER 7 Search Engine Optimization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 ■CHAPTER 8 Searching the Catalog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 ■CHAPTER 9 Receiving Payments Using PayPal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 ■CHAPTER 10 Catalog Administration: Departments and Categories . . . . . . . . . . 267 ■CHAPTER 11 Catalog Administration: Products and Attributes . . . . . . . . . . . . . . . 305 PART 2 ■ ■ ■ Phase II of Development ■CHAPTER 12 Creating Your Own Shopping Cart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 ■CHAPTER 13 Implementing AJAX Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399 ■CHAPTER 14 Accepting Customer Orders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431 ■CHAPTER 15 Product Recommendations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461 iv 8644ch00FMFINAL.qxd 1/30/08 12:35 PM Page iv www.it-ebooks.info PART 3 ■ ■ ■ Phase III of Development ■CHAPTER 16 Managing Customer Details. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477 ■CHAPTER 17 Storing Customer Orders. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543 ■CHAPTER 18 Implementing the Order Pipeline: Part 1 . . . . . . . . . . . . . . . . . . . . . . . 569 ■CHAPTER 19 Implementing the Order Pipeline: Part 2 . . . . . . . . . . . . . . . . . . . . . . . 593 ■CHAPTER 20 Processing Credit Card Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . 623 ■CHAPTER 21 Product Reviews. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655 ■CHAPTER 22 Using Amazon.com Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665 ■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689 8644ch00FMFINAL.qxd 1/30/08 12:35 PM Page v 98bed1a7be82c4ab97516c3da3c8c4e2 www.it-ebooks.info 8644ch00FMFINAL.qxd 1/30/08 12:35 PM Page vi www.it-ebooks.info Contents About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Technical Reviewers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi PART 1 ■ ■ ■ Phase I of Development ■CHAPTER 1 Starting an E-Commerce Site . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Deciding Whether to Go Online . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Get More Customers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Encourage Customers Spend More . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Reduce the Costs of Fulfilling Orders. . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Let’s Make Money . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Risks and Threats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Designing for Business. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Phase I: Getting a Site Up. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Phase II: Creating Your Own Shopping Cart. . . . . . . . . . . . . . . . . . . . . . 8 Phase III: Processing Orders and Adding Features. . . . . . . . . . . . . . . . 8 TShirtShop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 ■CHAPTER 2 Laying Out the Foundations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Designing for Growth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Meeting Long-Term Requirements with Minimal Effort . . . . . . . . . . 14 The Magic of the Three-Tier Architecture . . . . . . . . . . . . . . . . . . . . . . 15 Choosing Technologies and Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Using PHP to Generate Dynamic Web Content . . . . . . . . . . . . . . . . . . 20 Using Smarty to Separate Layout from Code . . . . . . . . . . . . . . . . . . . 21 Using MySQL to Store Web Site Data. . . . . . . . . . . . . . . . . . . . . . . . . . 23 MySQL and the Three-Tier Architecture. . . . . . . . . . . . . . . . . . . . . . . . 26 Choosing Naming and Coding Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 vii 8644ch00FMFINAL.qxd 1/30/08 12:35 PM Page vii www.it-ebooks.info ■CHAPTER 3 Starting the TShirtShop Project. . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Getting a Code Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Installing XAMPP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Preparing the tshirtshop Alias . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Installing Smarty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Implementing the Site Skeleton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Building TShirtShop’s Front Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Handling and Reporting Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Preparing the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Downloading the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 ■CHAPTER 4 Creating the Product Catalog: Part 1 . . . . . . . . . . . . . . . . . . . . . . 63 Showing Your Visitors What You’ve Got . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 What Does a Product Catalog Look Like? . . . . . . . . . . . . . . . . . . . . . . 64 Previewing the Product Catalog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Roadmap for This Chapter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Storing Catalog Information. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Understanding Data Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Creating the department Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Communicating with the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 The Structured Query Language (SQL) . . . . . . . . . . . . . . . . . . . . . . . . 79 MySQL Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Adding Logic to the Site . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Connecting to MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Writing the Business Tier Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Displaying the List of Departments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Using Smarty Plug-ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Creating the Link Factory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 ■CHAPTER 5 Creating the Product Catalog: Part 2 . . . . . . . . . . . . . . . . . . . . . 113 Storing the New Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 What Makes a Relational Database . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Creating and Populating the New Data Tables . . . . . . . . . . . . . . . . . 120 Adding Products and Relating Them to Categories . . . . . . . . . . . . . 122 Using Database Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Querying the New Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Getting Short Descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 ■CONTENTSviii 8644ch00FMFINAL.qxd 1/30/08 12:35 PM Page viii www.it-ebooks.info [...]... most of the necessary concepts and guides you through all the implementation steps, but it’s assumed that you have some basic knowledge of building web sites with PHP and MySQL W Jason Gilmore’s Beginning PHP and MySQL: From Novice to Professional, Second Edition (Apress, 2006) can provide this foundation knowledge for you The code in this book has been tested with PHP 5 and MySQL 5 The code is not compatible... www.it-ebooks.info 8644ch00FMFINAL.qxd 1/30/08 12:35 PM Page xxi Introduction W elcome to the second edition of Beginning PHP and MySQL E-Commerce: From Novice to Professional! This book is a practical, step-by-step PHP and MySQL tutorial that teaches you real-world development practices Guiding you through every step of the design and build process, this tutorial will teach you how to create high-quality, fully featured,... topics and showing you how to • Build a product catalog that can be browsed and searched • Implement the catalog administration pages that allow adding, modifying, and removing products, categories, and departments, and other administrative features • Create your own shopping basket and check-out mechanism in PHP • Increase sales by implementing product recommendations and product reviews • Handle payments... Heather Lang and Kim Wimpsett for their wonderful edits, which somehow made our copy sound like it was written by someone who actually knows English (and knows it well!) Laura Esterman and the rest of the production team for transforming the documents we’ve written and the graphics we’ve submitted into the book that you hold in your hands right now Bogdan Brinzarea-Iamandi, Sharon Dempsey, and Audra Hendrix... The code is not compatible with older versions of PHP, which lack the object-oriented programming (OOP) support required to implement the presented code architecture Information regarding the compatibility with newer versions of PHP and MySQL will be kept updated on the book’s support page at Cristian Darie’s web site at http://www.cristiandarie.ro /php- mysql- ecommerce-2/ How This Book Is Structured This... adding product review functionality We hope you’ll enjoy reading our book, and that you’ll find it useful and relevant to your development projects! xxi www.it-ebooks.info 8644ch00FMFINAL.qxd xxii 1/30/08 12:35 PM Page xxii ■INTRODUCTION Who This Book Is For Beginning PHP and MySQL E-Commerce: From Novice to Professional, Second Edition is aimed at developers looking for a tutorial approach to building... ■BOGDAN BRINZAREA-IAMANDI has a strong background in computer science, holding a master’s and bachelor’s degree from the Automatic Control and Computers Faculty of the Politehnica University of Bucharest, Romania, and another master’s degree from the computer science department of Ecole Polytechnique in Paris, France His main interests are new web technologies and distributed and mobile computing ■SHARON... Paris and working as an independent consultant Fluent in French, she focused her expertise on needs assessment, application development and deployment, and future growth planning for a variety of import/export and retail system clientele She was educated at Northwestern University in Evanston, Illinois, and began her computer career with Hewlett-Packard She currently consults as development advisor and. .. possible By the end, you’ll understand the concepts, and you’ll have the knowledge to create your own powerful web sites Owners of the first edition will find that a large part of the book has been rewritten and many features have been added, as a result of the advances in the web development scene and the extensive feedback we’ve received from the readers of the first edition Now, you’ll find the book... PayPal one we’ll implement initially, and you’ll add AJAX features to your site In the third phase, we’ll show you how to increase your profit margins by reducing costs through automating and streamlining order processing and administration and by handling credit card transactions yourself You also learn how to integrate external functionality through web services and improve your customer’s shopping . Guide to MySQL 5, Third Edition Beginning PHP and MySQL, Third Edition Beginning PHP and MySQL E-Commerce, Second Edition Beginning Ajax with PHP Practical. professionals ® Beginning PHP and MySQL E-Commerce: From Novice to Professional, SECOND EDITION Dear Reader, The PHP language and the MySQL database have