PHP & MySQL: The Missing Manual [PDF]

iMovie '11 & iDVD: The Missing Manual by David Pogue and Aaron Miller. iPad 2: The Missing Manual by J.D. Biersdorfe

12 downloads 55 Views 28MB Size

Recommend Stories


PHP & MySQL The Missing Manual 2nd Edition Pdf
Live as if you were to die tomorrow. Learn as if you were to live forever. Mahatma Gandhi

PHP-MySQL
Nothing in nature is unbeautiful. Alfred, Lord Tennyson

PHP & MySQL
Don't be satisfied with stories, how things have gone with others. Unfold your own myth. Rumi

[PDF] Learning PHP, MySQL JavaScript
Don't ruin a good today by thinking about a bad yesterday. Let it go. Anonymous

Dreamweaver Php Mysql Tutorial Pdf
We can't help everyone, but everyone can help someone. Ronald Reagan

[PDF] Learning PHP, MySQL JavaScript
The wound is the place where the Light enters you. Rumi

PHP & MySQL
Happiness doesn't result from what we get, but from what we give. Ben Carson

[PDF] Learning PHP, MySQL JavaScript
Love only grows by sharing. You can only have more for yourself by giving it away to others. Brian

[PDF] Learning PHP, MySQL JavaScript
Don’t grieve. Anything you lose comes round in another form. Rumi

Internet Programcılığı (PhP, MySQL)
Never let your sense of morals prevent you from doing what is right. Isaac Asimov

Idea Transcript


PHP & MySQL The book that should have been in the box®

Brett McLaughlin

Beijing | Cambridge | Farnham | Köln | Sebastopol | Tokyo

PHP and MySQL: The Missing Manual by Brett McLaughlin

Copyright © 2012 Brett McLaughlin. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or [email protected]. Printing History: November 2011:

First Edition.

Revision History: 2011-11-09

First release

See http://oreilly.com/catalog/errata.csp?isbn=9780596515867 for release details.

The Missing Manual is a registered trademark of O’Reilly Media, Inc. The Missing Manual logo, and “The book that should have been in the box” are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media is aware of a trademark claim, the designations are capitalized. While every precaution has been taken in the preparation of this book, the publisher assumes no responsibility for errors or omissions, or for damages resulting from the use of the information contained in it.

ISBN-13: 978-0-596-51586-7

Contents The Missing Credits. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi

Part One:

Chapter 1:

PHP and MySQL Basics PHP: What, Why, and Where?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Gathering Your Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Writing Your First Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Running Your First Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Writing Your Second Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Upload Your HTML, CSS, and PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Running Your Second Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21



Chapter 2:

PHP Syntax: Weird and Wonderful. . . . . . . . . . . . . . . . . . . . . . . . . 25 Get Information from a Web Form. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Working with Text in PHP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 The $_REQUEST Variable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 What Do You Do with User Information?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53



Chapter 3:

MySQL and SQL: rel="stylesheet" type="text/css" /> PHP & MySQL: The Missing Manual Example 1-1 Welcome!

Hello there. So I hear you're learning to be a PHP programmer!

Why don't you type in your name for me:





Chapter 1: PHP: What, Why, and Where?

17

Writing Your Second Program

Enter your name:

 Note  You can download this HTML, along with the rest of the book’s sample files, from www.missingmanuals.com/

cds/phpmysqlmm. You can also get the CSS and images used by the samples, which let you give your programs a little extra visual pizzazz. Still, especially as you’re just getting started, you’ll learn a lot more if you’ll type in the PHP code for these programs yourself.

Almost nothing about this page should be new to you. All it does is reference an external CSS style sheet, provide a text greeting like sayHello.php did, and then define a form into which users can type their names. The only thing that should have caught your attention is this line, in the form definition:

This code means that your form is going to submit its information to a program called sayHelloWeb.php, a new PHP program you’re about to write. Once the form is submitted, sayHelloWeb.php takes over and prints out the welcome message.

Write a PHP Script Now that you’ve got an HTML page sending information to sayHelloWeb.php, you need to actually write that code. When you write PHP to run on the Web, it’s not much different from the program you’ve already written (page 14). You have to get information a little differently, because there’s no command line that a user can type into. But other than that, things stay pretty much the same. Open up a new text editor and type the PHP code shown here; it should look sort of like an HTML-ized version of the sayHello.php program: PHP & MySQL: The Missing Manual Example 1-1

18

PHP & MySQL: The Missing Manual

Hello,

Great to meet you. Welcome to the beginning of your PHP programming odyssey.



Writing Your Second Program



Save this program as sayHelloWeb.php, and be sure you’ve got your file in plain text with the right extension. The first thing you probably noticed here is that this file looks a whole lot like HTML. In fact, compared to sayHello.php, the first PHP program you wrote, this version might look like a style of programming that’s a lot easier to learn. That’s because once you’re using PHP to work and interact with web pages, a lot of what your PHP programs will do is insert rel="stylesheet" type="text/css" /> PHP & MySQL: The Missing Manual Example 2-1 Join the Missing Manual (Digital) Social Club

Please enter your online connections below:

First Name:
Last Name:
E-Mail Address:
Facebook URL:
Twitter Handle:

 Tip  For more information on how HTML is used in this code, see the box below.

26

PHP & MySQL: The Missing Manual

Get Information from a Web Form POWER USERS’ CLINIC

HTML Should Be Semantically Meaningful You may have noticed some pretty big changes in this HTML from the simple form from Chapter 1 (page 18). In that chapter, the form used

tags to break up the form labels and input boxes, and manually formatted the form labels with tags. That got the job done, but it’s not a good use of HTML.

By making the HTML mean something, a browser (and other HTML authors) know what things actually are in your form: labels are meant for, well, labeling things. Fields are grouped together with fieldset . And italic and bold are left to your CSS, just as they should be.

Anytime you’re writing HTML, you’re actually structuring your page. So a form tag doesn’t really do anything visually; it just lets a browser know, “Hey, here’s a form.” When you use tags like , though, you’re not describing structure; you’re telling the browser how something should look. That’s really not what HTML is for, though; that’s the job of CSS.

What’s really cool here is that now your CSS can do an even better job of styling your form. Since you’ve gotten rid of formatting in the HTML, you can style all your form labels the same way—perhaps by bolding them, right-aligning them, and adding a right margin of 5 pixels. The same is true of your sets of fields; you might put a border around related fields, which is exactly what’s going on in the CSS applied to this form. In fact, to see how the CSS affects these HTML elements, check out Figure 2-1.

In this form, though, all the formatting has been pulled out. Instead, all the labels are identified with the label element, and a for attribute. That identifies the labels as labels—regardless of how those labels end up looking—and also connects each label with the specific input field to which it matches. There’s also a fieldset element that surrounds the different blocks within the form: one for the labels and text fields, and a second for the form buttons. This arrangement also provides semantic information: information that has meaning.

Truthfully, if you’re new to making your pages semantically meaningful, it may take you a little time to get used to using HTML just for structure, and keeping all your style in CSS. But stick with it; your pages will look better, and anyone who has to update your pages down the line will thank you.

Save this file as socialEntryForm.html. To make sure your HTML is just the way you want, go ahead and upload it to your server, in the ch02/ directory. Make sure you’ve got the book’s CSS in the right place—under css/ in your server’s root—and then open a browser and head over to your HTML form. You should see something like Figure 2-1. In sayHelloWeb.php, you used $_REQUEST to get submitted form information, and then asked specifically for the “name” value. But with this new form, there’s a lot more information being sent from the form. To get all that information, you need to create a new script called getFormInfo.php, and enter in this code:



Chapter 2: PHP Syntax: Weird and Wonderful

27

Get Information from a Web Form

PHP & MySQL: The Missing Manual Example 2-1

Here's a record of what information you submitted:

First Name:
Last Name:
E-Mail Address:
Facebook URL:
Twitter Handle:



Figure 2-1

This web form is a pretty typical entry page for a user to fill in. But what happens when this form gets submitted? You’re about to find out, and in fact, take control of all this entered information.

28

PHP & MySQL: The Missing Manual

 Note  If you want to start taking a little more control of your scripts, you can name this program something

other than getFormInfo.php. Just be sure that you also update socialEntryForm.html and change the form’s action attribute value to match your own custom script name.

Get Information from a Web Form

You can already see what’s going on here. In addition to grabbing the value of the “first_name” and “last_name” fields—similar to getting the value of the “name” field in sayHelloWeb.php—you’re using $_REQUEST to pull in the values the user entered into the other form fields. Go back to your web form, and enter your information. Then submit the form, and you should see the result of getFormInfo.php running. Your browser should show you something like Figure 2-2. In fact, this is the way you’ll use the $_REQUEST variable in most of your PHP programs: echo $_REQUEST['FORM_INPUT_FIELD_NAME'];

Figure 2-2

Almost everything in PHP begins with some piece of information submitted via either an HTML Web form or another PHP script.

Create Your Own Variables You may have times where you don’t want to just spit out the value of a field. Think back to your first program, sayHello.php (the version that didn’t run on the Web). In that program, you created your own variable: $name = trim(fgets(STDIN));



Chapter 2: PHP Syntax: Weird and Wonderful

29

Get Information from a Web Form

PHP lets you create all the variables you want. Just think of a descriptive name (for suggestions, see the box on page 31), and put a dollar sign before that name, like this: $numberSix = 6; $thisIsMyName = "Brett"; $carMake = "Honda";

With this chunk of code in mind, go back to your new program, getFormInfo.php. Instead of just using echo to print out the submitted information, store each piece of information in a variable. Then you can use that information however you want, and as many times as you want.  Warning  The $get_user_query in this code is intentionally incomplete. Those three dots won’t really

work; you’d need to include a WHERE piece that locates the user that was just added.

158

PHP & MySQL: The Missing Manual

This code gets you the user from the rel="stylesheet" type="text/css" /> PHP & MySQL: The Missing Manual User Profile C. J. Wilson

C. J. Wilson is the Texas Rangers ace pitcher. After years of relief pitching, C. J. debuted as a Rangers starter in 2010, and went on to become the staff's ace in 2011. He's a lefty, known

160

PHP & MySQL: The Missing Manual

for his strong opinions, those thick ropy necklaces, and a wicked set of stuff.

C. J. is into auto racing as much as baseball, and would rather be on safari in South Africa that sitting around do nothing at home. He's also been known to do some pretty wacky bits to get to rub shoulders with Carlton Cuse and Damon Lindelof, the writers and masterminds behind the hit TV show LOST.

Get in touch with C. J.:

  • ...by emailing him at [email protected]
  • ...by checking them out on Facebook
  • ...by following them on Twitter


Show Me the User

 Note  The bio and picture here are new, and not things you should already have in your users table. They’re

just nice touches for a user’s profile page. Just a name and a few links for email and Twitter was pretty sparse. Don’t worry, though. You’ll be adding a profile picture and bio to your rel="stylesheet" type="text/css" /> PHP & MySQL: The Missing Manual User Profile $first_name $last_name

Chapter 6: Generating Dynamic Web Pages

161

Show Me the User

$bio

Get in touch with $first_name:

  • ...by emailing them at $email
  • ...by checking them out on Facebook
  • ...by following them on Twitter
 Warning  This example is helpful to think through, but it’s not valid HTML or PHP. So don’t try and view

this code in a browser...you won’t get anything useful, and it certainly won’t work as a PHP script. Still, you should see that almost everything on the page really just represents information in the rel="stylesheet" type="text/css" /> PHP & MySQL: The Missing Manual User Profile $first_name $last_name

$bio

Get in touch with $first_name:

  • ...by emailing them at $email
  • ...by checking them out on Facebook
  • ...by following them on Twitter
 Note  Remember, save this with a .php extension, in your scripts/ directory. That might be ch06/scripts/ if

you’re using the book’s structure, or just your website’s scripts/ directory, if you’re putting all your PHP from all the chapters in a single place.

164

PHP & MySQL: The Missing Manual

Show Me the User

Now there are definitely some things here that are a bit odd: • Where’s the PHP? There’s no yet, and certainly no code. • Those variables are definitely PHP, not HTML. An HTML page won’t know what to do with them. • Where does the rel="stylesheet" type="text/css" /> PHP & MySQL: The Missing Manual User Profile

Smile Life

When life gives you a hundred reasons to cry, show life that you have a thousand reasons to smile

Get in touch

© Copyright 2015 - 2024 PDFFOX.COM - All rights reserved.