RU beehive logo ITEC dept promo banner
ITEC 325
2015spring
ibarland

homelectsexamshws
D2Lbreeze (snow day)

tq-db
DB connectivity

Due: 2015.Apr.23 (Wed)24 (Fri) 11:00.

You will complete your trivia-quack page by storing question in a database, and retreiving a summary list of all questions.

  1. As in previous homeworks, your page at https://php.radford.edu/~yourUserId/itec325/hw07/index.php should have a link to your question-entry form near the top, and include a list all your files (with links), and should also display their source (from Apr.24 until Jun.01).
    Also, submit your files on D2L.

    Exception: Don't publish your database password! That information should be in a small, separate file. To auto-generate your solution-listing and yet suppress this one file, you can either add an ad hoc if-statement inside the code that loops over your directory (kinda yucky), or you can generalize the function that generates the list to take in a directory and a list of filenames to exclude. (For example, search for array_diff in hw05-soln/ for an example.)

  2. (40%) Modify the ol' trivia-question entry form so once the information is validated, the question gets added to the database.

    Here is a wowtq-db-setup.sql file which you can use (either in MyPHP or Oracle SQL), to do a one-time creation the tables you'll need. (It's fine to adjust (or even ignore) this file, if you want something different. You can use the GUI interface via MySQL Admin if you prefer.)

    As discussed in lecture, be sure to guard against SQL injection — either by calling mysqli_real_escape_string on any user-provided text which becomes part of a SQL query, or (preferred, but not required) via prepared-statements. (Do this even if other validation requirements make such escaping moot.)

    Make sure you that you can insert/retrieve values that have “SQL-sensitive” characters like apostrophes, spaces, and semicolons (as well as the “html-sensitive” characters like less-than and ampersand that we've already handled). Automate this testing by adding/modifying your demo-forms (e.g. tq-edit-demo1.php) to include such characters.

  3. (30%) Make a page which shows a summary list of all questions previously entered: just the question name, the associated ability (“Wisdom” etc.)difficulty, and the minimum-levelweight. This page should also include a link to the question-entry-form page. (It does not need to be particularly pretty — just loop over the results of the database query.)

    Be sure to have a prominent link to this page near the start of your php.radford.edu/~yourUserId/hw07/index.php. (This can replace, or simply be next to, the link to the question-entry-form.)

    Be sure all your pages/programs always close any database connection they open!

  4. (30%) Each question-name on the summary list should be a link which, when clicked, brings you to a detailed-information page that includes the previous information plus the full description and who submitted the question.

    (The layout of this information does not need to be anything elegant, but it should not be prone to HTML/script injection.)

    Pro tip:

    From hw03 on, you already have a page which prints the summary information (on a successful submit); it used $_POST.

    You can re-factor that page so that instead of reading from $_POST, it reads from some other array, say, “$the-question-info”. Then:

    • To get the old behavior, $the-question-info = $_POST; followed by require("print-question-info.php");.
    • To meet this new requirement for this hw, read the database-info for a question into $the-question-info, and then you can just require("print-question-info.php"); from there!
    Adding a layer of indirection: is there any problem it can't solve?

    Note that this is asking for a link which behaves a little bit like a form-handler, since you won't have a separate URL for every single course — instead you'll have one page which (given a particular question-id) pulls the detailed information out of the database. How do you have a link which provides an argument (question-name) to another page? One easy way1 is to have the link contain the parameter(s) explicitly, e.g.<a href="questionDetail.php?questionId=43">…”, and the receiving page accesses that argument through the $_GET array (e.g., $_GET['questionId']).

    As always, to guard against HTML/script injection, be sure to call htmlspecialchars on any text which was originally provided by a user (even if retrieved from the database). (This goes for all pages, even if other validation requirements make such escaping moot).

    I'll say it again, just because it's fun: Be sure all your pages/programs always close any database connection they open!

Guidelines


1

Transmitting the arguments through $_GET of course is vulnerable to over-the-shoulder snooping; if you wanted to still use $_POST but not require a “submit” button, you can set the link's onclick attribute to be javascript which (a) sets the value of a hidden input tag, and then (b) does something like document.getElementByID( 'theFormsId' ).submit().

     

homelectsexamshws
D2Lbreeze (snow day)


©2015, Ian Barland, Radford University
Last modified 2015.Apr.20 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.