Lab 05 – Handle Database Data (Part II)
Intended Learning Outcome:
On completion of this lab, students are expected to be able to:
· Write in PHP code to retrieve data from a MySQL database table
· Display database data using different form controls (e.g. textbox, radio button, etc.)
· Write in PHP code to insert, update and delete records(s) in MySQL database
Resource Required:
1. A text editor such as Notepad++
2. WAMP or XAMPP package which contains Apache (web server) and mySQL (relational database)
3. A browser such as Google Chrome or Mozilla Firefox
Task:
1. Re-write a php file on Lab 4 Task 1 to shows a table containing all the records in the customers table. Add a new column to the table containing a hyperlink in each cell for deleting the corresponding record from the table. Save the file as Lab05_1a.php
i. Each hyperlink generated has a query string (?custID=xxxx) appended where xxxx is the custID for the row. Even without a form, we can pass the custID data to the PHP file Lab05_1b.php using get method.
ii. Create a php file Lab05_1b.php which gets the customer ID sent by Lab05_1a.php for the customer record to be deleted.
· Delete the record in the Customers table that matches with the customer ID.
· Redirect the browser to display the page Lab05_1a.php after the deleting the record.
2. Re-write a php file on Task 1 to shows a table containing all the records in the customers table. Modify the first column of the table containing a [Delete] button in each cell for deleting the corresponding record. Save the file as Lab05_2a.php
i. Create a form contains both table and a hidden element which store the custID value for the record to be deleted.
ii. When user click the [Delete] button, it will call a javascript function setValue() and pass the custID as parameter.
iii. Write a javascript function setValue() which will set the value to hidden element and submit the form to Lab05_2b.php.
iv. Create a php file Lab05_2b.php which gets the customer ID sent by Lab05_2a.php for the customer record to be deleted.
· Delete the record in the Customers table that matches with the customer ID.
· Redirect the browser to display the page Lab05_2a.php after the deleting the record.
CHALLENGE EXERCISE
Create a php file on to shows a table containing all the records in the Customers table. The first column of the table containing a hyperlink for updating the corresponding record. Save the file as Lab05_3a.php
i. After a hyperlink is clicked, it should retrieve data according to $_GET['custID'] and then display those data in a form under the table.
ii. After editing the data and click the [Update Record] button, all form data is submitted by post method to file Lab05_3b.php .
iii. When the [Cancel] button is clicked, it should run a Javascript code:
onclick="window.location.href='Lab05_3a.php';" />
Create a php file Lab05_3b.php to execute a SQL UPDATE command and then return to the page Lab05_3a.php using header function.
2020-06-15