Apress Access

PHP Solutions

Dynamic Web Design Made Easy

2nd Edition

By David Powers

  • eBook Price: $31.99
Buy eBook Buy Print Book
This book gives you real value right away through a series of practical examples that you can incorporate directly into your sites, optimizing performance and adding functionality.

Full Description

  • Add to Wishlist
  • ISBN13: 978-1-4302-3249-0
  • 528 Pages
  • User Level: Intermediate
  • Publication Date: November 23, 2010
  • Available eBook Formats: EPUB, MOBI, PDF

Related Titles

  • Go Recipes
  • Building APIs with Node.js
  • Pro MEAN Stack Development
  • Pro Marketing and Search Engine Optimization
  • Learn CakePHP, Second Edition
  • PHP Development Tool Essentials
  • PHP and MySQL Recipes
  • Practical Laravel 5
  • Lumen Programming Guide
Full Description

This is the second edition of David Powers' highly-respected PHP Solutions: Dynamic Web Design Made Easy. This new edition has been updated by David to incorporate changes to PHP since the first edition and to offer the latest techniques—a classic guide modernized for 21st century PHP techniques, innovations, and best practices.

You want to make your websites more dynamic by adding a feedback form, creating a private area where members can upload images that are automatically resized, or perhaps storing all your content in a database. The problem is, you're not a programmer and the thought of writing code sends a chill up your spine. Or maybe you've dabbled a bit in PHP and MySQL, but you can't get past baby steps. If this describes you, then you've just found the right book. PHP and the MySQL database are deservedly the most popular combination for creating dynamic websites. They're free, easy to use, and provided by many web hosting companies in their standard packages.

Unfortunately, most PHP books either expect you to be an expert already or force you to go through endless exercises of little practical value. In contrast, this book gives you real value right away through a series of practical examples that you can incorporate directly into your sites, optimizing performance and adding functionality such as file uploading, email feedback forms, image galleries, content management systems, and much more. Each solution is created with not only functionality in mind, but also visual design.

But this book doesn't just provide a collection of ready-made scripts: each PHP Solution builds on what's gone before, teaching you the basics of PHP and database design quickly and painlessly. By the end of the book, you'll have the confidence to start writing your own scripts or—if you prefer to leave that task to others—to adapt existing scripts to your own requirements. Right from the start, you're shown how easy it is to protect your sites by adopting secure coding practices.

What you’ll learn

  • Updates to the first edition with the latest PHP techniques for modern, dynamic web design
  • Create dynamic websites with design and usability in mind, as well as functionality
  • Understand how PHP scripts work, giving you confidence to adapt them to your own needs
  • Bring online forms to life, check required fields, and ensure user input is safe to process
  • Upload files and automatically create thumbnails from larger images
  • Manage website content with a searchable database

Who this book is for

This is a "must have" reference book for any Web developer using PHP scripting language. If you have read the first edition classic, then this update will bring you the latest PHP techniques and best practices. If you are new to dynamic web design with PHP, then David will introduce you to the core techniques and methods for dynamic PHP web sites, ready for the 21st century!

Table of Contents

Table of Contents

  1. What Is PHP—And Why Should I Care?
  2. Getting Ready to Work with PHP
  3. How to Write PHP Scripts
  4. Lightening Your Workload with Includes
  5. Bringing Forms to Life
  6. Uploading Files
  7. Using PHP to Manage Files
  8. Generating Thumbnail Images
  9. Pages That Remember: Simple Login and Multipage Forms
  10. Getting Started with MySQL
  11. Connecting to MySQL with PHP and SQL
  12. Creating a Dynamic Online Gallery
  13. Managing Content
  14. Formatting Text and Dates
  15. Pulling Data from Multiple Tables
  16. Managing Multiple Database Tables
  17. Authenticating Users with a Database
Source Code/Downloads

Downloads are available to accompany this book.

Your operating system can likely extract zipped downloads automatically, but you may require software such as WinZip for PC, or StuffIt on a Mac.


If you think that you've found an error in this book, please let us know by emailing to editorial@apress.com . You will find any confirmed erratum below, so you can check if your concern has already been addressed.

On page 22:
Step 4 says to save phpinfo.php to c:\inetpub\wwwroot and then (Step 5) you should be able to access that through localhost/phpinfo.

That only works if the document root is pointing at that directory. In my case the document root was c:/xampp/htdocs.

On page 116-118:

Two other people have reported script failures because the $name variable doesn't exist, and suggested creating it in contact.php. The root cause is in the processmail.inc.php include, which should be creating a variable for every element in the $expected array - $name, $email and $comment - but it isn't.

The culprit is the "elseif" in this chunk of code:
if (empty($temp) && in_array($key, $required)) {
$missing[] = $key;
} elseif (in_array($key, $expected)) {
${$key} = $temp;

You want to create the $name variable unconditionally if 'name' is in the $expected array, so use this code:
// if input field is empty and required, add to $missing array:
if (empty($temp) and in_array($key, $required)) {
$missing[] = $key;
// if the $_POST field is one we're expecting, assign it a variable with the same name as $key:
if (in_array($key, $expected)) {
${$key} = $temp;

Those of us who don't know C might want to replace the ternary operator "$temp = is_array($value) ? $value : trim($value);"

with the easier to understand and maintain equivalent:
if (is_array($value)) {
$temp = $value;
} else {
$temp = trim($value);

On page 116 - 118:
Need to test for $name, $email, and/or $comment being set else they are undefined and you get an error message. For example:

<?php if (($missing || $errors) && isset($email)) {
echo 'value="' . htmlentities($email,
ENT_COMPAT, 'UTF-8') . '"';
} ?>>

I substituted your contact_05.php and processmail.inc.php to be sure.

On page 118:
contact_05.php has an undefined variable error for each field with the code copied directly. I assume we have to make $name = "" or something if there's nothing in the arrays. Will try that.

On page 120:
Item 5. "Add a new warning message at the top of page contact.php like this:'

This is not correct as the $missing will be NULL and FALSE if inserted at the TOP of the page and executed from there.

This block of code needs to be inserted AFTER the require('./includes/processmail.inc.php') statement for it to work correctly.

On page 124:

<?php } elseif (isset($errors['email'])) { ?>
<span class="warning">Invalid email address</span>
<?php } ?>

this is not working. i checked it as well as replaced it with contact_07.php and processemail.inc_03.php overall and neither way do they work.

On page 126:
The code in step four needs to be updated to the code that is listed in the download file. It should read: <?php if ($_POST && $mailSent) {
echo "Message body\n\n";
echo htmlentities($message, ENT_COMPAT, 'UTF-8') . "\n";
echo "Headers\n\n";
echo htmlentities($headers, ENT_COMPAT, 'UTF-8');
} ?>

On page 155:
Step 7.

The line that reads:

echo $e->getMessage();

should read:

echo $e->getMessages():

the method created in step 6. is called getMessages(), not getMessage()

On page 222:

An unnecessary ")" in the code:

if ($last == '/') || $last == '\\') {

Will result in a parse error.

On page 306-308:
This is one of those cases where the error isn't actually pointing to the right place. The issue appears to be in the connection.inc.php file at roughly line 14 (will vary depending on how you format your code).

Book code: if($connectionType == 'mysqli') {
return new mysqli($host, $user, $pwd, $db) or die('Cannot open database');

Correct code: if($connectionType == 'mysqli')
$conn = new mysqli($host, $user, $pwd, $db) or die('Cannot open database');
return $conn;

Hope this helps someone.

On page 306:
I get the error:

Fatal error: Call to a member function query() on a non-object in ...

I had to search several forums to find the solution. Apparently it's something like replacing line 15 in the connection.inc.php file to $conn = new mysqli .... and then returning $conn; on the next line. But I don't actually understand it myself. That's why I bought a book about PHP. Just thought I'd let someone know. Take care.

On page 307:
At the top of the page in the example code, the line ---return new mysqli($host, $user, $pwd, $db) or die('Cannot open database');--- does not produce the expected result. I believe the problem is with the "or die('...')" as the "new mysqli()" should never return false. Either way, this line of code as it is returns a simple "true" boolean instead of the expected mysqli object. (Tested in PHP 5.3.5)

On page 308:

Fatal error: Call to a member function query() on a non-object

Line 8: $result = $conn->query($sql) or die(mysqli_error());

PHP solution 11-2: Counting records in result set (MySQLi)

Page 308 Step 4.

On page 308:
"Fatal error: Call to a member function query() on a non-object in C:\wamp\www\php_solutions\index.php on line 8"

After a lot of frustrating attempts I finally downloaded and copied the source files directly, only changing the database info, and got the exact same error.

Result is supposed to be: "A total of x records were found."

On page 312:

Solution 11-5: step 1;

the variable $getDetails in query() does not work. I assume because it is not defined. If you substitute $sql the table is returned like the MySQLi example.

On page 320:

Fatal error: Call to a member function query() on a non-object in C:\xampplite\htdocs\phpsols\mysql\mysqli_integer.php on line 5

Using the file provided generates the error noted above. A comparison of the files code and the code described in the book on page 320 shows no difference.


    1. PHP for Absolute Beginners


      View Book

    2. Pro PHP Security


      View Book

    3. Beginning PHP and MySQL


      View Book

    4. PHP Solutions


      View Book