cpsc 3220 html, php, mysql exercises chapter 19 murach’s php and mysql

6
CpSc 3220 HTML, PHP, MySQL Exercises Chapter 19 Murach’s PHP and MySQL

Upload: merilyn-logan

Post on 11-Jan-2016

242 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CpSc 3220 HTML, PHP, MySQL Exercises Chapter 19 Murach’s PHP and MySQL

CpSc 3220HTML, PHP, MySQL Exercises

Chapter 19Murach’s PHP and MySQL

Page 2: CpSc 3220 HTML, PHP, MySQL Exercises Chapter 19 Murach’s PHP and MySQL

© 2010, Mike Murach & Associates, Inc.

Murach's PHP and MySQL, C5Slide 2

Exercises from Chapter 19 1. Ask us to study and modify a Web site that works with the

my_guitar_shop1 database

2. The MVC pattern was used to create this Web site

3. It is coded using HTML, PHP, and MySQL

Page 3: CpSc 3220 HTML, PHP, MySQL Exercises Chapter 19 Murach’s PHP and MySQL

© 2010, Mike Murach & Associates, Inc.

Murach's PHP and MySQL, C5 Slide 3

The MVC pattern

`

Browser

Controllerindex.php

Viewproduct_list.phpproduct_add.php

database_error.php

Modeldatabase.php

product_db.phpcategory_db.php

Data store

HTTPrequest

HTTPresponse

Page 4: CpSc 3220 HTML, PHP, MySQL Exercises Chapter 19 Murach’s PHP and MySQL

© 2010, Mike Murach & Associates, Inc.

c:/xampp/htdocs/ex_starts/ch19_ex1

Murach's PHP and MySQL, C19Slide 4

Page 5: CpSc 3220 HTML, PHP, MySQL Exercises Chapter 19 Murach’s PHP and MySQL

index.php<?phprequire('util/main.php');require('model/database.php');require('model/product_db.php');// Select some products$category_id = 2;// Get the products$products = get_products_by_category($category_id);//Delete a product$name = 'Fender Telecaster';// Delete the product and display an appropriate message$delete_message = "No rows were deleted.";// Insert a product$category_id = 1;$code = 'tele';$name = 'Fender Telecaster';$description = 'NA';$price = '949.99';// Insert the data and Display an appropriate message$insert_message = "No rows were inserted.";include 'home.php';?>

Page 6: CpSc 3220 HTML, PHP, MySQL Exercises Chapter 19 Murach’s PHP and MySQL

database.php<?php$dsn = 'mysql:host=localhost:3307;dbname=my_guitar_shop2';$username = 'mgs_user';$password = 'pa55word';$options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);

try {    $db = new PDO($dsn, $username, $password, $options);} catch (PDOException $e) {    $error_message = $e->getMessage();    include 'errors/db_error_connect.php';    exit;}

function display_db_error($error_message) {    global $app_path;    include 'errors/db_error.php';    exit;}?>