/////////////////////////////////////////////////////////////////////Task 1(a)//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Index.php

Menu

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Main.css /* the styles for the HTML elements */ html { background-color: rgb(192, 192, 192); } body { font-family: Arial, Helvetica, sans-serif; width: 760px; margin: 0 auto; padding: 0 2em; background-color: white; border: 1px solid black; } header { border-bottom: 2px solid black; padding: .5em 0; } header h1 { color: black; } main { } aside { float: left; width: 150px; } section { float: left; width: 500px; } footer { clear: both; border-top: 2px solid black; } footer p { text-align: right; font-size: 80%; } h1 { font-size: 150%; margin: .5em 0; } h2 { font-size: 120%; margin: .25em 0 .5em; } h1, h2 { color: rgb(208, 133, 4); } ul { list-style-type: none; margin: 0; padding-left: 0; padding-bottom: 1em; } li { padding-bottom: 0.5em; } a { color: rgb(41, 64, 124); font-weight: bold; } a:hover { color: rgb(208, 133, 4); } br { clear: left; } table { border: 1px solid black; border-collapse: collapse; margin-bottom: 1em; } td, th { border: 1px dashed black; padding: .2em .5em .2em .5em; text-align: left; } form { } /* the styles for classes */ .right { text-align: right; } .first_paragraph { margin-top: 0; } .last_paragraph { margin-bottom: 2em; } /* the styles for the div tags that divide the page into sections */ #left_column { float: left; width: 150px; text-align: center; } #right_column { float: left; padding-left: 1em; padding-bottom: 2em; } /* Additional styles for the Product Manager application */ #add_product_form { margin: .5em 0; } #add_product_form label { width: 6em; padding-right: 1em; padding-bottom: .5em; float: left; } #add_product_form input { float: left; } #add_product_form input[text] { width: 15em; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// category_nav.php ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// footer.php ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// header.php Task 1a

Task 1a

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Product Manager category_list.php

Category List

Name  

Add Category

List Products

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// index.php //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Add Product






View Product List

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// product_list

Product List

Code Name Price  

Add Product

List Categories

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// product_list.php

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// product_view.php

<?php echo $image_alt; ?>

List Price: $

Discount: %

Your Price: $ (You save $)

Quantity:

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// category_db.php prepare($query); $statement->execute(); return $statement; } function get_category_name($category_id) { global $db; $query = 'SELECT * FROM categories WHERE categoryID = :category_id'; $statement = $db->prepare($query); $statement->bindValue(':category_id', $category_id); $statement->execute(); $category = $statement->fetch(); $statement->closeCursor(); $category_name = $category['categoryName']; return $category_name; } function add_category($name) { global $db; $query = 'INSERT INTO categories (categoryName) VALUES (:name)'; $statement = $db->prepare($query); $statement->bindValue(':name', $name); $statement->execute(); $statement->closeCursor(); } function delete_category($category_id) { global $db; $query = 'DELETE FROM categories WHERE categoryID = :category_id'; $statement = $db->prepare($query); $statement->bindValue(':category_id', $category_id); $statement->execute(); $statement->closeCursor(); } ?> ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// database.php getMessage(); include('../errors/database_error.php'); exit(); } ?> ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// product_db.php prepare($query); $statement->bindValue(':category_id', $category_id); $statement->execute(); $products = $statement->fetchAll(); $statement->closeCursor(); return $products; } function get_product($product_id) { global $db; $query = 'SELECT * FROM products WHERE productID = :product_id'; $statement = $db->prepare($query); $statement->bindValue(':product_id', $product_id); $statement->execute(); $product = $statement->fetch(); $statement->closeCursor(); return $product; } function delete_product($product_id) { global $db; $query = 'DELETE FROM products WHERE productID = :product_id'; $statement = $db->prepare($query); $statement->bindValue(':product_id', $product_id); $statement->execute(); $statement->closeCursor(); } function add_product($category_id, $code, $name, $price) { global $db; $query = 'INSERT INTO products (categoryID, productCode, productName, listPrice) VALUES (:category_id, :code, :name, :price)'; $statement = $db->prepare($query); $statement->bindValue(':category_id', $category_id); $statement->bindValue(':code', $code); $statement->bindValue(':name', $name); $statement->bindValue(':price', $price); $statement->execute(); $statement->closeCursor(); } ?> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Database Error

There was an error connecting to the database.

The database must be installed as described in the appendix.

MySQL must be running as described in chapter 1.

Error message:

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Error

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Shopping Cart - under construction

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// my_guitar_shop1.sql -- create and select the database DROP DATABASE IF EXISTS my_guitar_shop1; CREATE DATABASE my_guitar_shop1; USE my_guitar_shop1; -- MySQL command -- create the tables CREATE TABLE categories ( categoryID artistID INT(11) NOT NULL AUTO_INCREMENT, categoryName artistName VARCHAR(255) NOT NULL, PRIMARY KEY (categoryID) ); CREATE TABLE products ( productID albumID INT(11) NOT NULL AUTO_INCREMENT, categoryID artistID INT(11) NOT NULL, productCode albumCode VARCHAR(10) NOT NULL UNIQUE, productName albumName VARCHAR(255) NOT NULL, listPrice listPrice DECIMAL(10,2) NOT NULL, PRIMARY KEY (productID) ); CREATE TABLE orders ( orderID trackID INT(11) NOT NULL AUTO_INCREMENT, customerID customerID INT NOT NULL, orderDate trackDate DATETIME NOT NULL, PRIMARY KEY (orderID) ); -- insert data into the database INSERT INTO categories VALUES (1, 'HipHop'), (2, 'House'), (3, 'Rave'); INSERT INTO products VALUES (1, 1, 'strat', 'Fender Stratocaster', '699.00'), (2, 1, 'les_paul', 'Gibson Les Paul', '1199.00'), (3, 1, 'sg', 'Gibson SG', '2517.00'), (4, 1, 'fg700s', 'Yamaha FG700S', '489.99'), (5, 1, 'washburn', 'Washburn D10S', '299.00'), (6, 1, 'rodriguez', 'Rodriguez Caballero 11', '415.00'), (7, 2, 'precision', 'Fender Precision', '799.99'), (8, 2, 'hofner', 'Hofner Icon', '499.99'), (9, 3, 'ludwig', 'Ludwig 5-piece Drum Set with Cymbals', '699.99'), (10, 3, 'tama', 'Tama 5-Piece Drum Set with Cymbals', '799.99'); -- create the users CREATE USER IF NOT EXISTS mgs_user@localhost IDENTIFIED BY 'pa55word'; CREATE USER IF NOT EXISTS mgs_tester@localhost IDENTIFIED BY 'pa55word'; -- grant privleges to the users GRANT SELECT, INSERT, DELETE, UPDATE ON * TO mgs_user@localhost; GRANT SELECT ON products TO mgs_tester@localhost; /////////////////////////////////////////////////////////////////////Task 1(b)//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// task.sql -- create and select the database DROP DATABASE IF EXISTS task; CREATE DATABASE task; USE task; -- MySQL command -- create the tables CREATE TABLE modules ( modulesID INT(11) NOT NULL AUTO_INCREMENT, modulesName VARCHAR(255) NOT NULL, PRIMARY KEY (modulesID) ); CREATE TABLE lecturer ( lecturerID INT(11) NOT NULL AUTO_INCREMENT, artistID INT(11) NOT NULL, albumCode VARCHAR(10) NOT NULL UNIQUE, albumName VARCHAR(255) NOT NULL, listPrice VARCHAR(255) NOT NULL, PRIMARY KEY (lecturerID) ); CREATE TABLE moderator ( moderatorID INT(11) NOT NULL AUTO_INCREMENT, orderID INT NOT NULL, moderatorDate DATETIME NOT NULL, PRIMARY KEY (moderatorID) ); -- insert data into the database INSERT INTO modules VALUES (1, 'modules'), (2, 'lecturer'), (3, 'moderator'); INSERT INTO lecturer VALUES (1, 1, 'strat', 'Fender Stratocaster', '699.00'), (2, 1, 'les_paul', 'Gibson Les Paul', '1199.00'), (3, 1, 'sg', 'Gibson SG', '2517.00'), (4, 1, 'fg700s', 'Yamaha FG700S', '489.99'), (5, 1, 'washburn', 'Washburn D10S', '299.00'), (6, 1, 'rodriguez', 'Rodriguez Caballero 11', '415.00'), (7, 2, 'precision', 'Fender Precision', '799.99'), (8, 2, 'hofner', 'Hofner Icon', '499.99'), (9, 3, 'ludwig', 'Ludwig 5-piece Drum Set with Cymbals', '699.99'), (10, 3, 'tama', 'Tama 5-Piece Drum Set with Cymbals', '799.99'); -- create the users CREATE USER IF NOT EXISTS mgs_user@localhost IDENTIFIED BY 'pa55word'; CREATE USER IF NOT EXISTS mgs_tester@localhost IDENTIFIED BY 'pa55word'; -- grant privleges to the users GRANT SELECT, INSERT, DELETE, UPDATE ON * TO mgs_user@localhost; GRANT SELECT ON lecturer TO mgs_tester@localhost; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// main.css html { background-color: rgb(192, 192, 192); } body { font-family: Arial, Helvetica, sans-serif; width: 760px; margin: 0 auto; padding: 0 2em; background-color: white; border: 1px solid black; } header { border-bottom: 2px solid black; padding: .5em 0; } header h1 { color: black; } main { } aside { float: left; width: 150px; } section { float: left; width: 500px; } footer { clear: both; border-top: 2px solid black; } footer p { text-align: right; font-size: 80%; } h1 { font-size: 150%; margin: 0; padding: .5em 0 .25em; } h2 { font-size: 120%; margin: 0; padding: .25em 0 .5em; } h1, h2 { color: rgb(208, 133, 4); } ul { margin: 0 0 1em 0; padding: 0 0 0 2.5em; } li { margin: 0; padding: 0; } a { color: rgb(41, 64, 124); font-weight: bold; } a:hover { color: rgb(208, 133, 4); } form { margin: 0; } label { width: 5em; float: left; text-align: right; margin-right: 1em; margin-top: .25em; margin-bottom: .75em; } table { width: 70%; border-collapse: collapse; } td, th { padding: .25em 0; } br { clear: both; } /* the styles for the table header and footer */ #cart_header th { border-bottom: 2px solid black; } #cart_footer td { text-align: right; border-top: 2px solid black; } /* the styles for the classes */ .right { text-align: right; } .left { text-align: left; } .cart_qty { text-align: right; width: 3em; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// index.php ['name' => 'Anita Primary Lecturer ', 'cost' => '149.50'], '1MMS-6289' => ['name' => 'Mashu Secondary Lecturer', 'cost' => '199.50'], '1MMS-3408' => ['name' => 'Percival Moderator', 'cost' => '299.50'], ]; // Get the action to perform $action = filter_input(INPUT_POST, 'action'); if ($action === NULL) { $action = filter_input(INPUT_GET, 'action'); if ($action === NULL) { $action = 'show_add_item'; } } // Add or update cart as needed switch($action) { case 'add': $key = filter_input(INPUT_POST, 'productkey'); $quantity = filter_input(INPUT_POST, 'itemqty'); $product = $products[$key]; murach\cart\add_item($cart, $key, $quantity, $product); $_SESSION['cart13'] = $cart; header('Location: .?action=show_cart'); break; case 'update': $new_qty_list = filter_input(INPUT_POST, 'newqty', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); foreach($new_qty_list as $key => $qty) { if ($cart[$key]['qty'] != $qty) { murach\cart\update_item($cart, $key, $qty); } } $_SESSION['cart13'] = $cart; header('Location: .?action=show_cart'); break; case 'show_cart': include('cart_view.php'); break; case 'show_add_item': include('add_item_view.php'); break; case 'empty_cart': $cart = []; $_SESSION['cart13'] = $cart; include('cart_view.php'); break; } ?> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// cart_view.php Task 1b

Task 1b

Your Cart

There are no items in your cart.

$item) : $cost = number_format($item['cost'], 0); $total = number_format($item['total'], 0); ?>
Lecturers Item Menu Item Cost Quantity Item Total
$ $

Click "Update Cart" to update quantities in your cart.
Enter a quantity of 0 to remove an item.

Add Item

Empty Cart

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// cart.php 0) { // If item already exists in cart, update quantity if (isset($cart[$key])) { $quantity += $cart[$key]['qty']; update_item($cart, $key, $quantity); } else { // Add item $item = [ 'name' => $product['name'], 'cost' => $cost = $product['cost'], 'qty' => $quantity, 'total' => $cost = $product['cost'] * $quantity, ]; $cart[$key] = $item; } } } // Update an item in the cart function update_item(array &$cart, string $key, int $quantity) { if (isset($cart[$key])) { if ($quantity <= 0) { unset($cart[$key]); } else { $cart[$key]['qty'] = $quantity; $total = $cart[$key]['cost'] * $cart[$key]['qty']; $cart[$key]['total'] = $total; } } } // Get cart subtotal function get_subtotal(array $cart, int $decimals = 2) { $subtotal = 0; foreach ($cart as $item) { $subtotal += $item['total']; } $subtotal_f = number_format($subtotal, $decimals); return $subtotal_f; } } ?> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// add_item_view.php Task 1b

Task 1b

Add Item



View Cart

/////////////////////////////////////////////////////////////////////Task 2//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// percy.sql -- create and select the database DROP DATABASE IF EXISTS percy; CREATE DATABASE percy; USE percy; -- MySQL command -- create the tables CREATE TABLE artist ( artistID INT(11) NOT NULL AUTO_INCREMENT, artistName VARCHAR(255) NOT NULL, PRIMARY KEY (artistID) ); CREATE TABLE album ( albumID INT(11) NOT NULL AUTO_INCREMENT, artistID INT(11) NOT NULL, albumCode VARCHAR(10) NOT NULL UNIQUE, albumName VARCHAR(255) NOT NULL, listPrice DECIMAL(10,2) NOT NULL, PRIMARY KEY (albumID) ); CREATE TABLE track ( trackID INT(11) NOT NULL AUTO_INCREMENT, customerID INT NOT NULL, trackDate DATETIME NOT NULL, PRIMARY KEY (trackID) ); -- insert data into the database INSERT INTO artist VALUES (1, 'artist'), (2, 'album'), (3, 'track'); INSERT INTO album VALUES (1, 1, 'strat', 'Fender Stratocaster', '699.00'), (2, 1, 'les_paul', 'Gibson Les Paul', '1199.00'), (3, 1, 'sg', 'Gibson SG', '2517.00'), (4, 1, 'fg700s', 'Yamaha FG700S', '489.99'), (5, 1, 'washburn', 'Washburn D10S', '299.00'), (6, 1, 'rodriguez', 'Rodriguez Caballero 11', '415.00'), (7, 2, 'precision', 'Fender Precision', '799.99'), (8, 2, 'hofner', 'Hofner Icon', '499.99'), (9, 3, 'ludwig', 'Ludwig 5-piece Drum Set with Cymbals', '699.99'), (10, 3, 'tama', 'Tama 5-Piece Drum Set with Cymbals', '799.99'); -- create the users CREATE USER IF NOT EXISTS mgs_user@localhost IDENTIFIED BY 'pa55word'; CREATE USER IF NOT EXISTS mgs_tester@localhost IDENTIFIED BY 'pa55word'; -- grant privleges to the users GRANT SELECT, INSERT, DELETE, UPDATE ON * TO mgs_user@localhost; GRANT SELECT ON album TO mgs_tester@localhost; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// main.css /* the styles for the html elements */ html { background-color: rgb(192, 192, 192); } body { margin-top: 0; font-family: Arial, Helvetica, sans-serif; width: 760px; margin: 0 auto; background-color: white; border: 1px solid black; padding: .5em 2em; } header { margin: 0; border-bottom: 2px solid black; } header h1 { margin: 0; padding: .5em 0; color: black; } main { margin: 0; } aside { float: left; width: 150px; } nav ul { list-style-type: none; margin-left: 0; padding-left: 0; } nav ul li { padding-bottom: 0.5em; } section { float: left; width: 500px; padding-bottom: 1.5em; } footer { clear: both; margin-top: 1em; border-top: 2px solid black; } footer p { text-align: right; font-size: 80%; margin: 1em 0; } h1 { font-size: 150%; margin: 0; padding: .5em 0 .25em; } h2 { font-size: 120%; margin: 0; padding: .25em 0 .5em; } h1, h2 { color: rgb(208, 133, 4); } ul { margin: 0 0 1em 0; padding: 0 0 0 2.5em; } li { margin: 0; padding: 0; } a { color: rgb(41, 64, 124); font-weight: bold; } a:hover { color: rgb(208, 133, 4); } table { border: 1px solid black; border-collapse: collapse; } td, th { border: 1px dashed black; padding: .2em .5em .2em .5em; vertical-align: top; text-align: left; } form { margin: 0; } br { clear: left; } /* the styles for classes */ .right { text-align: right; } .last_paragraph { margin-bottom: 2em; } .margin_top_increase { margin-top: 1em; } /******************************************************************** * Additional styles for the Product Manager application ********************************************************************/ #add_product_form { margin: .5em 0 1em; } #add_product_form label { width: 6em; padding-right: 1em; padding-bottom: .5em; float: left; } #add_product_form input { float: left; } #add_product_form input[text] { width: 15em; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// index.php prepare($queryCategory); $statement1->bindValue(':category_id', $category_id); $statement1->execute(); $category = $statement1->fetch(); $category_name = $category['artistName']; $statement1->closeCursor(); // Get all categories $query = 'SELECT * FROM artist ORDER BY artistID'; $statement = $db->prepare($query); $statement->execute(); $categories = $statement->fetchAll(); $statement->closeCursor(); // Get album for selected category $queryProducts = 'SELECT * FROM album WHERE artistID = :category_id ORDER BY albumID'; $statement3 = $db->prepare($queryProducts); $statement3->bindValue(':category_id', $category_id); $statement3->execute(); $album = $statement3->fetchAll(); $statement3->closeCursor(); ?> Task 2

Task 2

Product List

Code Name Price  

Add Product

List Categories

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// error.php Task 2

Task 2

Error

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// delete_product.php prepare($query); $statement->bindValue(':product_id', $product_id); $success = $statement->execute(); $statement->closeCursor(); } // Display the Product List page include('index.php'); ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// prepare($query); $statement->bindValue(':category_id', $category_id); $statement->execute(); $statement->closeCursor(); // Display the Category List page include('category_list.php'); } ?> ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// delete_error.php Task 1

Task 1

Database Error

There was an error connecting to the database.

The database must be installed as described in the appendix.

MySQL must be running as described in chapter 1.

Error message:

 

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// database.php getMessage(); include('database_error.php'); exit(); } ?> ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// category_list.php prepare($query); $statement->execute(); $artist = $statement->fetchAll(); $statement->closeCursor(); ?> Task 2

Task 2

Category List

Name  

Add Category

List Products

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// add_product_form.php prepare($query); $statement->execute(); $artist = $statement->fetchAll(); $statement->closeCursor(); ?> Task 2

Task 2

Add Product






View Product List

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// add_product.php prepare($query); $statement->bindValue(':category_id', $category_id); $statement->bindValue(':code', $code); $statement->bindValue(':name', $name); $statement->bindValue(':price', $price); $statement->execute(); $statement->closeCursor(); // Display the Product List page include('index.php'); } ?> /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// add_category.php prepare($query); $statement->bindValue(':category_name', $name); $statement->execute(); $statement->closeCursor(); // Display the Category List page include('category_list.php'); } ?> /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// newfolder index.php query($query); $category = $category->fetch(); $category_name = $category['artistName']; // Get all artist $query = 'SELECT * FROM artist ORDER BY artistID'; $artist = $db->query($query); // Get album for selected category $query = "SELECT * FROH album WHERE artistID = $category_id ORDER BY albumID"; $album = $db->query($query); ?> Task 2
Product List

Code Name Price
Task 2
Database Error

There was an error connecting to the database.

The database must be installed as described in appendix A.

The database must be running as described in chapter l.

Error message:

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// database.php getMessage(); include('database_error.php'); exit() ; } ?> /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// add_product_form.php query($query); ?> How to use PHP with MySQL chtml xmlns="ht tp://www.w3.org/19 9 9/xhtml"> Task 2 clink rel="stylesheet" type="text/css" href="main.css" />
Add Product

cinput type="input" name="code" />


How to use PHP with MySQL
View Product List
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// add_product.php exec($query); // Display the Product List page include('index.php'); } ?> /////////////////////////////////////////////////////////////////////Task 3(a)//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// uploadform.php Upload Image

Upload Image

Images to be uploaded




Images in the directory

No images uploaded.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// main.css /* the styles for the HTML elements */ html { background-color: rgb(192, 192, 192); } body { font-family: Arial, Helvetica, sans-serif; width: 760px; margin: 0 auto; padding: 0 2em; background-color: white; border: 1px solid black; } header { border-bottom: 2px solid black; padding: .5em 0; } header h1 { color: black; } main { margin-bottom: 1em; } h1 { font-size: 150%; margin: .5em 0 .25em; } h2 { font-size: 120%; margin: .5em 0; } h1, h2 { color: rgb(208, 133, 4); } a { color: rgb(41, 64, 124); font-weight: bold; } a:hover { color: rgb(208, 133, 4); } img { border: none; vertical-align: middle; } br { clear: left; } ul { list-style-type: none; margin-bottom: 1em; padding-left: 0; } li { margin-bottom: 0.5em; } form { margin: .25em 0 .5em; } #upload_button { margin: .5em 0; } #upload_form label { padding-left: 1em; padding-bottom: .5em; } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// index.php /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// image_util.php 1 || $height_ratio > 1) { // Calculate height and width for the new image $ratio = max($width_ratio, $height_ratio); $new_height = round($old_height / $ratio); $new_width = round($old_width / $ratio); // Create the new image $new_image = imagecreatetruecolor($new_width, $new_height); // Set transparency according to image type if ($image_type == IMAGETYPE_GIF) { $alpha = imagecolorallocatealpha($new_image, 0, 0, 0, 127); imagecolortransparent($new_image, $alpha); } if ($image_type == IMAGETYPE_PNG || $image_type == IMAGETYPE_GIF) { imagealphablending($new_image, false); imagesavealpha($new_image, true); } // Copy old image to new image - this resizes the image $new_x = 0; $new_y = 0; $old_x = 0; $old_y = 0; imagecopyresampled($new_image, $old_image, $new_x, $new_y, $old_x, $old_y, $new_width, $new_height, $old_width, $old_height); // Write the new image to a new file $image_to_file($new_image, $new_image_path); // Free any memory associated with the new image imagedestroy($new_image); } else { // Write the old image to a new file $image_to_file($old_image, $new_image_path); } // Free any memory associated with the old image imagedestroy($old_image); } ?> /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// file_util.php /////////////////////////////////////////////////////////////////////Task 3(b)//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// main.css /* the styles for the HTML elements */ html { background-color: rgb(192, 192, 192); } body { font-family: Arial, Helvetica, sans-serif; width: 760px; margin: 0 auto; padding: 0 2em; background-color: white; border: 1px solid black; } header { border-bottom: 2px solid black; padding: .5em 0; } header h1 { color: black; } main { } aside { float: left; width: 150px; } section { float: left; width: 500px; } footer { clear: both; border-top: 2px solid black; } footer p { text-align: right; font-size: 80%; } h1 { font-size: 150%; margin: .5em 0; } h2 { font-size: 120%; margin: .25em 0 .5em; } h1, h2 { color: rgb(208, 133, 4); } ul { list-style-type: none; margin: 0; padding-left: 0; padding-bottom: 1em; } li { padding-bottom: 0.5em; } a { color: rgb(41, 64, 124); font-weight: bold; } a:hover { color: rgb(208, 133, 4); } br { clear: left; } table { border: 1px solid black; border-collapse: collapse; margin-bottom: 1em; } td, th { border: 1px dashed black; padding: .2em .5em .2em .5em; text-align: left; } form { } /* the styles for classes */ .right { text-align: right; } .first_paragraph { margin-top: 0; } .last_paragraph { margin-bottom: 2em; } /* the styles for the div tags that divide the page into sections */ #left_column { float: left; width: 150px; text-align: center; } #right_column { float: left; padding-left: 1em; padding-bottom: 2em; } /* Additional styles for the Product Manager application */ #add_product_form { margin: .5em 0; } #add_product_form label { width: 6em; padding-right: 1em; padding-bottom: .5em; float: left; } #add_product_form input { float: left; } #add_product_form input[text] { width: 15em; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// index.php

Menu

Product Manager

Product Catalog

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// header.php My Guitar Shop

My Guitar Shop

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// footer.php ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// product manager index.php getCategory($category_id); $categories = $categoryDB->getCategories(); $products = $productDB->getProductsByCategory($category_id); // Display the product list include('product_list.php'); break; case 'delete_product': // Get the IDs $product_id = filter_input(INPUT_POST, 'product_id', FILTER_VALIDATE_INT); $category_id = filter_input(INPUT_POST, 'category_id', FILTER_VALIDATE_INT); // Delete the product $productDB->deleteProduct($product_id); // Display the Product List page for the current category header("Location: .?category_id=$category_id"); break; case 'show_add_form': $categories = $categoryDB->getCategories(); include('product_add.php'); break; case 'add_product': $category_id = filter_input(INPUT_POST, 'category_id', FILTER_VALIDATE_INT); $code = filter_input(INPUT_POST, 'code'); $name = filter_input(INPUT_POST, 'name'); $price = filter_input(INPUT_POST, 'price', FILTER_VALIDATE_FLOAT); if ($category_id == NULL || $category_id == FALSE || $code == NULL || $name == NULL || $price == NULL || $price == FALSE) { $error = "Invalid product data. Check all fields and try again."; include('../errors/error.php'); } else { $current_category = $categoryDB->getCategory($category_id); // Create the Product object $product = new Product(); $product->setCategory($current_category); $product->setCode($code); $product->setName($name); $product->setPrice($price); // Add the Product object to the database $productDB->addProduct($product); // Display the Product List page for the current category header("Location: .?category_id=$category_id"); } break; } ?> /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// product_add.php

Add Product






View Product List

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// product_list.php

Product List

getName(); ?>

Code Name Price  
getCode(); ?> getName(); ?> getPriceFormatted(); ?>

Add Product

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// catalog index.php getCategory($category_id); $categories = $categoryDB->getCategories(); $products = $productDB->getProductsByCategory($category_id); include('product_list.php'); break; case 'view_product': $categories = $categoryDB->getCategories(); $product_id = filter_input(INPUT_GET, 'product_id', FILTER_VALIDATE_INT); $product = $productDB->getProduct($product_id); include('product_view.php'); break; } ?> ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// product_list.php

getName(); ?>

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// product_view.php

getName(); ?>

<?php echo $product->getImageAltText(); ?>

List Price: $getPrice(); ?>

Discount: getDiscountPercent(); ?>%

Your Price: $getDiscountPrice(); ?> (You save $getDiscountAmount(); ?>)

Quantity:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// category.php id = 0; $this->name = ''; } public function getID() { return $this->id; } public function setID(int $value) { $this->id = $value; } public function getName() { return $this->name; } public function setName(string $value) { $this->name = $value; } } ?> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// category_db.php query($query); $categories = []; foreach ($result as $row) { $category = new Category(); $category->setID($row['categoryID']); $category->setName($row['categoryName']); $categories[] = $category; } return $categories; } public function getCategory($category_id) { $db = Database::getDB(); $query = "SELECT * FROM categories WHERE categoryID = '$category_id'"; $statement = $db->query($query); $row = $statement->fetch(); $category = new Category(); $category->setID($row['categoryID']); $category->setName($row['categoryName']); return $category; } } ?> ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// database.php getMessage(); include('../errors/database_error.php'); exit(); } } return self::$db; } } ?> ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// product.php category = null; $this->id = 0; $this->name = ''; $this->description = ''; $this->price = 0; } public function getCategory() { return $this->category; } public function setCategory(Category $value) { $this->category = $value; } public function getID() { return $this->id; } public function setID(int $value) { $this->id = $value; } public function getCode() { return $this->code; } public function setCode(string $value) { $this->code = $value; } public function getName() { return $this->name; } public function setName(string $value) { $this->name = $value; } public function getPrice() { return $this->price; } public function getPriceFormatted() { $formatted_price = number_format($this->price, 2); return $formatted_price; } public function setPrice(float $value) { $this->price = $value; } public function getDiscountPercent() { $discount_percent = 30; return $discount_percent; } public function getDiscountAmount() { $discount_percent = $this->getDiscountPercent() / 100; $discount_amount = $this->price * $discount_percent; $discount_amount = round($discount_amount, 2); $discount_amount = number_format($discount_amount, 2); return $discount_amount; } public function getDiscountPrice() { $discount_price = $this->price - $this->getDiscountAmount(); $discount_price = number_format($discount_price, 2); return $discount_price; } public function getImageFilename() { $image_filename = $this->code . '.png'; return $image_filename; } public function getImagePath() { $image_path = '../images/' . $this->getImageFilename(); return $image_path; } public function getImageAltText() { $image_alt = 'Image: ' . $this->getImageFilename(); return $image_alt; } } ?> ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// product_db.php getCategory($category_id); $query = 'SELECT * FROM products WHERE products.categoryID = :category_id ORDER BY productID'; $statement = $db->prepare($query); $statement->bindValue(":category_id", $category_id); $statement->execute(); $rows = $statement->fetchAll(); $statement->closeCursor(); foreach ($rows as $row) { $product = new Product(); $product->setCategory($category); $product->setId($row['productID']); $product->setCode($row['productCode']); $product->setName($row['productName']); $product->setPrice($row['listPrice']); $products[] = $product; } return $products; } public function getProduct($product_id) { $db = Database::getDB(); $query = 'SELECT * FROM products WHERE productID = :product_id'; $statement = $db->prepare($query); $statement->bindValue(":product_id", $product_id); $statement->execute(); $row = $statement->fetch(); $statement->closeCursor(); $categoryDB = new CategoryDB(); $category = $categoryDB->getCategory($row['categoryID']); $product = new Product(); $product->setCategory($category); $product->setId($row['productID']); $product->setCode($row['productCode']); $product->setName($row['productName']); $product->setPrice($row['listPrice']); return $product; } public function deleteProduct($product_id) { $db = Database::getDB(); $query = 'DELETE FROM products WHERE productID = :product_id'; $statement = $db->prepare($query); $statement->bindValue(':product_id', $product_id); $statement->execute(); $statement->closeCursor(); } public function addProduct($product) { $db = Database::getDB(); $category_id = $product->getCategory()->getID(); $code = $product->getCode(); $name = $product->getName(); $price = $product->getPrice(); $query = 'INSERT INTO products (categoryID, productCode, productName, listPrice) VALUES (:category_id, :code, :name, :price)'; $statement = $db->prepare($query); $statement->bindValue(':category_id', $category_id); $statement->bindValue(':code', $code); $statement->bindValue(':name', $name); $statement->bindValue(':price', $price); $statement->execute(); $statement->closeCursor(); } } ?> ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// error.php

Error

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// database_error.php

Database Error

There was an error connecting to the database.

The database must be installed as described in the appendix.

MySQL must be running as described in chapter 1.

Error message:

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// cart index.php

Shopping Cart - under construction

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// main.css html { background-color: rgb(192, 192, 192); } body { font-family: Arial, Helvetica, sans-serif; width: 760px; margin: 0 auto; padding: 0 2em; background-color: white; border: 1px solid black; } header { border-bottom: 2px solid black; padding: .5em 0; } header h1 { color: black; } main { } aside { float: left; width: 150px; } section { float: left; width: 500px; } footer { clear: both; border-top: 2px solid black; } footer p { text-align: right; font-size: 80%; } h1 { font-size: 150%; margin: 0; padding: .5em 0 .25em; } h2 { font-size: 120%; margin: 0; padding: .75em 0 0; } h1, h2 { color: rgb(208, 133, 4); } /* styles for the form */ fieldset { margin: 1em; padding-top: 1em; } legend { font-weight: bold; font-size: 85%; } label { float: left; width: 10em; text-align: right; margin-top: .25em; margin-bottom: .5em; } input, select { margin-left: 0.5em; margin-bottom: 0.5em; } select { width: 11em; } br { clear: both; } span { vertical-align: middle; } .error { color: red; } .notice { color: red; font-size: 67%; text-align: right; } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// index.php getFields(); $fields->addField('first_name'); $fields->addField('last_name'); $fields->addField('phone', 'Use 888-555-1234 format.'); $fields->addField('email', 'Must be a valid email address.'); $action = filter_input(INPUT_POST, 'action'); if ($action === NULL) { $action = 'reset'; } else { $action = strtolower($action); } switch ($action) { case 'reset': // Reset values for variables $first_name = ''; $last_name = ''; $phone = ''; $email = ''; // Load view include 'view/register.php'; break; case 'register': // Copy form values to local variables $first_name = trim(filter_input(INPUT_POST, 'first_name')); $last_name = trim(filter_input(INPUT_POST, 'last_name')); $phone = trim(filter_input(INPUT_POST, 'phone')); $email = trim(filter_input(INPUT_POST, 'email')); // Validate form data $validate->text('first_name', $first_name); $validate->text('last_name', $last_name); $validate->phone('phone', $phone); $validate->email('email', $email); // Load appropriate view based on hasErrors if ($fields->hasErrors()) { include 'view/register.php'; } else { include 'view/success.php'; } break; } ?> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// view success.php

Success

The following registration information has been successfully submitted.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// register.php
User Information getField('first_name')->getHTML(); ?>
getField('last_name')->getHTML(); ?>
getField('phone')->getHTML(); ?>
getField('email')->getHTML(); ?>
Submit Registration
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// header.php My Guitar Shop

Register for an Account

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// footer.php /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// model fields.php name = $name; $this->message = $message; } public function getName() { return $this->name; } public function getMessage() { return $this->message; } public function hasError() { return $this->hasError; } public function setErrorMessage($message) { $this->message = $message; $this->hasError = true; } public function clearErrorMessage() { $this->message = ''; $this->hasError = false; } public function getHTML() { $message = htmlspecialchars($this->message); if ($this->hasError()) { return '' . $message . ''; } else { return '' . $message . ''; } } } class Fields { private $fields = array(); public function addField($name, $message = '') { $field = new Field($name, $message); $this->fields[$field->getName()] = $field; } public function getField($name) { return $this->fields[$name]; } public function hasErrors() { foreach ($this->fields as $field) { if ($field->hasError()) { return true; } } return false; } } ?> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// validate.php fields = new Fields(); } public function getFields() { return $this->fields; } // Validate a generic text field public function text($name, $value, $required = true, $min = 1, $max = 255) { // Get Field object $field = $this->fields->getField($name); // If field is not required and empty, remove error and exit if (!$required && empty($value)) { $field->clearErrorMessage(); return; } // Check field and set or clear error message if ($required && empty($value)) { $field->setErrorMessage('Required.'); } else if (strlen($value) < $min) { $field->setErrorMessage('Too short.'); } else if (strlen($value) > $max) { $field->setErrorMessage('Too long.'); } else { $field->clearErrorMessage(); } } // Validate a field with a generic pattern public function pattern($name, $value, $pattern, $message, $required = true) { // Get Field object $field = $this->fields->getField($name); // If field is not required and empty, remove errors and exit if (!$required && empty($value)) { $field->clearErrorMessage(); return; } // Check field and set or clear error message $match = preg_match($pattern, $value); if ($match === false) { $field->setErrorMessage('Error testing field.'); } else if ( $match != 1 ) { $field->setErrorMessage($message); } else { $field->clearErrorMessage(); } } public function phone($name, $value, $required = false) { $field = $this->fields->getField($name); // Call the text method and exit if it yields an error $this->text($name, $value, $required); if ($field->hasError()) { return; } // Call the pattern method to validate a phone number $pattern = '/^[[:digit:]]{3}-[[:digit:]]{3}-[[:digit:]]{4}$/'; $message = 'Invalid phone number.'; $this->pattern($name, $value, $pattern, $message, $required); } public function email($name, $value, $required = true) { $field = $this->fields->getField($name); // If field is not required and empty, remove errors and exit if (!$required && empty($value)) { $field->clearErrorMessage(); return; } // Call the text method and exit if it yields an error $this->text($name, $value, $required); if ($field->hasError()) { return; } // Split email address on @ sign and check parts $parts = explode('@', $value); if (count($parts) < 2) { $field->setErrorMessage('At sign required.'); return; } if (count($parts) > 2) { $field->setErrorMessage('Only one at sign allowed.'); return; } $local = $parts[0]; $domain = $parts[1]; // Check lengths of local and domain parts if (strlen($local) > 64) { $field->setErrorMessage('Username part too long.'); return; } if (strlen($domain) > 255) { $field->setErrorMessage('Domain name part too long.'); return; } // Patterns for address formatted local part $atom = '[[:alnum:]_!#$%&\'*+\/=?^`{|}~-]+'; $dotatom = '(\.' . $atom . ')*'; $address = '(^' . $atom . $dotatom . '$)'; // Patterns for quoted text formatted local part $char = '([^\\\\"])'; $esc = '(\\\\[\\\\"])'; $text = '(' . $char . '|' . $esc . ')+'; $quoted = '(^"' . $text . '"$)'; // Combined pattern for testing local part $localPattern = '/' . $address . '|' . $quoted . '/'; // Call the pattern method and exit if it yields an error $this->pattern($name, $local, $localPattern, 'Invalid username part.'); if ($field->hasError()) { return; } // Patterns for domain part $hostname = '([[:alnum:]]([-[:alnum:]]{0,62}[[:alnum:]])?)'; $hostnames = '(' . $hostname . '(\.' . $hostname . ')*)'; $top = '\.[[:alnum:]]{2,6}'; $domainPattern = '/^' . $hostnames . $top . '$/'; // Call the pattern method $this->pattern($name, $domain, $domainPattern, 'Invalid domain name part.'); } } ?> /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// main.css /* the styles for the html elements */ html { background-color: rgb(192, 192, 192); } body { margin-top: 0; font-family: Arial, Helvetica, sans-serif; width: 760px; margin: 0 auto; background-color: white; border: 1px solid black; padding: .5em 2em; } header { margin: 0; border-bottom: 2px solid black; } header h1 { margin: 0; padding: .5em 0; color: black; } main { margin: 0; } aside { float: left; width: 150px; } nav ul { list-style-type: none; margin-left: 0; padding-left: 0; } nav ul li { padding-bottom: 0.5em; } section { float: left; width: 500px; padding-bottom: 1.5em; } footer { clear: both; margin-top: 1em; border-top: 2px solid black; } footer p { text-align: right; font-size: 80%; margin: 1em 0; } h1 { font-size: 150%; margin: 0; padding: .5em 0 .25em; } h2 { font-size: 120%; margin: 0; padding: .25em 0 .5em; } h1, h2 { color: rgb(208, 133, 4); } ul { margin: 0 0 1em 0; padding: 0 0 0 2.5em; } li { margin: 0; padding: 0; } a { color: rgb(41, 64, 124); font-weight: bold; } a:hover { color: rgb(208, 133, 4); } table { border: 1px solid black; border-collapse: collapse; } td, th { border: 1px dashed black; padding: .2em .5em .2em .5em; vertical-align: top; text-align: left; } form { margin: 0; } br { clear: left; } /* the styles for classes */ .right { text-align: right; } .last_paragraph { margin-bottom: 2em; } .margin_top_increase { margin-top: 1em; } /******************************************************************** * Additional styles for the Product Manager application ********************************************************************/ #add_product_form { margin: .5em 0 1em; } #add_product_form label { width: 6em; padding-right: 1em; padding-bottom: .5em; float: left; } #add_product_form input { float: left; } #add_product_form input[text] { width: 15em; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// index.php prepare($queryCategory); $statement1->bindValue(':category_id', $category_id); $statement1->execute(); $category = $statement1->fetch(); $category_name = $category['categoryName']; $statement1->closeCursor(); // Get all categories $query = 'SELECT * FROM categories ORDER BY categoryID'; $statement = $db->prepare($query); $statement->execute(); $categories = $statement->fetchAll(); $statement->closeCursor(); // Get products for selected category $queryProducts = 'SELECT * FROM products WHERE categoryID = :category_id ORDER BY productID'; $statement3 = $db->prepare($queryProducts); $statement3->bindValue(':category_id', $category_id); $statement3->execute(); $products = $statement3->fetchAll(); $statement3->closeCursor(); ?> Task 4b

Task 4b

Task 4b

Code Name Price  

Add Product

List Categories

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// error.php Task 4b

Task 4b

Error

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// delete_product.php prepare($query); $statement->bindValue(':product_id', $product_id); $success = $statement->execute(); $statement->closeCursor(); } // Display the Product List page include('index.php'); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// delete_category.php prepare($query); $statement->bindValue(':category_id', $category_id); $statement->execute(); $statement->closeCursor(); // Display the Category List page include('category_list.php'); } ?> ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// database_error.php Task 4b

Task 4b

Database Error

There was an error connecting to the database.

The database must be installed as described in the appendix.

MySQL must be running as described in chapter 1.

Error message:

 

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// database.php getMessage(); include('database_error.php'); exit(); } ?> /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// category_list.php prepare($query); $statement->execute(); $categories = $statement->fetchAll(); $statement->closeCursor(); ?> Task 4b

Product Manager

Category List

Name  

Add Category

List Products

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// add_category_form.php prepare($query); $statement->execute(); $categories = $statement->fetchAll(); $statement->closeCursor(); ?> Task 4b

Task 4b

Add Product






View Product List

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// add_product.php prepare($query); $statement->bindValue(':category_id', $category_id); $statement->bindValue(':code', $code); $statement->bindValue(':name', $name); $statement->bindValue(':price', $price); $statement->execute(); $statement->closeCursor(); // Display the Product List page include('index.php'); } ?> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// add_category.php prepare($query); $statement->bindValue(':category_name', $name); $statement->execute(); $statement->closeCursor(); // Display the Category List page include('category_list.php'); } ?> ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// newfolder index.php query($query); $category = $category->fetch(); $category_name = $category['categoryName']; // Get all categories $query = 'SELECT * FROM categories ORDER BY categorylD'; $categories = $db->query($query); // Get products for selected category $query = "SELECT * FROH products WHERE categorylD = $category_id ORDER BY productID"; $products = $db->query($query); ?> Hy Guitar Shop
Product List

Code Name Price
Task4b
Database Error

There was an error connecting to the database.

The database must be installed as described in appendix A.

The database must be running as described in chapter l.

Error message:

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// database.php getMessage(); include('database_error.php'); exit() ; } ?> ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// add_category_form.php query($query); ?> How to use PHP with MySQL chtml xmlns="ht tp://www.w3.org/19 9 9/xhtml"> Task4b clink rel="stylesheet" type="text/css" href="main.css" />
Add Product

cinput type="input" name="code" />


How to use PHP with MySQL
View Product List
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// add_product.php exec($query); // Display the Product List page include('index.php'); } ?> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////Task 4(a)//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////Task 4(b)////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////