CIS 264: Web and Database Applications

Introduction

This class seeks to take a variety of technologies you have learned (HTML, Javascript, Databases, etc) and blend them together to create database driven web applications. Specifically, we will be using the so-called LAMP stack (Linux, Apache, MySQL and PHP).

Book: Learning PHP, MySQL, and JavaScript

ClassDateSubject
11/24Introduction

Brief explanation of the web, PHP, MySQL, etc. Syllabus. Link to notepad++.

Explanation of DNS
21/31Diving more into PHP: variables, arrays, etc.

You will get your server information this class.

Lab #1: Create a form and PHP page. The form should ask for a state's 2 letter abbreviation in all capital letters. The PHP page should take the user's input and provide the state's full name. Use an associative array (and, no, just put 2 or 3 states in the array not all of them). Put it in the lab1 folder on your website. Sample solution.

Notepad++ connection setup instructions Link to zip version of notepad++ (for the lab)
32/7Loops and branching in PHP (for loops, do loops, if/then and switches). I'll show you how to combine stuff we know to make smarter PHP scripts.

Lab #2: Create a single PHP page that will accept a number and print the multiplication table for it up to its square (e.g. for 2 it should print 1x2 and 2x2. For 3 it should print 1x3, 2x3 and 3x3). The single page should show a form if no input is given. Otherwise it should show the table and a direct link to the previous number and the next number (e.g. if the page is showing 3's table, it should link to 2 and 4). Hints: use wt2 as a base. Use a for loop (pg. 81) for the loop.
42/14Files. Files and more files. You're going to start to get an idea of why you want to use a database instead of files for storing data. First, though, I want you to suffer a little bit by dealing with files. Hopefully you'll appreciate databases more when we get there. Additional file help.

Lab #3: Create an order form that saves data to a file called order.txt. The order form should have the user's name and how many bananas they need. Don't wipe out previous orders! Sample solution.

HW #1: click me. Due 3/6. You have PLENTY of time, so no whining, please, about time.
52/21Tonight we'll start talking about relational databases.

Lab #4: Go to this website and think about what kind of database structure you'd need to support the data displayed. Create the tables in your database. Don't load data. Play with the application a bit and really pay attention to the data. Submit your database design (on paper) the next time we have class.

Exam in two weeks! (3/6/2012)
62/28Continuing with SQL fun and starting to use it with PHP. See SQL Course for more help. Exam next week. I plan on having some class time for project help, too.

In-class warm-up: SQL Lab. MySQL access. Sample solution.
73/6Midterm exam. Study guide. HW1/Project due.
--3/13Spring break. Enjoy yourself.
83/20Manipulating MySQL through PHP (Ch. 10 in the book). I wrote a sample solution up for the first project. It's over here and accessible through your accounts under demo/hw1/.

Lab #6: Take the walkthrough code for the zip codes and integrate it with the area codes tables. So, if you look up 08080, it gives you back Sewell, NJ. It should also give you the possible zip codes for Sewell (all possible NJ area codes).

Update: Grades are available. Login using notepad++ and you should see a file called "grades.txt". The average on the midterm was a B. If you do not have a project grade, I can't find your project or don't know what group you were in. Email me with your group info and the location of your project.

Homework: The course project is to create a PHP/MySQL web application in some form. It can be some boring data entry app, a game, photo album, whatever. It must use PHP and MySQL. For next week, you must create a project specification which includes who is in your group (minimum 1, maximum 3) and what you plan to build. Since you only have one week to do this, it does not have to be incredibly detailed, but it should list what major functionality you will include. See
sample. You must meet the specifications you set out to receive an 'A' on the final project, so plan carefully and don't bite off more than you can chew. Of course, if you don't actually attempt something of substance, I will tell you that more is needed. The goal is to not have any projects with a score of less than an 'A'. Of particular importance is the last section. If you need help with a major part of what you want to do, note it so that I can go over it. Next week's assignment will be to create a database schema for the project.

93/27User authentication.

See this article. I told you SQL injection was a real problem.

DB access tutorials: 1, 2.

Homework: Create a database schema (on paper) for you project and turn it in next class.
104/3User authentication, part 2.

Lab #7: Finish the walkthrough by creating a page where users with the role User can access the information. Just follow the template of the admin role.

For those of you needing image processing capabilities, GD was installed on the server. Documentation.
114/10Uploading files in PHP.
124/17Some sample code for a basic shopping cart and AJAX.
134/24Working session and review. See the study guide.
145/1Final.
155/8Last class. Project presentations.

The server is paid for through the end of the month, meaning this site will disappear by the end of May. I will create a zip file of all your databases and source code for you at the end of the semester so you can have a copy. Otherwise your stuff will be obliterated.
--5/9You can download all your source code and database contents now. Go to http://yourname.lux.nu/yourname.zip (so http://demo.lux.nu/demo.zip for all the demo files). I enjoyed this class a lot. If you want to keep up with my craziness, feel free to find me on Facebook. Good luck to all you graduates.

Available snippets

Snippets are small pieces of code aimed to demonstrate one very small concept that may be helpful but I haven't necessarily gone over in class.

lastinsertWhen you insert a row into a table with an auto_increment field, sometimes you want to know what value was assigned.
quotesShows how to download a page from the internet and get data out of it.
redirectRedirect a user to a website without Javascript.
searchSearch a text field in a database.
simplevalPerform simple validation on a form.
tableOutput values from a database into a variable number of column table.
checkboxProcess checkboxes in PHP.
selectProcess select boxes in PHP.

Demo files

Demo files are everything I've done in class and sample solutions to assigned work. You can view them on the web using the links provided or view the source through notepad++. To see the code, login to the server using notepad++. You should see a folder named 'demo'. Go in there and then you will see the hierarchy of folders.

testThe most simple test page possible.
walk through 1/index.html

walk through 1/process.php

A HTML form and PHP script. Demonstrates retrieving values posted to a script and a little bit of array syntax. If you give it an animal (chicken, cow or dog) it gives you the sound that animal makes.

Index is the form.

Process is PHP code that processes the form.
walk through 2

walk through 2/direct.php
Index file shows how to use get parameters (visit it with a value like this: http://demo.lux.nu/wt2/index.php?input=5 (link).

Direct shows you how to build links that use request parameters. Imagine it is a database program and you are search/linking to specific record #'s.
walk through 3/counter.php

walk through 3/recipe.php
Counter shows how to build a simple page view counter using files.

Recipe shows how to save data to files and maintain a basic index of those files.

Read shows how to display a file in the browser. It requires a file to read as a parameter.
walk through 4/simple.php

walk through 4/lookup.php

walk through 4/tables.txt
Simple shows two ways to query a database. The first is older and less secure because it does not use parameters. The second is the preferred method in the class.

Lookup shows how to build a dropdown list from a database table and also how to search based on exact matches.

Tables has the table data.
walk through 5/basic.php

walk through 5/logout.php

walk through 5/tables.txt

Both demonstrate the use of sessions and how to leverage them to login/logout users. Remember how basic authentication is when the box pops up. Notice how index compares hashed passwords.

Logout simply ends the session.

Tables has the table data.
walk through 6/index.php

walk through 6/admin.php

walk through 6/change.php

walk through 6/library.php

walk through 6/tables.txt

This walkthrough demonstrates one way to use the database to create a role-based authentication system. Users are given roles and then those roles determine how much access they have on the site. Since admin and index share a need for database code, that code was put in library and included in both files.

Index lets you login and then displays the roles the user has. It uses cookies to prepopulate the user's name when they return.

Admin shows you a message that lets you know if you are identified as an administrator.

Change lets a logged in user change their password.

Library has a custom function, is_admin(), that returns true if the current user is an admin and false otherwise.

Tables has the table definitions and sample data. Passwords have SHA1 hashes.
walk through 7/file.php

walk through 7/uploadcsv.php

walk through 7/savedb.php

walk through 7/getdb.php

walk through 7/tables.txt

walk through 7/banks.csv
File uploading, processing and saving.

File.php takes a file and moves it to the uploads folder.

Uploadcsv takes a CSV file and puts the contents in to a database table.

Savedb takes any file and saves it in the database.

Getdb lets you download files saved in the database.

Tables has the table definitions for uploadcsv (Banks) and savedb/getdb (Files).

Banks has data that can be processed by uploadcsv and put in to the Banks table.
lab1/index.html

Sample soution to lab #1.
lab2/index.php

Sample soution to lab #2.
lab3/index.php

Sample soution to lab #3.
sqlanswers.html

Sample soution to lab #5.