Differences

This shows you the differences between two versions of the page.

Link to this comparison view

lab_4_-_php_and_mysql [2017/09/06 12:10] (current)
anthony created
Line 1: Line 1:
 +====== Lab 4: PHP and MySQL ======
  
 +
 +Due October 13, 2016. 
 +Objectives
 +
 +Upon successful completion of this lab, you will have demonstrated the ability to: 
 +Write a PHP program that reads data from a web form and stores the data in a MySQL database
 +Perform basic field validation
 +Reference
 +
 +See lecture notes on connection strings. You may also read this. PHP gang.
 +Depending on whether you use your own mysql or zenit will depend whether you just use mysql or mysqli (zenit uses mysqli, which may require you to reconfigure your Apache and PHP using the --with-mysqli. instead of --with-mysql option. MAKE SURE YOU BACKUP HTDOCS CONF AND CGI-BIN DIRECTORIES IN YOUR HOME DIRECTORY THEN MOVE THEM BACK IN THE SERVER ROOT.
 +Your mysql server db-mysql.zenit. Your database name is your zenit username. Your password is your original zenit password your table will be lab 4, with the fields of the form. Refer to the instructions on zenit here 
 +Required Materials
 +
 +None
 +Lab Preparation
 +
 +IMPORTANT:
 +
 +
 +
 +You should read the Lecture Notes and sample programs before doing this lab.
 +If you haven'​t done all the required things for the last lab, please complete these first. (Note that these labs will look a fair bit like your assignment work!)
 +You might want to look at this sample program which is very similar to what I showed in class. Please use the file method I should in the example lab in class (under Week 4)
 +Introduction
 +
 +Part I - Repopulating Selects, Radio Buttons and Checkboxes
 +
 +Your form will have a number of fields, these fields will be the same as what you put in the table in your MySQL database. Your table will be called fsossregister. Name the program fsosstshirt.php. When an error occurs in an input field on a web form, the form should be redisplayed,​ with all the input fields repopulated (i.e., they should contain the values the user entered before they hit submit) and an error message for each field in error. To repopulate text fields, you need to set the value attribute to the value POSTed back for the field. That is, if there is a POST for that input field, then echo it back into the text box. The form fields will have firstName, lastName as text boxes, sex as a radio button with male or female, a checkbox indicate if they want multiple shirts and if yes a textbox indicated the number and a dropdown menu with size, small, medium, large or xlarge.
 +
 +<input type="​text"​ name="​firstName"​ value="<?​php if ($_POST['​firstName'​]) echo $_POST['​firstName'​];​ ?>>
 +
 +The idea is the same for a textarea, but rather than use the value attribute, you would post back between the open and close textarea tags:
 +
 +<​textarea name="​mytext"><?​php if ($_POST['​mytext'​]) echo $_POST['​mytext'​];​ ?></​textarea>​
 +
 +To set a checkbox to what was selected before, we have to add CHECKED (or CHECKED="​CHECKED"​) to the INPUT tag.
 +
 +<input type="​checkbox"​ name="​sex"​ <?php if ($_POST['​sex'​]) echo "​CHECKED";​ ?>>
 +
 +For radio buttons, you need to add CHECKED to the appropriate INPUT tag:
 +
 +Male<​input type="​radio"​ name="​sex"​ value="​m"​ <?php if ($_POST['​sex'​] == "​m"​) echo "​CHECKED";​ ?>>
 +Female<​input type="​radio"​ name="​sex"​ value="​f"​ <?php if ($_POST['​sex'​] == "​f"​) echo "​CHECKED";​ ?>>
 +For drop downs, you need to add SELECTED to the appropriate OPTION tag:
 +
 +<select name="​size">​
 +<option name="​s"​ <?php if ($_POST['​s'​]) echo "​SELECTED";​ ?>>​Small</​option>​
 +<option name="​m"​ <?php if ($_POST['​m'​]) echo "​SELECTED";​ ?>>​Medium</​option>​
 +etc...
 +
 +If you want to use multiple selections in drop downs, you can do so by using arrays in the HTML tags and testing to see if a value is in the array. The POSTed value of sel ($_POST['​sel'​]) will be an array, and it will contain all the values selected. Note that we have to test to see in the sel array has been posted back first (isset), because if it doesn'​t exist we will get an error from the in_array function because the $_POST['​sel'​] array does not exist!
 +
 +<select name="​sel[]">​
 +<option value="​a"​ <?php if (isset($_POST['​sel'​])) { if (in_array("​a",​ $_POST['​sel'​])) echo "​SELECTED";​} ?>>​A</​option>​
 +<option value="​b"​ <?php if (isset($_POST['​sel'​])) { if (in_array("​b",​ $_POST['​sel'​])) echo "​SELECTED";​} ?>>​B</​option>​
 +</​select>​
 +Create a new directory under your cgi-bin directory in your document root called lab4.
 +Copy your fsoss-register.php lab to the lab4 directory.
 +Revise your program so that all fields are validated and repopulated. For title make sure one radio button is selected, for the checkboxes make sure at least either yes or no is checked (otherwise, they are not attending either day!), and the default in the drop down ("​--Please choose--"​) must not be selected (i.e. they have to pick a t-shirt size).
 +Part II - Storing and Retrieving form data in a DB
 +
 +Here is a sample DB access program.
 +
 +Create a table in your int322 DB to store data from the form. You may pick any reasonable data type and length for the columns in your DB table. For the checkboxes you will need two columns since neither, both or just one may be checked (you can't store two values in one column!). Do this through the MySQL command line - do not use PHP!
 +Revise fsoss-register.php so that it is a single, self-referential program that does the following:
 +If all the data is valid, then your program should store the form data in a MySQL database when the submit button is pressed, then display all the data in the table with labels in a nicely formatted HTML table, one record per line (don't print the form or input fields again). That is, if you've submitted the form 4 times with valid fields, there should be four records in the DB and you should print all four. This means you need to execute a "​select"​ DB query to retrieve all the records after performing an "​insert"​ to store the current form data. Add a link to the bottom of the display page to go back to your fsoss-register.php to add another record.
 +If any data is not valid, then your program should redisplay the web form, with all the fields repopulated with what the user entered, and an error message displayed next to the field(s) in error.
 +Part III - Dynamic Links
 +
 +Sometimes we would like to generate links dynamically - that is, create a link that is based on the value of something we don't know beforehand. For example, in your assignment you are asked to generate a link that says either "​Delete"​ or "​Restore"​ depending on what is stored in the DB for that record. ​
 +
 +To generate a dynamic link, you simply put the code for a link, then drop into PHP mode to generate the dynamic content:
 +
 +<a href="​somepage.php"><?​php if ($x == "​something"​) print $y; else print $z; ?></​a>​
 +
 +If this dynamic link needs to pass some value to the program it calls (somepage.php),​ add GET values that the program can retrieve through $_GET:
 +
 +<a href="​somepage.php?​var=<?​php if ($x == "​something"​) print '​c';​ else print '​d';​ ?>"><?​php if ($x == "​something"​) print '​a';​ else print '​b';​ ?></​a>​
 +
 +You can do this in a loop if you are generating links for all records in a DB.
 +Your record will indicate the number of t-shirts ordered by record (use last name as your search in the table. The total number of tshirts available less the number ordered. There are 200 total T-shirts available.
 +
 +Revise your last program so that the user has an option through a dynamic link at the end of each record that says, "​Cancel"​ or order more indicating the number of tshirts still available. When clicked, it should "​uncheck"​ both Yes and No (that is, change the values you store in the DB to represent "​unchecked"​ for both.
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported