database driven

profilesani
HIT326MidtermTestS12021.docx

Part 1 Interactive Web Concepts

(Answers should be short and concise)

Question 1

Examine the below PHP script, and complete the following questions.

try{

$query = "SELECT bookid, version, book FROM library";

$statement = $db->prepare($query);

$statement -> execute();

$list = $statement->fetchall(PDO::FETCH_ASSOC);

require 'list1.html.php';

}

catch(PDOException $e){

$errors[] = "Statement error because: {$e->getMessage()}";

require 'db_error.html.php';

exit();

}

if(isset($_POST['_method']) && $_POST['_method']=='post'){

if(!empty($_POST['bookid']) && !empty($_POST['version']) && !empty($_POST['book'])){

$errors = array();

require 'db.php';

if(!$db){

require 'db_error.html.php';

exit();

}

$bookid = $_POST[‘bookid’];

$version = $_POST[‘version’];

$book = $_POST['book'];

try{

$query = "INSERT INTO library (bookid,version,book) VALUES (?,?,?)";

$statement = $db->prepare($query);

$binding = array($bookid,$version,$book);

$statement -> execute($binding);

}

catch(PDOException $e){

$errors[] = "Statement error because: {$e->getMessage()}";

require 'db_error.html.php';

exit();

}

header('Location: index.php?list');

exit();

}

else{

header('Location: index.php?new');

exit();

}

}

a) Based on your understanding of the DRY principle, Re-use the above code as much as possible without hindering the controller logic.

b) In your own words, explain how the if statement provided in the above script will operate.

c) Is there a potential reload problem in the above script? Why?

Question 2

Examine the below html file, and answer the following questions:

<!doctype html>

<html>

<head>

<meta charset='utf-8' />

<title>Week 3 examples</title>

</head>

<body>

<h1>Add Item</h1>

<p><a href='index.php?list'>Item list</a></p>

<p><a href='index.php'>Home</a></p>

<form action='index.php' method='POST'>

<input type='hidden' name='_method' value='post' />

<label for=’item-name'>Item name</label>

<input type='text' id= ‘item-name' name='Iname' />

<label for='item-number'>Item name</label>

<input type='text' id='item-number' name='Inumber' />

<label for='products'>Products</label>

<input type='text' id='products' name='products' />

<input type='submit' value='Create new item />

</form>

</body>

</html>

a) Considering the above file and based on your understanding of the adopted MVC pattern, explain what can be done in terms of restructuring the codes/files as a best practice.

-

b) Explain in your own words how the above system (assuming it is complete) would handle the following client request:

-Request to post an item (i.e. t-shirt) in the inventory items (DB).

-

Question 3

When working on your upcoming project, it is important to understand how your web application will handle client requests. Let’s assume a user visits your web application and heads to complete a product purchase. The user registers her/his details, adds a product into the cart and while submitting the order the system crashes (not responding). Explain in your own words, how should the system ideally handle this situation based on the best practices learned in this unit? *hint provide examples when possible.

Part 2 PHP Related

Question 1

Answer both parts of this question.

Part A

1) Examine carefully the following PHP file, and answer below questions:

<?php

$errors = array();

try{

$db = new PDO('sqlite:vehicle.db');

if($statement = $db->prepare("SELECT color, model, year FROM car WHERE make = ?")){

$binding = array('Mazda');

$statement -> execute($binding);

$result = $statement->fetchall(PDO::FETCH_ASSOC);

if(!empty($result)){

foreach($result as $item){

echo "<p>{$item[' color ']}, {$item['model']}, {$item['year']}</p>";

}

}

else{

echo "<p>No results</p>";

}

}

?>

a) What is the objective of this PHP code and the anticipated output (assuming it runs)?

b) The above script is missing an important component. Provide the missing code.

c) Modify the foreach loop to print the key as well as the value.

d) Write a complete php code to insert a new car in the above example. *hint multiple lines

Part B

Considering the above PHP file.

a) Create a page with a form which allows users to search for cars’ models by cars’ make.

b) Create an interactive feature that makes it mandatory for users to enter a value for the car make when using the form. *hint display an error.

c) Modify the form created in a) to allow users to add cars’ models. *hint mediate any problems that may arise due to this change.

-

Question 2

Answer all parts of this question.

A)

Assume the following variables have been declared and initialised:

$dbName = "testdb";

$host = "localhost";

$username = "username";

$password = "PHP2021";

Based on your understanding of the DRY principle, write a PHP script that will start a connection to the server, open the database testdb and store the connection in variable $dbConnect. *hint: code may be re-used.

-

B)

Examine the below PHP code. What can be done to refactor this code? Provide your answer.

<?php

$vehicle = “Car”;

if($vehicle == "Car"){

echo "Your Vehicle has four wheels!";

} elseif($vehicle == "Bike"){

echo " Your Vehicle has two wheels!";

} else{

echo "You Vehicle might have more than four wheels";

}

?>

-

C)

Examine the below PHP code, and answer the following question:

if( !_empty($_GET['list'])){

$errors = array();

$db = get_db($errors);

}

$list = null;

try{

$query = "SELECT Student_ID, Name, Course FROM Student";

$statement = $db->prepare($query);

$statement -> execute();

$list = $statement->fetchall(PDO::FETCH_ASSOC)

}

catch(PDOException $e){

$errors = "Problem with query";

require LIB.'/db_error.html.php';

exit()

}

}

1.The above script is missing important PHP elements and includes an obvious error. Provide the missing elements/codes and the correction.

-

Part 3 Database Related

(Answers should be short and concise)

Question 1

Answer the three parts of this question.

A)

A well-structured database starts from an effective Entity Relationship Diagram (ERD). For this part of the question, you will need to sketch an ERD for the following gym scenario:

“A member must have a member ID, name and contact number, can be enrolled in one or more gym session. A gym session can be enrolled in by one or more members. Each gym session is identified by a session ID, and has a name, date and instructor details (Instructor ID). An instructor has an instructor ID, name and contact number, and must only be assigned to one gym session. A gym session can have one or more instructors assigned in it.”

*show entities, relationship types and cardinalities. Crow’s foot model notations recommended.

-

B)

Provide the complete SQL query to create the gym session table on MySQL database.

*datatypes can be determined based on your assumptions (but must be relevant to columns).

-

C)

Using the above scenario and ERD, give an example on how folksonomy as a tagging system is implemented in databases. Write SQL queries or draw diagram if necessary.

*hint: you may propose to add a new table.

-

Question 2

Considering MySQL, explain at least Two aspects about databases that need to remain

online 24x7? Provide an example for each aspect.

-

-The End-

Semester 1, 2021: HIT326 - Database-Driven Web Applications, Midterm Test

Page 1 of 2