cyan yelloW MaGenTa Black panTone 123 c Books for professionals By professionals® The eXperT’s Voice® in open source Companion eBook Available Beginning PHP and MySQL E-Commerce: From Novice to Professional, SECoNd EditioN Beginning Dear Reader, Beginning EP The PHP language and the MySQL database have long offered an ideal blend of practicality and power for the novice and experienced programmer alike. - H This book shows you how to take advantage of this powerful duo to build a fully C featured, modern, search engine–optimized web site. We guide you through Cristian Darie, author of the entire design and build process, so you’ll create a professionally developed P PHP MySQL o Professional Search Engine application that allows for the ongoing integration of new features in an orga- and Optimization with PHP nized manner. m AJAX and PHP: Building With each chapter, you’ll learn how to develop and deploy an online product a Responsive Web Applications catalog complete with a shopping cart, checkout mechanism, product search, n dynamic product recommendations, administrative features, AJAX and search m engine optimization features, customer accounts, product reviews, online d E-Commerce order management system, and much more. You’ll also learn how to process electronic payments by integrating popular e M payment services such as PayPal, DataCash, and Authorize.net. With each new feature, you’ll be introduced to fresh challenges and theoretical r concepts, which are all thoroughly explained. Along the way, you will gain an c intimate understanding of every piece of code you write, which will enable you y e to build your own powerful and flexible web sites efficiently and rapidly with From Novice to Professional PHP and MySQL. S Have fun reading our book! Emilian Balanescu, Q author of Cristian and Emilian Beginning PHP and PostgreSQL E-Commerce: L This practical PHP and MySQL tutorial will From Novice to Professional teach you how to successfully design and build THE APRESS ROADMAP Beginning PHP and Companion eBook MySQL E-Commerce, fully featured e-commerce web sites. Second Edition Beginning PHP and MySQL, Pro MySQL Third Edition SECoNd Beginning Ajax with PHP EditioN The Definitive Guide to Pro PHP Security SECoND EDitioN See last page for details MySQL 5, Third Edition Practical Web 2.0 on $10 eBook version Applications with PHP B a SOURCE CODE ONLINE ISBN-13: 978-1-59059-864-1 la Cristian Darie and Emilian Balanescu www.apress.com n ISBN-10: 1-59059-864-4 eD 54699 sa US $46.99 cri ue , Shelve in PHP/E-Commerce/ Web Development User level: 9 781590 598641 Beginner–Intermediate www.it-ebooks.info this print for content only—size & color not accurate spine = 1.383" 736 page count www.it-ebooks.info 8644ch00FMFINAL.qxd 1/30/08 12:35 PM Page i Beginning PHP and MySQL E-Commerce From Novice to Professional SECOND EDITION Cristian Darie and Emilian Balanescu www.it-ebooks.info 8644ch00FMFINAL.qxd 1/30/08 12:35 PM Page ii 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 atrademark symbol with every occurrence of atrademarked 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 [email protected], 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 [email protected], 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. www.it-ebooks.info 8644ch00FMFINAL.qxd 1/30/08 12:35 PM Page iii www.it-ebooks.info 8644ch00FMFINAL.qxd 1/30/08 12:35 PM Page iv 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 www.it-ebooks.info 8644ch00FMFINAL.qxd 1/30/08 12:35 PM Page v 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 98bed1a7be82c4ab97516c3da3c8c4e2 www.it-ebooks.info 8644ch00FMFINAL.qxd 1/30/08 12:35 PM Page vi www.it-ebooks.info 8644ch00FMFINAL.qxd 1/30/08 12:35 PM Page vii 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 www.it-ebooks.info 8644ch00FMFINAL.qxd 1/30/08 12:35 PM Page viii viii ■ CONTENTS ■ 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 www.it-ebooks.info
Description: