Showing posts with label PHP. Show all posts

Second Edition

Adrian W. West & Steve Prettyman


e-books shop
e-books shop
Purchase Now !
Just with Paypal



Book Details
 Price
 3.00
 Pages
 561 p
 File Size 
 15,578 KB
 File Type
 PDF format
 ISBN-13
 978-1-4842-3842-4 (pbk)
 978-1-4842-3843-1 (electronic) 
 Copyright©   
 2018 by Adrian W. West and Steve Prettyman 

About the Authors
Adrian W. West resigned as a chartered design engineer to become the UK director of a correspondence school. He has been teaching in one form or another since 1982. He introduced computers into his workplace in 1987 and taught the staff how to use them. For four years, he taught undergraduates computer skills at a college in Cheshire in the United Kingdom.
Adrian lives in Colyton, a town in Devon, England, and for the last 18 years, he has designed and
produced websites for UK businesses and charities.
Adrian is the author of three books published by Apress: Practical HTML5 Projects, a book of tips and
tricks (now rather out of date); Practical PHP and MySQL Website Databases (the first edition of this book); and Practical Web Design for Absolute Beginners (published in 2016).
Steve Prettyman earned his Bachelor of Arts degree in secondary education from Oglethorpe University in 1979. He quickly began his teaching career as a high school mathematics instructor while continuing his education by earning a master’s degree in business information systems from Georgia State University (1985). Since then, Steve has spent more than 30 years in the IT industry. The last almost 20 of those he has been an instructor and professor at Chattahoochee Technical College, Kennesaw State University, and Southern Polytechnic State University. He is currently the Computer Science Department chairperson for Florida Keys Community College in Key West, Florida. His primary teaching responsibilities include programming, web design, and web application development.

Introduction
What’s New in This Edition?
The code and instructions in the first edition of this book (written in 2012) were made obsolete by new and very different versions of XAMPP, EasyPHP, phpMyAdmin, PHP, and MySQL/MariaDB. This second edition contains new code and instructions to match the latest versions of the software.
With the massive increase in cybercrime and other cyber threats, this new edition has been fortified
with a much stronger emphasis on security. This book takes the approach of sanitizing any data that has
been accepted from any outside source and, additionally, sanitizing any data before it is displayed on a web page. Most of the examples use prepared statements that ensure that any externally accepted data cannot be executed and therefore cannot cause security vulnerabilities.
We have chosen Bootstrap to provide responsive web design (RWD) for each of the book’s example
websites. While Bootstrap provides the CSS and JavaScript to format the examples in this book for any size device, you can easily reformat these examples with your own CSS code if you desire.
This new edition also takes a brief look at Oracle’s MySQL 8. A comparison is provided of the tools
available in each version. Step-by-step procedures provide you with the ability to upgrade to MySQL 8.

The Teaching Method
This book uses a different way of teaching website database design compared with the majority of manuals. The usual layout starts with several lessons on PHP followed by snippets of code and may eventually conclude with a project or two. This book abandons that approach. The primary focus is on fully worked, practical MySQL/MariaDB database projects built into real-world web pages.
Instead of presenting PHP, SQL, and MySQL/MariaDB as completely separate topics, they are explained in the context of each project. However, you will find a useful quick reference of PHP syntax in Appendix B. In this book, practical databases and interactive web pages are presented as early as possible; in fact, you will create a database and a table in the first chapter. In the second chapter, you will embed a database into an interactive web page and test it. Each subsequent chapter will introduce you to increasingly sophisticated and useful database-driven website pages.
We assume that you have little or no knowledge of PHP and databases. This book will demonstrate:
• How to create a free environment for testing database-driven web pages.
• How to embed PHP and interactive databases into real-world web pages. This is the
primary theme throughout the book.
• How PHP, HTML, and MySQL/MariaDB work together for creating and maintaining
a database and its data.
• How to create a user-friendly interface so that an administrator with minimum
computer skills can monitor the database.
Because databases need to be viewed and tested on a server, the first part of Chapter 1 has instructions
for using a free server that can be downloaded and installed on your computer. This ensures you will have a safe development platform for learning and testing as you explore the book’s practical projects.
Starting with a separate study of PHP theory and syntax can deter learners and prolong the time until
they get their hands on a practical application. Learners are enthused when they achieve something. This book jumps into the database driving seat right from the beginning. Essential PHP and MySQL/MariaDB techniques are presented in context within each tutorial where they are most relevant.

The “Quick and Easy to Learn” Myth
Books frequently state that PHP and MySQL/MariaDB databases are easily and quickly learned, but this discourages beginners, because when they are confronted with the inevitable difficulties (and error messages), they begin to think that they will never grasp even the basic principles.
Beginners should not be discouraged if they remember the following fact: authors claiming that PHP
and MySQL/MariaDB are easily and quickly learned have probably been using PHP and MySQL for more than a decade, and they have forgotten the difficulties they encountered when they first began.
If you accept that some time and effort are required to learn PHP and MySQL/MariaDB, then as
you work through the book, it will become increasingly apparent that you are learning something very
worthwhile. So, have patience and persevere, and you will then begin to enjoy mastering this valuable discipline.

The Origin of This Book
Most of the PHP/MySQL/MariaDB books tend to demonstrate the author’s deep and extensive knowledge of PHP and MySQL/MariaDB instead of teaching how to embed MySQL/MariaDB databases into web pages. In contrast, this book uses fully worked examples to demonstrate how to integrate databases into a website.
The boatload of PHP/MySQL/MariaDB database books that this book’s authors own (or borrowed)
were unnecessarily complicated. The authors of these books had become used to using neat tricks and
shortcuts that were second nature, but these cluttered the code and made it difficult for beginners to discern the essential structure.
This book avoids this mistake; a few useful tricks are introduced gradually and are fully explained in
plain English. This book is based on a quote from the composer Brahms.

It is easy to compose but wonderfully hard to let the superfluous notes fall under the table.
Almost all the PHP/MySQL/MariaDB books were written backward; they grind away for chapter after
chapter with PHP functions and statements (yawn), and then they add the MySQL/MariaDB bit. Practical PHP and MySQL Website Databases explains the necessary PHP and MySQL/MariaDB topics in context within each database tutorial.
MySQL/MariaDB books are nearly always written assuming that the web designer will administer the
databases. However, small e-commerce websites, clubs, and societies cannot afford to do this and would prefer that their membership secretary be able to administer the database using a user-friendly interface. The majority of the databases created in this book can be administered by both an unskilled membership secretary and the web designer.
Eventually, Adrian W. Wood concluded that he must write his own manual based on what he could
learn by concatenating snippets of information from multiple resources. He also based the manual on his own trial-and-error approach as a raw beginner. This automatically ensured that the manual’s content was presented in simple, logical, and progressive steps without suddenly introducing unexplained items.
The homegrown manual was so useful that he decided that it should be shared with other website
designers; the first edition of this book was the result of that decision.
Computer software and database techniques are constantly improving and updating. Because of this,
we have researched the latest versions of the scripts, tools, and the available software. This ensures that, in this second edition, the content and illustrations will remain relevant for as long as possible.
Following the tutorials in this book requires an absolute minimum of software. Some manuals ask
readers to download and learn a new piece of software before they can proceed to each new chapter. In
fact, Adrian came across one book that required readers to download MySQL, Apache, PHP, phpMyAdmin, Prototype 1.5, Scriptaculous, Zend Framework, Smarty Template Engine, FCK editor, jQuery, and Ajax. In this book, besides a code editor, the software required is limited, as described next.


Table of 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
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
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
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
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 Administrator’s Page ..................................................................... 318
Administrator’s View of the Entire Stock of Houses for Sale ........................................ 320
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
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
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
Undefined Index 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 <style> Not Allowed as Child of Element <div> ............................................ 514
Problem with Prepared Statement ............................................................................... 514
Logical Errors ............................................................................................................... 515
The Internet Is Your Friend ........................................................................................... 515
Summary ...................................................................................................................... 515
■■Appendix B: Resources ...................................................................................... 517
PHP Quick Reference ................................................................................................... 517
Arrays .................................................................................................................................................517
Bootstrap Template ...................................................................................................... 533
MySQL and phpMyAdmin Quick Reference .................................................................. 534
date_time ...........................................................................................................................................535
INSERT ................................................................................................................................................536
SELECT ...............................................................................................................................................536
UPDATE ...............................................................................................................................................536
What Next? ................................................................................................................... 537
Resources .................................................................................................................... 537
Books on PHP and MySQL for Databases ........................................................................................... 537
PHP and MySQL Internet Resources ...................................................................................................538
E-commerce Resources .....................................................................................................................538
Summary ...................................................................................................................... 539
Index ..................................................................................................................... 541


Bookscreen
e-books shop

Who Is This Book For?
The book assumes you are thoroughly familiar with HTML5 and CSS3. However, we assume you have no knowledge of MySQL/MariaDB, PHP, and phpMyAdmin. As the chapters unfold, you will progress from intermediate level to advanced level.
You do not need to acquire an extensive knowledge of PHP to create interactive databases. We
introduce all the PHP you will need in the appropriate place within each project. Each piece of PHP code is explained fully in plain English. The step-by-step, fully worked examples will show you what MySQL/ MariaDB and PHP can do and how to do it. This book is for web designers who want to begin developing database-driven websites.
With this in mind, this book uses a highly motivational step-by-step approach. We recognize fully that
a sense of achievement encourages readers to look forward eagerly to the next step. The book will teach
enough PHP and MySQL/MariaDB to complete all the projects in the book. 
Web developers who have not
kept up-to-date with MySQL/MariaDB and PHP will also benefit from this approach. College and university
programming instructors will find that this book provides an excellent text, and the projects can form a basis
for students to adapt for their course work.

by Steve Suehring and Janet Valade

7 BOOKS IN 1

Contents at a Glance

Book I: Getting Started with PHP & MySQL
Understanding the Languages of the Web
Installing a Web Server
Installing PHP
Setting Up MySQ
Setting Up Your Web Development Environment with the XAMPP Package
Book II: HTML and CSS
Creating a Basic Page with HTML
Adding Style with CSS
Creating and Styling Web Forms
Book III: JavaScript
Understanding JavaScript Basics
Building a JavaScript Program
Adding jQuery
Reacting to Events with JavaScript and jQuery
Troubleshooting JavaScript Programs
Book IV: PHP
Understanding PHP Basics
Building PHP Scripts
PHP and Your Operating System
Object-Oriented Programming
Considering PHP Security
Tracking Visitors with Sessions
Book V: MySQL
Introducing MySQL
Administering MySQL
Designing and Building a Database
Using the Database
Communicating with the Database from PHP Scripts
Book VI: Web Applications
Improving Your PHP Programs
Creating and Using a Web Service
Validating Web Forms with JavaScript and PHP
Building a Members-Only Website
Book VII: PHP and Templates
Configuring PHP
Building a Templating System

e-books shop
e-books shop
Purchase Now !
Just with Paypal



Book Details
 Price
 3.00
 Pages
 724 p
 File Size 
 14,193 KB
 File Type
 PDF format
 ISBN
 978-1-118-21370-4 (pbk)
 978-1-118-22874-6 (ebk)
 978-1-118-23134-0 (ebk)
 978-1-118-26617-5 (ebk)
 Copyright©   
 2013 by John Wiley & Sons, Inc  

About the Authors
Steve Suehring is the author of several technology books. Steve has written
web applications, big and small, for a variety of organizations and in a variety
of programming languages. Steve’s expertise is in finding creative solutions
to complex problems and complex solutions to simple problems.

Janet Valade is the author of PHP & MySQL For Dummies, which is in its third
edition. She has also written PHP & MySQL Everyday Apps For Dummies and
PHP & MySQL: Your visual blueprint for creating dynamic, database-driven Web
sites. In addition, Janet is the author of Spring into Linux and a coauthor of
Mastering Visually Dreamweaver CS3 and Flash CS3 Professional.
Janet has 20 years of experience in the computing field. Most recently, she
worked as a Web designer and programmer in an engineering firm for four
years. Prior to that, Janet worked for 13 years in a university environment,
where she was a systems analyst. During her tenure, she supervised the
installation and operation of computing resources, designed and developed
a data archive, supported faculty and students in their computer usage,
wrote numerous technical papers, and developed and presented seminars
on a variety of technology topics.

Introduction
Although web development has changed over the years, the actual core
details of creating a web page have stayed the same. You create a document
and put it out on the web for people to view. Of course, to put something
on the web you need to learn the special languages that are spoken
on the web. No, we’re not talking about OMG, BRB, and all the other cryptic
shorthand to communicate. We’re talking about the languages that are used
to create web documents and sites.

This book looks at many aspects of web development, including the language
used to make web pages and ways to make web pages look good,
make web pages accept information from visitors, and create programs to
create other web pages! If that seems like a lot of information, don’t worry.
It’s all broken up into manageable pieces so that you can consume the information
at your own pace.

Table of Contents
Introduction.................................................................. 1
About This Book...............................................................................................1
Foolish Assumptions........................................................................................1
How This Book Is Organized...........................................................................2
Book I: Getting Started with PHP and MySQL.....................................2
Book II: HTML and CSS...........................................................................2
Book III: JavaScript.................................................................................2
Book IV:PHP.............................................................................................2
Book V: MySQL........................................................................................2
Book VI: Web Applications....................................................................2
Book VII: PHP and Templates................................................................2
Companion Website...............................................................................3
Icons Used in This Book..................................................................................3
Where to Go from Here....................................................................................3
Book I: Getting Started with PHP & MySQL.................... 5
Chapter 1: Understanding the Languages of the Web 7
Understanding How the Web Works..............................................................7
The web browser....................................................................................8
The web server.......................................................................................8
Understanding Web Page Languages...........................................................10
Marking up with HTML.........................................................................10
Styling pages with CSS.........................................................................11
Changing behaviors with JavaScript..................................................11
Understanding the Language of Web Servers.............................................12
Building dynamic web applications with PHP and MySQL.............12
Sending the page to the browser with Apache.................................13
Choosing How You Want to Develop...........................................................14
Choosing a host for your website.......................................................14
Hosting for a company website..........................................................15
Choosing a web-hosting company......................................................16
Using a hosted website........................................................................18
Setting Up Your Local Computer for Development....................................19
Installing the web server......................................................................19
Installing PHP........................................................................................20
Installing MySQL...................................................................................20
Chapter 2: Installing a Web Server 21
Testing Your Web Server...............................................................................21
Obtaining Apache...........................................................................................22
Selecting a version of Apache.............................................................22
Downloading from the Apache website.............................................23
Obtaining Apache for Windows..........................................................23
Obtaining Apache for Linux.................................................................23
Obtaining Apache for Mac...................................................................24
Obtaining all-in-one installation kits...................................................24
Verifying a downloaded file.................................................................24
Installing Apache............................................................................................25
Installing Apache on Windows............................................................25
Installing Apache on a Mac..................................................................27
Installing Apache from source code on Linux and Mac...................27
Starting and Stopping Apache......................................................................28
Starting and stopping Apache on Windows......................................28
Starting Apache on Linux, Unix, and Mac..........................................29
Restarting Apache on Linux, Unix, and Mac......................................30
Stopping Apache on Linux, Unix, and Mac........................................30
Getting Information from Apache.................................................................31
Getting Apache information on Windows..........................................31
Getting Apache information on Linux, Unix, and Mac.....................31
Configuring Apache........................................................................................32
Changing settings..................................................................................32
Changing the location of your Document Root.................................33
Changing the port number..................................................................33
Chapter 3: Installing PHP . 35
Checking the PHP Installation.......................................................................36
Obtaining PHP.................................................................................................36
Downloading from the PHP website...................................................37
Obtaining PHP for Windows................................................................37
Obtaining PHP for Linux......................................................................37
Obtaining PHP for the Mac OS............................................................38
Obtaining all-in-one installation kits...................................................38
Verifying a downloaded file.................................................................39
Installing PHP..................................................................................................40
Installing on Unix and Linux................................................................40
Installing on Mac OS X..........................................................................42
Installation options for Unix, Linux, and Mac...................................44
Installing on Windows..........................................................................46
Configuring Your Web Server for PHP.........................................................47
Configuring your web server on Windows........................................47
Configuring Apache on Linux and Mac..............................................49
Configuring PHP..............................................................................................50
Using the XAMPP Control Panel...................................................................78
Testing Your Development Environment....................................................79
Opening the XAMPP web page............................................................80
Testing phpMyAdmin...........................................................................81
Testing PHP...........................................................................................81
Configuring Your Development Environment.............................................82
Configuring PHP....................................................................................83
Configuring Apache..............................................................................83
Configuring MySQL...............................................................................84
Uninstalling and Reinstalling XAMPP...........................................................84
Troubleshooting.............................................................................................85
Book II: HTML and CSS................................................ 87
Chapter 1: Creating a Basic Page with HTML 89
Understanding the HTML Building Blocks..................................................89
Document types....................................................................................90
Sections of an HTML Document...................................................................91
The root element..................................................................................92
The head section and title element....................................................92
The body section..................................................................................94
Creating Good HTML......................................................................................94
Using the appropriate elements.........................................................94
Putting text on a page..........................................................................95
Creating your first page.......................................................................97
Choosing block-level or inline elements............................................98
Inserting line breaks and spaces........................................................99
Making your document easier to maintain......................................101
Adding lists and tables.......................................................................102
Practicing Creating a Table.........................................................................105
Including Links and Images on Your Web Page........................................108
Adding links.........................................................................................108
Adding images.....................................................................................113
Writing Valid HTML......................................................................................116
Validating Your HTML.................................................................................117
Chapter 2: Adding Style with CSS 121
Discovering What CSS Can and Can’t Do for Your Web Page.................121
What is CSS?........................................................................................121
Why use CSS?......................................................................................122
Limitations of CSS...............................................................................122
Connecting CSS to a Page............................................................................123
Adding styling to an HTML element.................................................123
Using an internal style sheet.............................................................126
Using an external style sheet............................................................128
Targeting Styles............................................................................................129
Selecting HTML elements..................................................................130
Selecting individual elements............................................................130
Selecting a group of elements...........................................................131
Changing Fonts.............................................................................................134
Setting the font family........................................................................134
Setting font size...................................................................................136
Setting the font color..........................................................................138
Adding Borders.............................................................................................140
Changing List Styles.....................................................................................144
Changing bullet styles........................................................................145
Removing bullets................................................................................146
Adding a Background...................................................................................147
Changing the background color........................................................147
Adding a background image..............................................................150
Creating Page Layouts.................................................................................155
Creating a single-column layout........................................................155
Creating a two-column layout...........................................................159
Adding Headers and Footers to a Page......................................................163
Creating a header, header menu, and footer..................................163
Examining the HTML and CSS files...................................................166
Chapter 3: Creating and Styling Web Forms . 169
Using Web Forms to Get Information.........................................................169
Understanding web forms.................................................................170
Looking at form elements..................................................................170
Creating a Form............................................................................................172
All about the form element................................................................172
Adding a text input.............................................................................173
Adding a drop-down box...................................................................174
Creating check boxes.........................................................................176
Using radio buttons............................................................................178
Submitting and clearing the form.....................................................179
Using CSS to Align Form Fields...................................................................180
Book III: JavaScript................................................... 185
Chapter 1: Understanding JavaScript Basics 187
Viewing the World of JavaScript.................................................................187
JavaScript isn’t Java...........................................................................187
Knowing what JavaScript can do......................................................188
Examining the Ways to Add JavaScript to a Page....................................188
Adding the JavaScript tag..................................................................189
Adding JavaScript to a page’s HTML................................................189
Using external JavaScript..................................................................190
Chapter 2: Building a JavaScript Program . 191
Getting Started with JavaScript Programming..........................................191
Sending an alert to the screen..........................................................191
Adding comments...............................................................................193
Holding data for later in variables....................................................193
Holding multiple values in an array.................................................195
Creating strings to keep track of words...........................................195
Working with numbers.......................................................................196
Testing Things with Conditionals...............................................................197
Performing Actions Multiple Times with Loops.......................................200
For what it’s worth.............................................................................200
While you’re here................................................................................203
Using Functions to Avoid Repeating Yourself..........................................203
Creating functions..............................................................................204
Adding function arguments...............................................................204
Calling a function................................................................................204
Improving the addNumbers function...............................................205
Returning results from functions......................................................207
Objects in Brief.............................................................................................208
Creating objects..................................................................................208
Adding properties to objects............................................................209
Working with HTML Documents.................................................................210
Accessing HTML with JavaScript......................................................211
Using GetElementById to access a specific element......................211
Working with Web Browsers.......................................................................214
Detecting the browser........................................................................214
Redirecting to another page..............................................................216
Chapter 3: Adding jQuery . 219
jQuery Introduced........................................................................................219
Installing jQuery............................................................................................220
Installing jQuery locally.....................................................................220
Using CDN-hosted jQuery..................................................................221
Adding jQuery to a Page..............................................................................221
Adding local jQuery to a page...........................................................221
Adding CDN jQuery to a page............................................................222
Incorporating the jQuery ready() Function..............................................223
Selecting Elements with jQuery..................................................................225
jQuery selectors up close..................................................................226
Filtering................................................................................................227
Working with HTML Using jQuery..............................................................227
Adding HTML to a page......................................................................227
Changing elements.............................................................................230
Changing Attributes and Styles..................................................................232
Reading attributes..............................................................................233
Writing attributes...............................................................................234
Changing CSS.......................................................................................237
Chapter 4: Reacting to Events with JavaScript and jQuery . 241
Understanding Events..................................................................................241
Working with Forms.....................................................................................242
Adding a Submit Handler...................................................................242
Checking for blank fields....................................................................246
Monitoring Mouse Events............................................................................247
Capturing mouse clicks......................................................................247
Watching mouse movements............................................................251
Reacting to Keyboard Events......................................................................254
Counting characters...........................................................................254
Preventing character input................................................................257
Chapter 5: Troubleshooting JavaScript Programs 261
Employing Basic JavaScript Troubleshooting Techniques.....................261
Adding alerts.......................................................................................262
Using comments in JavaScript..........................................................262
Identifying JavaScript Problems with Firebug..........................................264
Installing Firebug................................................................................264
Using Firebug.......................................................................................266
Book IV: PHP............................................................ 269
Chapter 1: Understanding PHP Basics 271
How PHP Works............................................................................................271
Examining the Structure of a PHP Script...................................................273
Looking at PHP Syntax.................................................................................275
Using simple statements....................................................................276
Using complex statements.................................................................276
Writing PHP Code.........................................................................................277
Displaying Content in a Web Page..............................................................278
Using PHP Variables.....................................................................................281
Naming a variable...............................................................................282
Creating and assigning values to variables.....................................282
Using variable variables.....................................................................283
Displaying variable values.................................................................284
Using PHP Constants....................................................................................287
Understanding Data Types..........................................................................288
Working with integers and floating-point numbers........................289
Working with character strings........................................................292
Working with the Boolean data type................................................295
Working with the NULL data type....................................................296
Using Arrays..................................................................................................296
Creating arrays....................................................................................296
Viewing arrays.....................................................................................298
Removing values from arrays............................................................299
Sorting arrays......................................................................................299
Getting values from arrays................................................................301
Walking through an array..................................................................302
Storing values with multidimensional arrays..................................305
Using Dates and Times.................................................................................307
Setting local time................................................................................307
Formatting a date................................................................................308
Storing a timestamp in a variable.....................................................309
Understanding PHP Error Messages..........................................................310
Types of PHP error messages...........................................................310
Displaying error messages................................................................313
Logging error messages.....................................................................315
Adding Comments to Your PHP Script......................................................316
Chapter 2: Building PHP Scripts . 319
Setting Up Conditions..................................................................................320
Comparing values...............................................................................320
Checking variable content.................................................................322
Pattern matching with regular expressions....................................323
Joining multiple comparisons...........................................................327
Using Conditional Statements.....................................................................329
Using if statements.............................................................................330
Using switch statements....................................................................333
Repeating Actions with Loops....................................................................335
Using for loops....................................................................................335
Using while loops................................................................................339
Using do..while loops.........................................................................341
Avoiding infinite loops.......................................................................343
Breaking out of a loop........................................................................344
Using Functions............................................................................................346
Creating a function.............................................................................347
Using variables in functions..............................................................347
Passing values to a function..............................................................349
Returning a value from a function....................................................354
Using built-in functions......................................................................356
Organizing Scripts........................................................................................357
Separating display code from logic code.........................................357
Reusing code.......................................................................................358
Organizing with functions..................................................................358
Organizing with include files.............................................................359
Chapter 3: PHP and Your Operating System . 365
Managing Files...............................................................................................366
Getting information about files.........................................................366
Copying, renaming, and deleting files..............................................368
Organizing files....................................................................................369
Using Operating System Commands..........................................................372
Using backticks...................................................................................373
Using the system function.................................................................374
Using the exec function......................................................................375
Using the passthru function..............................................................376
Accessing error messages from system commands......................376
Understanding security issues..........................................................377
Using FTP.......................................................................................................378
Logging in to the FTP server.............................................................379
Getting a directory listing..................................................................380
Downloading and uploading files with FTP.....................................380
Looking at other FTP functions.........................................................382
Reading and Writing Files............................................................................383
Accessing files.....................................................................................384
Writing to a file....................................................................................386
Reading from a file..............................................................................387
Exchanging Data with Other Programs......................................................390
Exchanging data in flat files...............................................................390
Exchanging data in comma-delimited format.................................390
Using other delimiters........................................................................391
Using SQLite..................................................................................................394
Chapter 4: Object-Oriented Programming 397
Introducing Object-Oriented Programming..............................................397
Objects and classes............................................................................398
Properties............................................................................................399
Methods...............................................................................................399
Inheritance...........................................................................................400
Developing an Object-Oriented Script.......................................................400
Choosing objects................................................................................401
Selecting properties and methods for each object........................401
Creating and using an object.............................................................402
Defining a Class.............................................................................................402
Writing a class statement..................................................................403
Setting properties...............................................................................403
Accessing properties using $this......................................................404
Adding methods..................................................................................405
Understanding public and private properties and methods........407
Writing the constructor.....................................................................409
Putting it all together.........................................................................410
Using a Class in a Script...............................................................................413
Using Abstract Methods in Abstract Classes and Interfaces..................415
Using an abstract class......................................................................415
Using interfaces...................................................................................417
Preventing Changes to a Class or Method................................................418
Handling Errors with Exceptions................................................................419
Copying Objects............................................................................................420
Comparing Objects.......................................................................................421
Getting Information about Objects and Classes.......................................422
Destroying Objects.......................................................................................423
Chapter 5: Considering PHP Security 425
Securing the Server......................................................................................425
Hardening the server..........................................................................425
Using a firewall....................................................................................426
Securing Apache...........................................................................................426
Securing PHP applications with SuExec..........................................426
mod_security.......................................................................................427
Setting Security Options in php.ini............................................................428
Handling Errors Safely.................................................................................429
Understanding the dangers...............................................................429
Testing for unexpected input............................................................430
Handling the unexpected...................................................................431
Checking all form data.......................................................................431
Sanitizing Variables......................................................................................432
Converting HTML special characters...............................................432
Uploading Files without Compromising the Filesystem................433
Avoiding DoS attacks on the filesystem...........................................433
Validating files.....................................................................................433
Using FTP functions to ensure safe file uploads.............................434
Chapter 6: Tracking Visitors with Sessions . 437
Understanding Sessions and Cookies........................................................437
Looking at sessions............................................................................437
Working with cookies.........................................................................438
Checking if cookies are enabled.......................................................438
Using Sessions to Pass Data........................................................................440
Starting a session................................................................................440
Closing a session.................................................................................445
Using session_write_close()..............................................................445
Understanding Other Session Options......................................................446
Book V: MySQL......................................................... 447
Chapter 1: Introducing MySQL 449
Examining How MySQL Works....................................................................449
Understanding Database Structure............................................................450
Communicating with MySQL.......................................................................450
Building SQL queries..........................................................................451
Sending SQL queries...........................................................................452
Using the mysql client........................................................................453
Protecting Your MySQL Databases............................................................454
Chapter 2: Administering MySQL . 457
Understanding the Administrator Responsibilities.................................457
Default Access to Your Data.......................................................................458
Controlling Access to Your Data................................................................459
Account names and hostnames........................................................460
Passwords............................................................................................461
Account privileges..............................................................................461
Setting Up MySQL Accounts........................................................................462
Identifying what accounts currently exist.......................................464
Adding accounts.................................................................................465
Adding and changing passwords......................................................465
Changing privileges............................................................................466
Removing accounts............................................................................467
Backing Up Your Database..........................................................................468
Backing up on Windows.....................................................................469
Backing up on Linux, Unix, and Mac................................................469
Restoring Your Data.....................................................................................471
Upgrading MySQL.........................................................................................473
Chapter 3: Designing and Building a Database . 475
Designing a Database...................................................................................475
Choosing the data...............................................................................475
Organizing the data............................................................................477
Creating relationships between tables............................................480
Storing different types of data..........................................................481
Designing a Sample Database.....................................................................484
Writing Down Your Design..........................................................................487
Building a Database......................................................................................489
Creating a new database....................................................................489
Creating and deleting a database.....................................................490
Adding tables and specifying a primary key...................................491
Removing a table................................................................................493
Changing the Database Structure...............................................................494
Chapter 4: Using the Database 497
Adding Information to a Database..............................................................498
Adding one row at a time...................................................................498
Adding a bunch of data......................................................................500
Looking at the Data in a Database..............................................................502
Retrieving Information from a Database...................................................502
Retrieving specific information.........................................................503
Retrieving data in a specific order....................................................505
Retrieving data from specific rows...................................................505
Combining information from more than one table.........................508
Updating Information in a Database..........................................................513
Removing Information from a Database....................................................513
Chapter 5: Communicating with the Database from PHP Scripts . 515
Knowing How MySQL and PHP Work Together........................................515
PHP Functions That Communicate with MySQL......................................516
Communicating with MySQL.......................................................................516
Connecting to the MySQL server......................................................517
Sending an SQL statement.................................................................519
Sending multiple queries...................................................................520
Selecting a Database....................................................................................521
Handling MySQL Errors...............................................................................522
Using Other Helpful mysqli Functions.......................................................523
Counting the number of rows returned by a query.......................523
Determining the last auto entry........................................................524
Counting affected rows......................................................................525
Escaping characters...........................................................................525
Converting mysqli Functions to mysql Functions....................................526
Book VI: Web Applications......................................... 529
Chapter 1: Improving Your PHP Programs 531
Automatically Including Helper Functions................................................531
Using auto_prepend_file....................................................................531
Starting sessions with a prepended file...........................................532
Using classes for efficiency................................................................534
Reusing Code.................................................................................................535
Using functions...................................................................................536
Using object-oriented programming................................................539
Chapter 2: Creating and Using a Web Service . 541
Understanding Web Services......................................................................541
Returning Data from a Web Service...........................................................542
Returning the date..............................................................................542
Returning web service data from a database..................................545
Accepting Input to a Web Service..............................................................548
Querying with input data...................................................................548
Returning XML results.......................................................................550
Returning JSON and XML...................................................................551
Chapter 3: Validating Web Forms with JavaScript and PHP 555
Understanding How to Validate Web Forms.............................................555
Always assume bad data....................................................................556
Never assume JavaScript...................................................................556
Sometimes mirror client- and server-side validation.....................556
Performing Basic JavaScript Validation....................................................557
Looking at the form HTML and CSS..................................................561
Adding JavaScript validation.............................................................563
Performing PHP Validation..........................................................................574
Validating required fields..................................................................576
Validating text.....................................................................................579
Validating drop-downs, radio buttons, and check boxes..............579
Validating numbers............................................................................580
Validating URLs and e-mail addresses.............................................581
Making sure the passwords match...................................................582
Creating a validation function...........................................................585
Chapter 4: Building a Members-Only Website 587
Understanding a Members-Only Site.........................................................588
Creating the User Database.........................................................................589
Designing the Customer database....................................................589
Building the Customer database.......................................................590
Accessing the Customer database...................................................591
Creating Base Functions..............................................................................591
Creating Web Forms.....................................................................................593
Creating the registration pages.........................................................593
Building a success page.....................................................................603
Creating the login page......................................................................604
Creating a User Object.................................................................................607
Building the User class.......................................................................607
Building the login-process PHP file..................................................610
Adding Authenticated Pages.......................................................................612
Building a protected page..................................................................612
Building a log out page.......................................................................614
Adding E-mail Functionality........................................................................618
Building the password reset database.............................................619
Building the password recovery page.............................................619
Building the process files...................................................................625
Building the class methods...............................................................628
Book VII: PHP and Templates..................................... 633
Chapter 1: Configuring PHP . 635
Understanding the php.ini...........................................................................635
Working with the php.ini...................................................................635
Making changes outside of the php.ini............................................636
Understanding Common Configuration Changes.....................................636
Changing session timeout..................................................................636
Changing other session parameters.................................................637
Disabling functions and classes........................................................637
Changing error display.......................................................................639
Changing resource limits...................................................................639
Chapter 2: Building a Templating System . 641
Understanding Template Systems..............................................................641
Building a PHP Template.............................................................................642
Creating a template class...................................................................642
Creating the top of the page..............................................................643
Creating the bottom of the page.......................................................646
Connecting the top, bottom, and middle.........................................646
Extending the Template...............................................................................650
Building an About page......................................................................650
Building a Contact page.....................................................................651
Index........................................................................ 655


Bookscreen
e-books shop

About This Book
This book is intended as both a reference and, in certain places, a tutorial.
Most of the information in the book doesn’t need to be read in a certain
order. However, certain areas build on each other and, if you find that
you’re stuck in one of the later chapters, you might find that reading an
earlier chapter will reveal the information that you need.
Loading...
DMCA.com Protection Status