This worksheet will be done over two weeks, Weeks 6 and 7 (or Weeks 7 and 8 for Wednesday groups).
The idea of this worksheet is to write a new script, called buy.php
, which will allow a user to buy a song. The effect of the buy.php
script will be to reduce the quantity in stock of that song. It will be linked to from your searchresults.php
script, so that a user can select a song to buy by clicking on a link next to that song.
What is a query string? (also see week 6 lecture notes)
A query string is extra information added onto a URL which the script needs to do its job. For example: http://edward/lecture.php?
week=11
Here, we have an imaginary script called lecture.php
. This script is designed to display any week's lecture notes, so we have to tell it which week to display. The extra information at the end of the URL, week=11 (the
query string) tells lecture.php which week to display.
It contains two parts: the query string variable name (this can be anything, we've chosen week here) plus the query string value (11 here), which is the actual value we're passing across.
You can then use $_GET with the query string variable name to read the value in, e.g for the example above:
<?php
$w = $_GET["week"]; echo "<h1>Lecture for week $w </h1>";
// .. code to display the lecture for that week…
Adding a "buy" link to your script
1.Go back to your searchresults.php
script from Worksheet 4. Within the
“while” loop which displays all the search results, add a hyperlink labelled
“Buy” which will link to the buy.php
script (not yet written), allowing the user to buy that hit. For the moment, it should read something like: echo "<a href='buy.php'>Buy this hit</a>";
Upload to Edward and test.
Writing the Query String
2. Add a query string to your link so that it passes across the ID of the current song to a new script called buy.php (which is not yet written). The query string variable name should be “songID” and the query string value should be the ID of the current song.
You might find this working answer helpful: http://www.free-map.org.uk/course/dfti/week7/
You are now going to actually write the buy.php
script. Do not worry about correct use of GET and POST just yet, this is covered in the further questions, below. For the moment you are just going to use a GET request to run buy.php.
3. The buy.php
script should actually buy the user’s chosen song, by reducing the quantity in stock by 1.
The first thing you need to do is read in the information you passed across from the searchresults.php script. Look at the lecture notes on query strings to see how to obtain information sent across via a query string. Using this information, write in your log book how you would obtain the information sent across in your case.
Now start the buy script, so that it reads in this information into a variable called $id.
4. Now you need to reduce the quantity in stock of the song the user has selected. a) You need to reduce the quantity in stock of the song by one, represented by the quantity column in the database. You will need an SQL UPDATE statement for this. Add an appropriate UPDATE statement to your script.
THE SQL UPDATE should be as below (you need to complete it):
UPDATE ???? SET quantity=quantity-1 WHERE ???? b) Finally, complete your buy script and test it. If you are using the
"dftitutorials" database, you can find all the songs and their quantities in stock here: http://edward/ewt/hits.php
These questions allow you to use hidden fields and also allow you to rewrite your purchasing system so that you use GET and POST appropriately.
5. This question allows you to practice with hidden form fields. Make a script called confirm.php
containing a form which allows the user to specify a quantity to buy. Add a link to confirm.php from searchresults.php. You should pass across the song ID from searchresults.php to confirm.php
using a query string, in the same way that you pass across the ID to buy.php.
The confirm.php
script should read in the ID from the query string and echo out a form with two fields:
- a quantity field,
- and a hidden form field containing the ID.
The form should send its information to a new script, buy2.php
, using a method of "post" (why?) c)Make a copy of buy.php called buy2.php
. Modify it so that it reads in the
ID and quantity from the form in Question 4, and uses it to reduce the quantity in stock of the selected hit by the selected amount.
6. Alter the confirm.php
script so that it displays the full details of the hit that the user has chosen. Use an SQL statement (what type?) to do this.
7. Using an if statement, add error checking to the buy.php
script, so that the hit is not bought if the quantity available in stock is less than 0, and instead an error message appears.
If you are not sure how to do this, break it down, in your logbook, into a series of logical steps in English before you write the code. Think how it could be done!