Page 447 - Beginning PHP 5.3
P. 447

Chapter 14: Manipulating MySQL Data with PHP
                           You may recognize these functions from Chapter 9 (although they ’ ve been slightly modified).
                           validateField()  checks to see if the supplied field name is within the list of fields that the user
                         forgot to fill in. If it is, a   class= “ error ”   attribute is output, which highlights the missing field
                         in red.

                          Meanwhile,   setChecked()  and  setSelected()  output the markup to pre - check a checkbox and
                          pre - select an option in a menu, respectively. Both methods expect an object derived from the
                            DataObject  class (such as a  Member  object), the name of the field to look up, and the value to
                          compare. If the supplied value matches the value of the field in the object, the markup is output.
                          These functions are used within the registration form to help prefill fields in the form, as you see in a
                          moment.



                           Enhancing the Member Class
                           The next thing to do is add some more functionality to your  Member  class. First, add a couple of extra
                         methods for retrieving   Member  objects from the database.  getByUsername()  retrieves the member with
                         the supplied username, and   getByEmailAddress()  retrieves the member with the given email address.
                          These will be used to ensure that a prospective member doesn ’ t accidentally register with a username or
                          email address that is already in the database.

                            Open up the   Member.class.php  file that you created in Chapter 13 and add the following code to the
                          file, after the existing   getMember()  method:


                               public static function getByUsername( $username ) {
                                 $conn = parent::connect();
                                 $sql = “SELECT * FROM “ . TBL_MEMBERS . “ WHERE username = :username”;

                                 try {
                                   $st = $conn- > prepare( $sql );
                                   $st- > bindValue( “:username”, $username, PDO::PARAM_STR );
                                   $st- > execute();
                                   $row = $st- > fetch();
                                   parent::disconnect( $conn );
                                   if ( $row ) return new Member( $row );
                                 } catch ( PDOException $e ) {
                                   parent::disconnect( $conn );
                                   die( “Query failed: “ . $e- > getMessage() );
                                 }
                               }

                               public static function getByEmailAddress( $emailAddress ) {
                                 $conn = parent::connect();
                                 $sql = “SELECT * FROM “ . TBL_MEMBERS . “ WHERE emailAddress =
                             :emailAddress”;

                                 try {






                                                                                                         409





                                                                                                      9/21/09   9:14:04 AM
          c14.indd   409
          c14.indd   409                                                                              9/21/09   9:14:04 AM
   442   443   444   445   446   447   448   449   450   451   452