<<

Practical PHP 7, MySQL 8, and MariaDB

A Simplified Approach to Developing -Driven — Second Edition — Adrian W. West Steve Prettyman

www.allitebooks.com Practical PHP 7, MySQL 8, and MariaDB Website Databases

A Simplified Approach to Developing Database-Driven Websites

Second Edition

Adrian W. West Steve Prettyman

www.allitebooks.com 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 ://doi.org/10.1007/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 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 (www.freepik.com) 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 www.springeronline.com. 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 protected]. Apress titles may be purchased in bulk for academic, corporate, or promotional use. versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales at www.apress.com/bulk-sales. 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 www.apress.com/9781484238424. For more detailed information, please visit www.apress.com/source-code. Printed on acid-free paper

www.allitebooks.com 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 That Are Collected in an Array ��������������������������������������������� 60

iii

www.allitebooks.com 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 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

www.allitebooks.com 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 �����������������������������������������������������������������������������������������281 Connecting to the Database on the Remote Host �������������������������������������������������������������������������������282 Securely Uploading the mysqli_connection. 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 Administrator’s Page ��������������������������������������������������������������������� 318 Administrator’s View of the Entire Stock of Houses for Sale ���������������������������������������� 320

vii Contents ■

The Administrator’s Search Page ��������������������������������������������������������������������������������� 325 The Result of a Search �����������������������������������������������������������������������������������������������������������������������327 The Contact Us Page ���������������������������������������������������������������������������������������������������� 328 Summary ���������������������������������������������������������������������������������������������������������������������� 334 ■■Chapter 9: Join Multiple Tables and Other Enhancements �������������������������������� 335 Introduction to Multiple Tables ������������������������������������������������������������������������������������� 335 Normalization �������������������������������������������������������������������������������������������������������������������������������������336 Creating the Database and Tables ������������������������������������������������������������������������������������������������������337 Viewing the Connection File ���������������������������������������������������������������������������������������������������������������338 Preparing the Tables for Joining ���������������������������������������������������������������������������������������������������������339 Populating the Two Tables ������������������������������������������������������������������������������������������������������������������340 Joining Data from the Two Tables �������������������������������������������������������������������������������� 342 Creating the Third Table ����������������������������������������������������������������������������������������������������������������������345 Creating Pages to Display the Data from Your Joined Tables �������������������������������������������������������������347 The Home Page ����������������������������������������������������������������������������������������������������������������������������������347 The Main Menu for the Pages �������������������������������������������������������������������������������������������������������������349 The Header for All the Pages ��������������������������������������������������������������������������������������������������������������350 The Page for Viewing the Birds ����������������������������������������������������������������������������������������������������������351 The Page for Viewing the Locations and Habitats of the Reserves ����������������������������������������������������355 Payments by Check ������������������������������������������������������������������������������������������������������ 366 A Choice of Payment Method �������������������������������������������������������������������������������������������������������������367 Printing Online Forms ��������������������������������������������������������������������������������������������������� 375 Summary ���������������������������������������������������������������������������������������������������������������������� 376 ■■Chapter 10: Create a Message Board ���������������������������������������������������������������� 377 The Plan ����������������������������������������������������������������������������������������������������������������������� 377 Creating the Database �������������������������������������������������������������������������������������������������� 378 Creating the Tables ������������������������������������������������������������������������������������������������������� 378 Creating the Second Table �������������������������������������������������������������������������������������������� 379

viii Contents ■

Creating the Registration Form ������������������������������������������������������������������������������������ 385 The “Thank You” Page ������������������������������������������������������������������������������������������������������������������������393 Populating the Members Table ������������������������������������������������������������������������������������� 394 The Login Page ������������������������������������������������������������������������������������������������������������� 395 Logging Out ������������������������������������������������������������������������������������������������������������������ 400 Creating a Gateway to a Choice of Quotes ������������������������������������������������������������������� 400 The Form for Posting Quotations ���������������������������������������������������������������������������������� 402 Posting Some Quotations ��������������������������������������������������������������������������������������������� 408 The Comical Quotes Page �������������������������������������������������������������������������������������������� 409 The Wise Quotes Page ������������������������������������������������������������������������������������������������������������������������413 Adding Search Facilities ����������������������������������������������������������������������������������������������� 417 The Header for ViewPosts.php ������������������������������������������������������������������������������������� 420 Searching for Specific Words or Phrases ��������������������������������������������������������������������� 421 The Full Text Search Form ������������������������������������������������������������������������������������������������������������������422 Displaying the Search Results �������������������������������������������������������������������������������������� 424 The Header for the quotes_found Page ����������������������������������������������������������������������������������������������427 Enhancing the Message Board ������������������������������������������������������������������������������������� 428 Converting the Message Board to a Forum ����������������������������������������������������������������������������������������428 Summary ���������������������������������������������������������������������������������������������������������������������� 429 ■■Chapter 11: E-commerce: A Brief Introduction ������������������������������������������������� 431 Security Warning ���������������������������������������������������������������������������������������������������������� 433 The Plan ����������������������������������������������������������������������������������������������������������������������� 433 Creating the PayPal Cart Site ��������������������������������������������������������������������������������������� 434 Creating the PayPal Cart Database and Tables �����������������������������������������������������������������������������������434 Viewing the Connection File ���������������������������������������������������������������������������������������������������������������435 Populating the PayPal Cart Tables ������������������������������������������������������������������������������������������������������435 The PayPal Cart Home Page ���������������������������������������������������������������������������������������������������������������438 Using the PayPal Cart Home Page to Search for Paintings �����������������������������������������������������������������441 Integrating with the PayPal Shopping Cart Buttons ����������������������������������������������������������������������������444

ix Contents ■

Creating a Custom Cart ������������������������������������������������������������������������������������������������ 450 The Custom Cart Home Page ��������������������������������������������������������������������������������������������������������������452 Create the Custom Cart Database and Tables ������������������������������������������������������������������������������������452 Retrieving a Forgotten Password �������������������������������������������������������������������������������������������������������460 The Custom Cart Search Page ������������������������������������������������������������������������������������������������������������464 Adding Paintings to a Table for a Custom Shopping Cart �������������������������������������������������������������������465 The Checkout Page �����������������������������������������������������������������������������������������������������������������������������477 The Additional Administrative Tasks ���������������������������������������������������������������������������������������������������477 Summary ���������������������������������������������������������������������������������������������������������������������� 478 ■■Chapter 12: Take a Brief Look at Oracle MySQL 8 ��������������������������������������������� 479 Advantages of Upgrading ��������������������������������������������������������������������������������������������� 479 Installing MySQL 8 Community Server ������������������������������������������������������������������������� 481 Exploring the Features of MySQL Workbench �������������������������������������������������������������� 488 Connecting PHP 7 to the MySQL 8 Community Server ������������������������������������������������� 495 Migrating to MySQL 8 Community Server �������������������������������������������������������������������� 498 Using Our PHP Files with MySQL 8 Community Server ������������������������������������������������ 502 Summary ���������������������������������������������������������������������������������������������������������������������� 505 ■■Appendix A: Troubleshooting ����������������������������������������������������������������������������� 507 Browser Quirks ������������������������������������������������������������������������������������������������������������� 508 Tables Not Displaying ��������������������������������������������������������������������������������������������������� 508 A Style Change Has No Effect �������������������������������������������������������������������������������������� 509 Included Items Missing from the Display ��������������������������������������������������������������������� 509 A Page Fails to Validate ������������������������������������������������������������������������������������������������ 509 A PayPal Pull-Down Menu Does Not Work ������������������������������������������������������������������� 509 Access Denied �������������������������������������������������������������������������������������������������������������� 509 PHP Error Levels ����������������������������������������������������������������������������������������������������������� 510 Call to an Undefined Function �������������������������������������������������������������������������������������� 510 Cannot Redeclare Function ������������������������������������������������������������������������������������������ 510

x Contents ■

Undefined or Undefined Variable ������������������������������������������������������������������������ 510 Empty Variable Value ���������������������������������������������������������������������������������������������������� 511 Headers Already Sent ��������������������������������������������������������������������������������������������������� 511 Blank Screen ���������������������������������������������������������������������������������������������������������������� 511 Unexpected End of File in Line xxx ������������������������������������������������������������������������������� 511 Parse Error and Unexpected Characters ���������������������������������������������������������������������� 512 Unexpected T_STRING �������������������������������������������������������������������������������������������������� 512 Unexpected T_ELSE ������������������������������������������������������������������������������������������������������ 513 Wrong Equal Sign ��������������������������������������������������������������������������������������������������������� 513 Failed to Open Stream �������������������������������������������������������������������������������������������������� 513 Syntax Errors ���������������������������������������������������������������������������������������������������������������� 513 Warning: Division by Zero ��������������������������������������������������������������������������������������������� 514 Display Is Not What Was Expected ������������������������������������������������������������������������������� 514 Reference to a Primary Key Could Not Be Created ������������������������������������������������������ 514 Element

Complete your Registration by Paying with a Check

Thank you for registering online, now please fill out this form. Asterisks indicate essential fields. When you have filled out the form please print two copies by clicking the "Print This Form" button. Sign one copy and keep one for reference sign a check payable to "The Devon Bird Reserves".

Mail the signed form and check to:
The Treasurer, The Devon Bird Reserves, 99 The Street, The Village, EX99 99ZZ







Signed______ Date______



Explanation of the Code This section explains the code.

It is impossible to print the form using PHP because PHP is a server-side script. However, the form is displayed by a browser, and we can print from a screen displayed by a browser. We achieve this by means of a link to a separate conditional style sheet with the media attribute media=“print”. The first line links to the main style sheet birds.css that displays the page on the screen using the media attribute media=“screen”. The second line links the page to the print version of the page. This is automatically invoked when the display is sent to a printer or print preview. The printed form does not need the header, menu, or footer. Also, using the CSS style sheet, we can cause it to print using only black ink so that the user does not have to use the more expensive color cartridge.

374 Chapter 9 ■ Join Multiple Tables and Other Enhancements


This is the code for the button on the page that sets the browser display to the printer.

Printing Online Forms Figure 9-19 shows the printed page.

Figure 9-19. The top half of the printed page in black ink contains all the essential information

To produce the printout shown in Figure 9-19, the form was filled out on the user’s screen, and then the Print button was clicked. The result used minimal ink and no colored ink; however, it contained all that is necessary to become a member of the Devon Bird Reserves. The user signs the form and mails it with the check. The user’s address is entered into the database when they fill out the registration page; therefore, this does not need be repeated in the printed form. The style sheet print.css is the key to producing the printable form. Listing 9-19 shows the code for this style sheet.

375 Chapter 9 ■ Join Multiple Tables and Other Enhancements

Listing 9-19. Creating the Style Sheet for Printing Forms (print.css) /*PRINT.CSS: style amendments for printing only*/ /*SELECT ITEMS THAT YOU DO NOT WANT TO PRINT, e.g., header, menu, print-this-page button, and footer*/ #header, #nav, #leftcol, #button, #rightcol, #footer, #info-col, ul { display:none; } input { border:1px black solid; } h2 { font-size:16pt; color:black; text-align:center; } h3 { text-align:center; font-size:11pt;} /*REVEAL OUTGOING URL links on printed page*/ a[href^="http://":after {content: "(" attr(href)")"; }

The CSS statement { display:none; } tells the printer which items should not be printed. To avoid wasting paper and ink when testing the appearance of the printed page, use the Print Preview feature of the browser. Load the page into a browser and select File and then Print Preview to see what the printable page will look like. If the printable page includes a page break, click the right-facing arrow of the Print Preview screen to see subsequent pages. Press the Esc key to switch out of the Print Preview mode. h2 { font-size:16pt; text-align:center; } h3 { text-align:center; font-size:11pt;}

To choose the correct font sizes for the printer, use point sizes (such as 16 pt. and 12 pt.) and use trial and error to optimize the sizes. You might have text within tags, so be sure to include a style for the paragraph font size.

/*REVEAL OUTGOING URL links on the printed page*/ a[href^="http://]:after {content: "(" attr(href)")"; }

If the page contains a URL to your website, you might want this to appear on the printed page in a format that will be useful for the user (assuming that they print and retain a copy of the form). Note the use of three types of brackets. In the form, the HTML would look like this:

Click for The Devon Bird Reserves

The URL would be displayed in a browser as follows: Click to visit the Devon Bird Reserves website. Using the previous code, the printed form would appear as follows: Click to visit the Devon Bird Reserves website (www.devonbirdreserves.co.uk).

Summary In this chapter, we introduced the theory and practice of using multiple tables. You learned that such tables are virtual tables that are present only in the volatile memory of the server and that they can be viewed in a DBMS such as MySQL or MariaDB. The difference between various join methods was described. We then demonstrated that the virtual tables could be made visible in a web page by using SQL queries and PHP. A tutorial showed you how to implement membership payments by check. This was augmented by a demonstration of economical form printing so that an application form and a check could be sent to the organization. In the next chapter, we will introduce you to an online message board.

376 CHAPTER 10

Create a Message Board

A message board can be a stand-alone feature or an important component in a forum. A basic forum has at least four tables, one each for messages, membership registrations, threads, and replies. However, to save space and to defer to what’s in the subtitle of this book, A Simplified Approach, this chapter describes a simple message board with a table for messages and a table for members. Our hope is that a grasp of the principles of this message board will inspire you to expand its features and explore more complex solutions. For your interest, at the end of this chapter we have added a chart showing how you could enhance the message board to create a forum. Message boards have fewer features than a forum; generally, they lack the ability to accept replies in a manner that allows them to be collected as threads. In a forum, the replies are connected to the original posting ID and displayed in ascending date order. Our message board is designed to collect wise and comical quotations and then insert them into a searchable database. After completing this chapter, you will be able to • Create a message board with a database and tables • Create a registration page • Create login and logout pages • Create a gateway to the message board categories • Create pages to display the quotations • Create search facilities

■■Caution To prevent the display of unpleasant content, message boards require constant monitoring (moderating). Your potential clients should be warned about this before they commit themselves to a design contract with you. In 2018, the United States implemented laws that place the responsibility of the content of public message boards and other public forums on the forum provider. This is a reversal of previous enforcement, which relieved forum providers from responsibility for public content.

The Plan To simplify the message board in this chapter, users will not be able to view messages until they are registered and logged in. However, some of the messages will be shown on the home page to tempt users to register. In our example, the messages are quotations, and the aim is to build up a useful database of

© Adrian W. West and Steve Prettyman 2018 377 A. W. West and S. Prettyman, Practical PHP 7, MySQL 8, and MariaDB Website Databases, https://doi.org/10.1007/978-1-4842-3843-1_10 Chapter 10 ■ Create a Message Board quotations. The Login button on the home page will redirect registered users to a page where they will be able choose which of the types of quotation to view, either comical quotes or wise quotes. When the message board is accessed, the registered member will be able to contribute a quote. Because the member is contributing quotations, we refer to threads as quotations in the tutorial. For further simplification, the number of columns in the tables is reduced to a practical minimum. The username for logging in will be a unique pseudonym chosen by the user because message boards and forums should protect a user’s personal information. When members post a new quote, their pseudonym is the only name shown on the message board. However, when registering, they might also be asked to provide additional information, such as their e-mail address, so that the site administrator can contact them if necessary. Their e-mail address is never disclosed on the message board. We will now create the database and tables for the message board.

Creating the Database Download the files for this chapter from the book’s page at Apress.com and place them in a new folder named msgboard within the htdocs or eds-www folder. Start XAMPP or EasyPHP, and create the database named msgboarddb in phpMyAdmin. Set the encoding to utf8_general_ci. Scroll down and click Add new user. Then select the Databases tab, select the box next to msgboarddb, and click privileges. Add a new user with the following details: • Username: brunel • Host: localhost • Password: tra1lblaz3r Scroll down to Global privileges and check the check all box. Click Save (or Go in some versions). The database connection file mysqli_connect.php is included in the downloadable files. Be sure to add it to your htdocs or eds-www folder. If you want to create the file manually, use the following code:

Creating the Tables Either import the tables using the downloadable .sql files or create them manually. If you want to create the tables from scratch, within phpMyAdmin, in the left panel, click the word msgboarddb. Then create the first table with six columns and name the table members. Use the attributes in Table 10-1 and select the collation utf8_general_ci for the columns that will accept a collation (reg_date and member_level do not have a collation). As you create the columns, pop-up dialog boxes will appear for the member_id and user_name indexes. For the member_id pop-up, both fields will be PRIMARY. In the user_name index pop-up, enter user_name in the first field and UNIQUE in the second field. 378 Chapter 10 ■ Create a Message Board

Table 10-1. The members Table Column Name Type Length/Value Default Attributes NULL Index A_I member_id INT 8 None UNSIGNED ☐ PRIMARY ☑ user_name VARCHAR 12 None ☐ UNIQUE ☐ email VARCHAR 60 None ☐ ☐ passcode CHAR 60 None ☐ ☐ reg_date DATETIME None ☐ ☐ member_level TINYINT 2 None ☐ ☐

The member_id column is the PRIMARY key, and the user_name column has a UNIQUE index to prevent duplicate entries. Scroll down and click Save.

Creating the Second Table You can import the forum table from the downloadable SQL file or manually create the forum table with five columns. It contains a column named post_id, and this is configured as the PRIMARY KEY. Our project will enable users to search for a phrase or word from a quotation; therefore, a full text search is provided. Give the message column a FULL TEXT index. Table 10-2 shows the table details.

Table 10-2. The Attributes for the forum Table Column Name Type Length/Value Default Attributes NULL Index A_I post_id INT 8 None UNSIGNED ☐ PRIMARY ☑ user_name VARCHAR 12 None ☐ ☐ subject VARCHAR 60 None ☐ ☐ message TEXT None ☐ FULL TEXT ☐ post_date DATETIME None ☐ ☐

When you create the post_id column in the forum table, you will see a pop-up dialog. For the column post_id, both fields should contain the word PRIMARY. Click Go. When you create the message column in the forum table, you will see the pop-up dialog shown in Figure 10-1.

379 Chapter 10 ■ Create a Message Board

Figure 10-1. Add index dialog

Next we will examine some of the pages for the website. Creating the Home Page for the Message Board The home page displays some sample quotes to encourage people to register. After they register and log in, they will be able to view more quotes and also contribute quotes to the collection. If you prefer to create the tables from scratch, no quotes (messages) will be displayed on the home page until the messages table is populated. Figure 10-2 shows the home page when the tables are populated.

Figure 10-2. The tables are populated, so the home page displays four quotes

380 Chapter 10 ■ Create a Message Board

Listing 10-2a shows the code for producing the home page.

Listing 10-2a. Creating the Home Page for the Forum’s Website (index.php) Message Board Home Page

This home page shows a selection from our large collection of quotations.

To view the whole collection, please register.

You will then be able to contribute to this message board by adding quotations.

381 Chapter 10 ■ Create a Message Board

$result = mysqli_query( $dbcon, $query ) ; if ( mysqli_num_rows( $result ) > 0 ) { ?>

'; } echo '
Posted By Subject Message
' . $user_name . ' ' . $post_date . ' ' . $message . '
' ; } else { echo '

There are currently no messages.

' ; } mysqli_close( $dbcon ) ; ?>

The header uses a case statement that contains every horizontal menu button required by this chapter, but various buttons will be disabled to suit each page. Listing 10-2b gives the code for the home page header code segment for the index page.

382 Chapter 10 ■ Create a Message Board

Listing 10-2b. Creating the Header for the Home Page (includes/header.php)