Activity: Get started with database interfacing
(no submission)
Purpose:
- Experience with a simple web program
- Get started with database programming
- Help you get started with your course project, if you choose to do a web-based project
You may work individually or with your project team member(s).
In this activity, you will be exposed to simple web programming.
This activity provides an overview of database programming,
a very brief introduction to HTML, CSS, and PHP; as well as web deployment.
Please note: our focus is on the database part, not the web programming part.
The intention of this activity to help you get started with your project
(if you plan to develop a web app using PHP).
If you are already familiar with HTML, CSS, and PHP, feel free to use this
activity to refresh your memory or simply do it for fun.
If you do not plan to develop a PHP project, you may do this activity using
any language of your choice.
Part 1: Get started with HTML and PHP
- Understand how database programming works in general.
- Connect and upload files to the web server.
- Create a simple HTML page (let's name it index.html) and deploy it on the web server.
- Feel free to use
bootstrap
to style your page.
- You may create an HTML file from strach or use the provided
template.
- In your index.html, add a link to your simpleform.php
- Create a simple form (let's name it simpleform.php) and deploy it on the web server
- In your simpleform.php,
modify the form such that the form data are submitted to form-processing.php.
Specify how the form data will be packaged and transmitted to form-processing.php,
by setting the method attribute to post.
<form name="mainForm" action="form-processing.php" method="post">
For more experience, try changing the method attribute to get and observe the URL
(in the browser's address bar).
- Create a PHP program, named form-processing.php, to process the form submission.
- Let's experience with "sticky form" and function in PHP
Part 2: Prepare your database
- Create a database (you may use your existing database if you'd prefer)
- In the database, create a table (let's name it friends)
containing at least 3 attributes: name, major, and year.
CREATE TABLE friends (
name varchar(30) NOT NULL,
major varchar(10) NOT NULL,
year int NOT NULL,
PRIMARY KEY (name) );
Alternatively, you may create a table consisting of any attributes of your choice.
Part 3: Connect to your database
- Create a PHP program that serves as a utility program to connect to the database
(let's name it connectdb.php).
You may create a the program from scratch or use the provided
connectdb.php (text version).
- Include code to connect to the database in your simpleform.php
- In your simpleform.php,
write code to insert a friend's information,
retrieve data from the friends table and
display them on the screen.
- create a function (named addFriend) that takes 3 parameters: name, major, and year.
The function inserts the form data entry
(name, major, year) into a friends table
in the database.
- Add code to call the addFriend function.
Use the pass-by-value concept to pass the values of name, major, and year
retrieved from the form to the function.
To test, enter a friend's information and click the add button.
Inspect the friends table in the database.
- Create another function (named getAllFriends) that
returns an array of all rows of data from the friends table.
- Add code to call the getAllFriends function
and save the returning value in a variable (for example, friends).
- Add code to display the result of the getAllFriends() function.
You may modify your interface the way you like.
Get creative. Feel free to add additional elements you feel should be included and have fun!
To host your web resources, use of the following options or use any web server of your choice
To connect your PHP to the database
Additional resource:
Basic web deployment
(borrowed from CS 4640)