CS 199 Willamette University Spring, 2019
 
Programming in PHP, Databases with MySQL,
and Web Applications
 

Modifying a Database with PHP

This lab is the second in a series that gradually provides experience with PHP with MySQL.

The Framework for using INSERT and DELETE Statements

The previous lab described the following PHP code for establishing a PDO connection to a database:

<?php
   // identify details and credentials for accessing the database
   $server   = 'local host';
   $database = ... ;
   $username = ... ;
   $password = ... ;

   // Establish an environment to report trouble, if a problem occurs
   try {
       $pdo = new PDO("mysql:host=local host;dbname=$database", $username, $password);    

       // set the PDO error mode to throw exception when run-time error encountered
       $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

       // provide as much protection as possible to counteract possible injection attacks
       $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
   }
   catch (Exception $e) {
       echo "error connecting to $database \n";
       echo "full error message " . $e->getMessage() . "\n";
       echo "exiting\n";
       exit;
   }

   echo "connected to database '$database'\n";

Within this context, the lab described two common approaches for executing SQL queries:

Those same approaches apply to INSERT and UPDATE queries, as well. For example, the following code inserts a new record into the faculty table, as described previously.

In reviewing these statements, several observations may be helpful.

A complete program with these insert statements is available as a text file.

UPDATE and DELETE Statements

The approaches just described for implementing INSERT statements also apply to UPDATE and DELETE statements.

Steps for this lab

The lab on More Practice with MySQL suggested that you create a table of second-floor rooms in Ford Hall, using these main steps:

Although you manually typed individual SQL statements for these steps in the earlier labs, your experience with PHP can help streamline some of these steps. Translating the earlier lab to a PHP program is the basis for this lab.

In what follows, develop your program in small stages, so you can focus on each step before going onto the next step!

For the first 3 steps below, use the $pdo->query approach for executing queries.

  1. Write a CREATE statement within a PHP program to establish the room table, simply placing the earlier CREATE statement in a PHP program that defines and executes the table.

  2. To insert records for rooms 201 through 224, a program could define and execute one INSERT statement and then place the statement in a loop to cycle through all identified rooms.

  3. To delete the rooms 203, 205, and 222, create an array containing these three numbers. Then develop a DELETE statement to remove a single record, and place it in a loop that will cycle through the room numbers in the array.

For the remaining steps, use the $pdo->prepare and $pdo->execute approach, as described above.

  1. Write UPDATE queries to change the information stored for the classrooms 201, 202, 203, and 224. (You may decide to do this with 4 separate UPDATE statements, or you may discover a pattern that will simplify the overall work.)

  2. Write additional UPDATE statements to modify the data stored for Rooms 209, 218, and 220.




created 11 February 2019
revised 20 February 2019
Valid HTML 4.01! Valid CSS!