Page 449 - Beginning PHP 5.3
P. 449

Chapter 14: Manipulating MySQL Data with PHP
                                   $st = $conn- > prepare( $sql );
                                   $st- > bindValue( “:username”, $this- > data[“username”], PDO::PARAM_STR );
                                   $st- > bindValue( “:password”, $this- > data[“password”], PDO::PARAM_STR );
                                   $st- > bindValue( “:firstName”, $this- > data[“firstName”], PDO::PARAM_STR );
                                   $st- > bindValue( “:lastName”, $this- > data[“lastName”], PDO::PARAM_STR );
                                   $st- > bindValue( “:joinDate”, $this- > data[“joinDate”], PDO::PARAM_STR );
                                   $st- > bindValue( “:gender”, $this- > data[“gender”], PDO::PARAM_STR );
                                   $st- > bindValue( “:favoriteGenre”, $this- > data[“favoriteGenre”],
                             PDO::PARAM_STR );
                                   $st- > bindValue( “:emailAddress”, $this- > data[“emailAddress”],
                             PDO::PARAM_STR );
                                   $st- > bindValue( “:otherInterests”, $this- > data[“otherInterests”],
                             PDO::PARAM_STR );
                                   $st- > execute();
                                   parent::disconnect( $conn );
                                 } catch ( PDOException $e ) {
                                   parent::disconnect( $conn );
                                   die( “Query failed: “ . $e- > getMessage() );
                                 }

                               }
                           If you ’ ve worked through the previous chapter and this chapter so far, there should be no surprises here.
                           insert()  builds an SQL statement to insert the data stored in the current  Member  object into the
                          database. Notice that the statement doesn ’ t attempt to insert a value for the   id  field, because this is
                         generated automatically by MySQL.
                           Then the method prepares the statement with   PDO::prepare() , binds each of the placeholders to the
                          appropriate value in the   Member  object ’ s  $data  array property, and executes the statement by calling
                            PDOStatement::execute() . If there were any problems with the insertion, the exception is caught and
                          displayed and the application exits.


                           Creating the Registration Script

                           Now that you ’ ve added the required functionality to your common code file and  Member  class file,
                         you ’ re ready to build the registration script itself. Save the following code as   register.php  in your
                           book_club  folder:
                               < ?php

                             require_once( “common.inc.php” );

                             if ( isset( $_POST[“action”] ) and $_POST[“action”] == “register” ) {
                               processForm();
                             } else {
                               displayForm( array(), array(), new Member( array() ) );
                             }

                             function displayForm( $errorMessages, $missingFields, $member ) {
                               displayPageHeader( “Sign up for the book club!” );

                               if ( $errorMessages ) {




                                                                                                         411





                                                                                                      9/21/09   9:14:05 AM
          c14.indd   411
          c14.indd   411                                                                              9/21/09   9:14:05 AM
   444   445   446   447   448   449   450   451   452   453   454