You've reached the end of your free preview.
Want to read all 561 pages?
Unformatted text preview: Practical PHP 7,
MySQL 8, and MariaDB
Website Databases
A Simplified Approach to Developing
Database-Driven Websites
—
Second Edition
—
Adrian W. West
Steve Prettyman Practical PHP 7,
MySQL 8, and MariaDB
Website Databases
A Simplified Approach to Developing
Database-Driven Websites
Second Edition Adrian W. West
Steve Prettyman Practical PHP 7, MySQL 8, and MariaDB Website Databases: A Simplified Approach to Developing
Database-Driven Websites
Adrian W. West Steve Prettyman
Colyton, Devon, United Kingdom Key West, Florida, USA
ISBN-13 (pbk): 978-1-4842-3842-4 ISBN-13 (electronic): 978-1-4842-3843-1 Library of Congress Control Number: 2018957652
Copyright © 2018 by Adrian W. West and Steve Prettyman
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage
and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or
hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are
not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material
contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Steve Anglin
Development Editor: Matthew Moodie
Coordinating Editor: Mark Powers
Cover designed by eStudioCalamar
Cover image designed by Freepik ( )
Distributed to the book trade worldwide by Springer Science+Business Media New York,
233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail
[email protected], or visit . Apress Media, LLC is a California LLC
and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc).
SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected]; for reprint, paperback, or audio
rights, please email [email protected]
Apress titles 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 Print and eBook Bulk Sales
web page at .
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub via the book's product page, located at . For more
detailed information, please visit .
Printed on acid-free paper Contents
About the Authors�������������������������������������������������������������������������������������������������� xiii
Acknowledgments���������������������������������������������������������������������������������������������������xv
Introduction�����������������������������������������������������������������������������������������������������������xvii
■Chapter
■
1: Create and Test a Database and Table������������������������������������������������� 1
Defining Developer, Administrator, and User�������������������������������������������������������������������� 2
Defining Interactive Websites����������������������������������������������������������������������������������������������������������������� 2 Summary������������������������������������������������������������������������������������������������������������������������ 31
■Chapter
■
2: Create Web Pages That Interact with Users��������������������������������������� 33
Creating the Folder for Holding the Database Pages����������������������������������������������������� 34
Creating the Temporary Template���������������������������������������������������������������������������������� 34
Introducing the PHP include( ) Function������������������������������������������������������������������������� 36
The Included Header File���������������������������������������������������������������������������������������������������������������������� 37
The Included Menu File������������������������������������������������������������������������������������������������������������������������ 39
The Included Information Column�������������������������������������������������������������������������������������������������������� 40
The Included Footer File����������������������������������������������������������������������������������������������������������������������� 41 The Interactive Version of the Template������������������������������������������������������������������������� 42
Connecting to the Database������������������������������������������������������������������������������������������� 44
The Registration Page�������������������������������������������������������������������������������������������������������������������������� 47 The PHP Keyword echo�������������������������������������������������������������������������������������������������� 58
The “Thank You” Page���������������������������������������������������������������������������������������������������� 58
Displaying Error Messages That Are Collected in an Array��������������������������������������������� 60 iii ■ Contents Hashing the Password���������������������������������������������������������������������������������������������������� 61
Viewing Members’ Records�������������������������������������������������������������������������������������������� 61
The View Users Page������������������������������������������������������������������������������������������������������ 61
The Change Password Page������������������������������������������������������������������������������������������� 65
Confirming a Successful Password Change������������������������������������������������������������������� 72
Testing the Tutorial’s Pages������������������������������������������������������������������������������������������������������������������ 72 More About Arrays���������������������������������������������������������������������������������������������������������� 73
Summary����������������������������������������������������������������������������������������������������������������������� 74
■■Chapter 3: Create Login/Logout Functionality for Members and
an Administrator������������������������������������������������������������������������������������������������� 75
Creating the logindb Database and users Table������������������������������������������������������������� 76
Removing or Replacing Redundant Menu Buttons in the Headers��������������������������������� 77
Adding a Login Button to the Home Page Header�������������������������������������������������������������������������������� 78
Removing Redundant Buttons from the Registration and New Password Headers����������������������������� 79
The Revised Registration Page������������������������������������������������������������������������������������������������������������� 79
The New Header for the New Password Page�������������������������������������������������������������������������������������� 80
A New Header Menu for the Members page���������������������������������������������������������������������������������������� 82
Amend the Header for the “Thank You” Page��������������������������������������������������������������������������������������� 82 The Registration Page and Undesirable Characters������������������������������������������������������� 84
Registering Some Members����������������������������������������������������������������������������������������������������������������� 87 Differentiating Between Two Types of Membership������������������������������������������������������� 88
Creating User Levels to Limit Access to Private Pages�������������������������������������������������� 88
Log In���������������������������������������������������������������������������������������������������������������������������� 91
The Header for the Login Page������������������������������������������������������������������������������������������������������������� 91
The Login Page������������������������������������������������������������������������������������������������������������������������������������� 92
Sessions����������������������������������������������������������������������������������������������������������������������������������������������� 99 A Members-Only Page�������������������������������������������������������������������������������������������������� 101
Planning the Administrator’s Role�������������������������������������������������������������������������������� 104
A New Header for the Administration Page���������������������������������������������������������������������������������������� 105 iv ■ Contents Testing the Login/Logout Function������������������������������������������������������������������������������� 109
Amending and Deleting Individual Records���������������������������������������������������������������������������������������� 109 Summary���������������������������������������������������������������������������������������������������������������������� 110
■Chapter
■
4: Create an Administration Interface�������������������������������������������������� 111
The Administration Database��������������������������������������������������������������������������������������� 111
The Users Table��������������������������������������������������������������������������������������������������������������������������������� 113 Revising the View Users Page to Include Editing and Deleting������������������������������������ 115
Displaying Pages of Records (Pagination)�������������������������������������������������������������������� 121
Planning the Search Criteria���������������������������������������������������������������������������������������� 128
A Temporary Search Page for Displaying Specified Members������������������������������������� 128
The Search Form���������������������������������������������������������������������������������������������������������� 132
The Final Form Handler for Receiving Search Form Input������������������������������������������� 135
Editing Records������������������������������������������������������������������������������������������������������������ 139
Deleting Records�������������������������������������������������������������������������������������������������������������������������������� 145 Summary���������������������������������������������������������������������������������������������������������������������� 150
■Chapter
■
5: Expand and Enrich Your Website����������������������������������������������������� 151
Creating a New Database, a Table with 15 Columns, and a Price Table����������������������� 151
Creating the File for Connecting to the Database������������������������������������������������������������������������������� 152
Creating the Tables����������������������������������������������������������������������������������������������������������������������������� 153
Using ENUM��������������������������������������������������������������������������������������������������������������������������������������� 154 The Importance of Documentation������������������������������������������������������������������������������� 154
Extending the Registration Form and Adding a Pull-Down Menu�������������������������������� 156
Always Announce Prices and Fee Payments Up-Front����������������������������������������������������������������������� 157 Adding PayPal Debit/Credit Card Images��������������������������������������������������������������������� 168
Including PayPal on the “Thank You” Page������������������������������������������������������������������ 168
Registering Some Members����������������������������������������������������������������������������������������� 173
A Small Amendment to the Login Page���������������������������������������������������������������������������������������������� 175 Amending the Administrator’s Header������������������������������������������������������������������������� 175
Adding Class and Paid to the admin_view_users Table����������������������������������������������� 177
v ■ Contents Searching and Editing Records������������������������������������������������������������������������������������ 181
Modifying the Form for Editing Records��������������������������������������������������������������������������������������������� 184 Summary���������������������������������������������������������������������������������������������������������������������� 190
■Chapter
■
6: Add the Finishing Touches: Security and Validation������������������������ 191
Creating the Database�������������������������������������������������������������������������������������������������� 191
Creating the File for Connecting to the Database������������������������������������������������������������������������������� 192 Creating the users Table by Importing a SQL Dump File���������������������������������������������� 193
Registering Some Members Manually������������������������������������������������������������������������������������������������ 195 Adding a Title Column to the users Table��������������������������������������������������������������������� 197
Creating the Prices Table by Importing a SQL Dump File������������������������������������������������������������������� 199 Tidying Up the Folders and Filing System�������������������������������������������������������������������� 200
Degrees of Security������������������������������������������������������������������������������������������������������ 200
Adding a Layer of Security����������������������������������������������������������������������������������������������������������������� 200
An Increased Layer of Protection�������������������������������������������������������������������������������������������������������� 202 Validation and Sanitization������������������������������������������������������������������������������������������� 202
The filter_var() Function��������������������������������������������������������������������������������������������������������������������� 202
Validation�������������������������������������������������������������������������������������������������������������������������������������������� 202
Sanitization����������������������������������������������������������������������������������������������������������������������������������������� 204
Validating Telephone Numbers���������������������������������������������������������������������������������������������������������� 205 A Safer Registration Page�������������������������������������������������������������������������������������������� 205
Searching for a Title, an Address, or a Telephone Number������������������������������������������� 221
Viewing the Retrieved Title, Address, and Phone Number������������������������������������������������������������������ 225
Editing the Title, Addresses, and Telephone Numbers������������������������������������������������������������������������ 228 Summary���������������������������������������������������������������������������������������������������������������������� 236
■Chapter
■
7: Migrate to a Host and Back Up Your Website Database������������������� 237
Making Last-Minute Changes�������������������������������������������������������������������������������������� 237
Creating a New Database������������������������������������������������������������������������������������������������������������������� 238
Allowing Members to Update Their Own Records������������������������������������������������������������������������������ 240
A Secure Feedback Form������������������������������������������������������������������������������������������������������������������� 254 vi ■ Contents The Feedback Form���������������������������������������������������������������������������������������������������������������������������� 255
The “Thank You” Page and the Error Messages��������������������������������������������������������������������������������� 266 A Common Header������������������������������������������������������������������������������������������������������� 269
Logging Exceptions and Error�������������������������������������������������������������������������������������� 272
Migrating the Database and Tables to a Remote Host������������������������������������������������� 274
A Puzzling Error Message������������������������������������������������������������������������������������������������������������������� 275
Creating and Exporting the SQL File��������������������������������������������������������������������������������������������������� 275
Using the GUIs on a Remote Host’s Server����������������������������������������������������������������������������������������� 281
Connecting to the Database on the Remote Host������������������������������������������������������������������������������� 282
Securely Uploading the mysqli_connection.php File�������������������������������������������������������������������������� 283
Uploading the Interactive Pages to the Host�������������������������������������������������������������������������������������� 285 Backing Up Your Database������������������������������������������������������������������������������������������� 286
Summary���������������������������������������������������������������������������������������������������������������������� 286
■Chapter
■
8: Create a Product Catalog����������������������������������������������������������������� 287
Preparing the Database and Administration Plan��������������������������������������������������������� 287
Creating a New Database��������������������������������������������������������������������������������������������� 288
Creating the File for Connecting to the Database�������������������������������������������������������� 289
Security������������������������������������������������������������������������������������������������������������������������ 290
Creating a Home Page with Search Capability������������������������������������������������������������� 292
The Header for the Majority of the Pages������������������������������������������������������������������������������������������� 293
The Home Page Code������������������������������������������������������������������������������������������������������������������������� 294 Displaying the Catalog������������������������������������������������������������������������������������������������� 296
The Header for the Page of Search Results����������������������������������������������������������������� 303
Creating the House Details Page���������������������������������������������������������������������������������� 304
Creating the Admin/Adding a House Page�������������������������������������������������������������������� 309
The Header for the Admin...
View
Full Document
- Fall '19
- Nature, administrator, Web page