PHP Programming                                                      Spring 2013

                        Exercise: PDO - PHP Data Objects

See your text and the "Programming Examples" link for help with these questions.
This exercise uses the table (file) named Item from the PHP to MySQL Basics 
exercise, with data stored by load utility. Item has these attributes: id, 
decimal(5) primarykey, name varchar(15), cost decimal(5,2).
                                          
1. Code a script named db.php with statement to instantiate a new PDO object 
   passing parameters for data source name, user name, password, and error 
   mode options. Include a try block to invoke this statement, plus a catch
   (PDOException $e) {} block with statements to present an error message
   and invoke exit(). The options parameter is an array variable with this
   element value and key:  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION.

2. Prepare an html form to collect an item id, name and cost. This form's action
   is to invoke insertItem.php a script to insert an item row that uses the post
   method for parameter passing. Include input tags with type text fields for 
   name = "id", name = "name", name = "cost", and for the submit type.
   
3. Create a PHP script that includes db.php to connect to a database, defines 
   $sql, a PHP string for an SQL INSERT statement, and uses PDO syntax inside 
   a try{} to prepare a statement object, bind parameters to that statement, 
   have that statement invoke execute(), rowCount(), and closeCursor(). Provide
   a catch for PDOException and print an error message.   
   
4. Prepare an html form to collect an item id and cost. This form's action
   is to invoke a php program to update the cost in one item row and uses the 
   post method for parameter passing. Include input tags for type text fields 
   for name = 'id', cost = 'cost', and for the submit type.

5. Code the php update program referenced in the action option of the input form.
   a. Assign the $_POST[] parameter values for 'id' and 'cost' to local variables.
   b. Define an update string variable, with named parameters for those variables.
   c. Then 
      try{} to prepare statement object (e.g. $statement) the value returned 
         when the $db object invokes prepare($sql);
      have $statement bind values to the named parameters by having it invoke
      bindValue(:namedParm, $var) for corresponding variables $id and $cost;
      have statement invoke execute( ) to execute the update string 
   d. catch(PDOException $e);

6. Create a PHP script that connects to a database, defines a PHP string for 
   an SQL SELECT statement and uses PDO syntax to prepare,[ bind parameters,] 
   execute a query, then fetch query results for printing before closing the 
   connection. 
   a. For the select string, specify any needed named parameters (e.g. in 
      where clause).
   b. Assign to a statement object (e.g. $statement) the value returned when
      $db invokes prepare($sql).
   c. Have $statement invoke execute( ) for query results that can be fetched.
   d. Should more than one result row be expected, construct a while with an
      expression that has $statement invoke fetch( ). The while statement 
      body could print local variables bound to named parameters.
   e. After fetching the result set, have $statement invoke closeCursor().