Lab 8

Lab 8

Project 3

Working with Databases

Goal

To get started with Project 3 and to understand...

  1. How to connect to a MySQL database
  2. How to excecute SQL statements using PHP
  3. How to connect HTML forms with PHP scripts

Connecting to server

Remember that you must...

  1. save your PHP files,
  2. upload them to the remote web server,
  3. open your scripts via an absolute URL and
  4. "hard refresh" your web browser to test any changes

Use WinSCP to connect to our remote server.

Details about your specific server, userid and password were emailed to you. Here is the general information:

  • Hostname: ftp.sienasellbacks.com   or   ftp.breimer.net
  • Username: userid@sienasellbacks.com   or   userid@breimer.net
  • Password: Sent via email
  • Port: 21
  • Use FTP; Do not use SFTP or SCM

Be sure to replace userid with your actual Siena userid; But, do not add @siena.edu

Getting started and overview

  1. Download project3.zip and unzip the folder
  2. This represents a solution to Project 2, which is good starting point for Project 3. This solution shows four different ways to generate the courses page:
    • Courses1: Hard-coded with custom CSS
    • Courses2: Hard-coded with with Bootstrap columns
    • Courses3: Programmatically in table format
    • Courses4: Programmatically in nested list format
  3. In this lab, we will generate the courses page from a database table and we will create a form and script for inserting courses into the table.

Making sure you can connect

  1. Open functions.php in your editor of choice
  2. Locate the db_connect() function
  3. Un-comment the line for connecting to your server:
    breimer.net uses the prefix breimern and
    sienasellbacks.com uses sienasel
  4. Un-comment the echo statement in the db_connect() function
  5. Open connect.php in your editor of choice and notice it requires functions.php
  6. Read about the require function to understand what it does. Click the link
  7. Upload your entire project3 folder to the remoter server
  8. Navigate to your project3 folder URL, i.e.,
    http://www.breimer.net/userid/projects/project3/ or
    http://www.sienasellbacks.com/userid/projects/project3/
    Be sure to use your actual userid in the URL above.
  9. You should see a folder listing of the project3 folder
  10. Click on connect.php
  11. You should see a Connection successful message
  12. Open functions.php and comment the echo statement in db_connect so it will no longer print the connection successful message.

Make you own courses table

  1. Open create_courses_table.php in your editor of choice
  2. Notice that this script definites a string called sql (which is an SQL query for creating a table), then it runs the query by connecting to the database (using db_connect()) and finally it runs the query (i.e., $db->query($sql))
  3. Modify the query so that it creates a table called userid_courses, where userid is your userid .
  4. Save and upload create_courses_table.php
  5. Navigate to your project3 folder URL, i.e.,
    http://www.breimer.net/userid/projects/project3/ or
    http://www.sienasellbacks.com/userid/projects/project3/
    Be sure to use your actual userid in the URL above.
  6. Click on create_courses_table.php
  7. You should see a message Courses table created successfully

Verify that the table was create correctly

  1. In your project3 folder, create a new file called show_table.php
  2. Add the following code:
    code
  3. Be sure to replace ebreimer_courses with your table name, i.e, userid_courses where userid is your userid.
  4. Save and upload show_table.php
  5. Navigate to your project3 folder URL
  6. Click on show_table.php
  7. You should the see column names and field types of the courses table.

TASK 1: Modify show_table.php

  1. Modify show_table.php so that it ouputs the column names as <th>'s in an HTML table:
    code
  2. Rather than output a block of HTML, use the project2 framework to output a basic page
  3. Store the output of you script as a string and pass it to the make_basic_page functions.
  4. Pass in "Show table" as the page_name

Inserting courses into your table

  1. In your project3 folder, create a new file called insert_course.php
  2. Add the following code:
    code
  3. Replace ebreimer_courses with your table name, i.e, userid_courses where userid is your userid.
  4. Save and upload insert_course.php
  5. Navigate to your project3 folder URL, i.e.,
    http://www.breimer.net/userid/projects/project3/ or
    http://www.sienasellbacks.com/userid/projects/project3/
  6. Click on insert_course.php
  7. You should have a form for submitting new courses and all newly inserted courses should appear at the bottom of the form.

TASK 2: Modify insert_course.php

  1. Modify insert_course.php so that the form lets you specify all the fields, except cid
  2. You do not have to enter the cid because it is an auto-incremented primary key that the database maintains.
  3. Rather than output a block of HTML, use the project2 framework to output a basic page
  4. Integrate the code from show_table.php so that you output the column names as <th>'s as the first row of the table.

DELIVERABLE

None. To get credit for lab you must work productively for the 2 hour period.

Do not share

While it is OK to help other students with concepts and general trouble-shooting, you should not share code. It is expected that each individual project will be unique.