jon wolfgram lorena krenitsky jeff mattson cs 3600 a music ...zshen/webfiles/notes/... · jon...

19
Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up pg. 1 Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database

Upload: others

Post on 30-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music ...zshen/Webfiles/notes/... · Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

pg. 1

Jon Wolfgram

Lorena Krenitsky

Jeff Mattson

CS 3600

A Music Store Database

Page 2: Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music ...zshen/Webfiles/notes/... · Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

pg. 2

Table of Contents

Introduction Pg. 3

E/R Diagram Pg.3

Constraints Pg. 3

IC Summarization Pg. 5

MySQL tables and Normalization Pg. 5

GUI Pg. 9

MySQL queries – Managers and Cashiers Pg. 10

MySQL queries – Cashier Pg. 15

Summation Pg. 19

Page 3: Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music ...zshen/Webfiles/notes/... · Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

pg. 3

Introduction

Our project that we as a team chose to develop was a database for a Music store. We developed a

database for Employees, Inventory, Customers, Sales, and Returns. This paper is intended to

demonstrate the many aspects of our project that we developed throughout this course.

E/R Diagram

E/R Diagram: The purpose of our E/R diagram was to state our design assumptions and to guide our

design decisions. We felt our design assumptions were reasonable, but we did not depict an IsA to

Customer in which we implemented in further design.

Our database depicts a collection of tables that are in Boyce-Codd normal form to the best of our

abilities.

Constraints

The following are some realistic constraints such as foreign keys, check constraints, and not null

constraints, for the tables and attributes we created.

Any store requires some fundamental rules to sell in the continental US. We needed to implement

the following:

o sale prices cannot be negative, (check constraint)

o employee salaries cannot be negative, (check constraint)

o number of items in stock cannot be negative, (check constraint)

o we cannot sell more of an item than we have in stock, (check constraint)

Page 4: Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music ...zshen/Webfiles/notes/... · Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

pg. 4

o Employee IDs must refer to a real person's ID that is known to the company, (foreign key

constraint)

o An employee's manager must also be an employee of the company, (foreign key constraint)

o Any customer that signs up for a corporate account must have contact information on file

with the company, (foreign key constraint)

o State abbreviations in addresses can only be two letters long and must exist in the list of

official abbreviations given by the USPS, (check constraint)

o Email addresses must contain an @ sign to be valid, (check constraint)

o If a customer account is used in a sale, it must be a valid ID from our store, (foreign key

constraint)

o If an employee ID is used in a sale, it must be a valid ID from our store, (foreign key

constraint)

o If an item ID is mentioned in a sale, it must be a valid ID from our store of an object we

currently have in stock, (foreign key constraint)

o Customers cannot return items that we no longer stock or never kept in stock, (foreign key

constraint)

o In order for an item to be returned, the original sale receipt or ID must be known to prove

the item was purchased from our store, (foreign key constraint)

o If an item was purchased under a customer account, then only that account can receive a

refund, (foreign key constraint)

o A customer cannot return a larger quantity of a particular item than they purchased

originally, (trigger)

o Monetary refunds cannot exceed the original purchase price per item, (trigger)

o When a return is processed the employee ID logged must be a valid ID from our store,

(foreign key constraint)

Our music store also has some specific requirements:

o Employee and customer accounts should never have blank fields for first name, last name,

address, town, or zipcode, (not null constraint)

o Employee salary, employee manager, and company position should never be blank, (not null

constraint)

o Artist or manufacturer name should never be blank, (not null constraint)

o Album title or item description should never be blank, (not null constraint)

o Item price can never be blank, (not null constraint)

o Employee positions must fall under the approved company titles, such as Manager or

Cashier, (check constraint)

Page 5: Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music ...zshen/Webfiles/notes/... · Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

pg. 5

IC Summarization As our database is relational, each table is headed by a primary key that by definition cannot be blank.

We chose to implement each table with a numeric ID as the primary key. IDs of an identical object or

person are duplicated across tables in order to imply relationships between the two records. We chose

to use auto-incremented IDs provided by the database rather than Social Security numbers for PersonID

since using SSNs poses a security risk.

MySQL tables and Normalization

Person table: The first table in our database is the Person table. The attributes for the table are: PersonID, FirstName, LastName, Address, Town, State, ZipCode, PhNum, BDay, and Email. Example values for those attributes are 11222, Peter, Griffin, 10 Elm Avenue, Plymouth, NH, 3264, 603-535-2222, 1965-03-10, [email protected]. The key is PersonID. We know that the table must be in the 1st Normal Form because it is an RDB. It is in the second normal form because functional dependencies do not contain subsets of key in the left side of implications. FDS {PersonID} {FirstName, LastName,Address,Town,State,ZipCode,PhNum,BDay,Email} Corrections: This is in BCNF. Table: +----------+------------+------------+----------------------------+---------------+-------+---------+--------------+------------+----------------+ | PersonID | FirstName | LastName | Address | Town | State | ZipCode | PhNum| BDay | Email | +----------+------------+------------+----------------------------+---------------+-------+---------+--------------+------------+----------------+ | 11222 | Peter | Griffin | 10 Elm Avenue | Plymouth | NH | 3264 | 603-535-2222 | 1965-03-10 | NULL | | 11333 | Brian | Griffin | 10 Elm Avenue | Plymouth | NH | 3264 | 603-535-2222 | 2000-08-21 | NULL | | 11444 | Stewie | Griffin | 10 Elm Avenue | Plymouth | NH | 3264 | 603-535-2222 | 2011-10-03 | NULL | | 22111 | Nick | Stokes | 56 5th Avenue | New York | NY | 10033 | 516-627-1234 | 2072-02-29 | [email protected] | | 22222 | Catherine | Willows | 7843 Maple Street | New York | NY | 10033 | 516-627-5432 | 2071-05-15 | NULL | | 33111 | Eric | Cartman | 4 Apple Road | South Park | CO | 80204 | 303-209-0101 | 2008-07-18 | NULL | | 33222 | Kyle | Broflovski | 63 Spring Street | South Park | CO | 80204 | 303-209-2587 | 2008-09-02 | NULL | | 33333 | Kenny | McCormick | 7 Park Drive | South Park | CO | 80204 | 303-209-3333 | 2008-06-13 | NULL | | 33444 | Stan | Marsh | 8 Apple Road | South Park | CO | 80204 | 303-209-3394 | 2008-03-30 | NULL | | 44111 | Montgomery | Scott | 1 Starfleet Academy Street | San Francisco | CA | 94115 | 415-571-1701 | 2228-10-11 | NUL | | 44222 | Hikaru | Sulu | 1 Starfleet Academy Street | San Francisco | CA | 94115 | 415-571-1701 | 2248-12-01 | NULL | | 44333 | Leonard | McCoy | 1 Starfleet Academy Street | San Francisco | CA | 94115 | 415-571-1701 | 2236-01-19 | NULL | | 55111 | Tyrion | Lannister | 1 Casterly Rock | Westeros | FL | 33110 | 407-147-0000 | 1256-01-07 | NULL | | 55222 | Tywin | Lannister | 1 Casterly Rock | Westeros | FL | 33110 | 407-147-0000 | 1224-06-27 | NULL | | 55333 | Jon | Snow | 1 Winterfell Lane | Westeros | AK | 99501 | 407-212-5555 | 1272-11-06 | NULL | | 55444 | Bran | Stark | 1 Winterfell Lane | Westeros | AK | 99501 | 407-212-5555 | 1265-05-26 | NULL | | 55555 | Arya | Stark | 1 Winterfell Lane | Westeros | AK | 99501 | 407-212-5555 | 1267-07-08 | NULL | | 66111 | Albert | Einstein | 17 High Street | Plymouth | NH | 3264 | 603-535-7890 | 1879-03-14 | NULL | | 66222 | Nikola | Tesla | 17 High Street | Plymouth | NH | 3264 | 603-535-1278 | 1856-07-10 | NULL | | 77111 | Jane | Doe | 123 Main Street | AnyTown | NH | 3333 | 603-535-1212 | 2000-04-05 | [email protected] | +----------+------------+------------+----------------------------+---------------+-------+---------+--------------+------------+----------------+ Employee table: The attributes for the table are EmpID , Salary, EmpMgrID, Position, The key is EmpID.

Page 6: Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music ...zshen/Webfiles/notes/... · Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

pg. 6

Normalization: The table is in the 1st Normal Form as it is a RDB. It is also in the second normal form as the functional dependencies do not contain subsets of key in the left side of the implications. FDS {EmpID} {Salary, EmpMgrID, Position} Corrections: This is BCNF because EmplID is a superkey. Tables: mysql> select * from Employee;

+-------+----------+----------+------------+

| EmpID | Salary | EmpMgrID | Position |

+-------+----------+----------+------------+

| 33444 | 19000.00 | 44222 | Cashier |

| 44222 | 25000.00 | 55222 | Manager |

| 44333 | 21000.00 | 44222 | Cashier |

| 55111 | 21000.00 | 44222 | Cashier |

| 55222 | 31000.00 | 55222 | Manager |

| 55333 | 19000.00 | 44222 | Cashier |

| 55444 | 17000.00 | 66222 | Data Entry |

| 55555 | 19000.00 | 44222 | Cashier |

| 66111 | 20000.00 | 66222 | Data Entry |

| 66222 | 27000.00 | 55222 | Manager |

Inventory: It contains: ItemID, ArtManu, ItemName, NumStock, CurPrice, ForCat, Genre, The key is ItemID. Normalization: The table is in the 1st Normal Form and the 2nd Normal Form because the left sides of the FDs do not contain subsets of the key. FDS {ItemID} {ArtManu, ItemName, NumStock, CurPrice, ForCat, Genre} Corrections: It is in BCNF because ItemID is a superkey. Tables: mysql> select * from Inventory;

+--------+-------------------+---------------------------+----------+----------+--------+-------+

| ItemID | ArtManu | ItemName | NumStock | CurPrice | ForCat | Genre |

+--------+-------------------+---------------------------+----------+----------+--------+-------+

| 11110 | Maroon 5 | Songs About Jane | 8000 | 13.00 | CD | POP |

Page 7: Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music ...zshen/Webfiles/notes/... · Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

pg. 7

| 11111 | Michael Jackson | Thriller | 1000 | 15.00 | DVD | POP |

| 22222 | Bruce Springsteen | Born in the U.S.A | 1000 | 15.00 | CD | POP |

| 33333 | Elvis Presley | Elvis Presley Rock n Roll | 1000 | 15.00 | CD | POP |

| 44444 | Madonna | True Blue | 1000 | 15.00 | CD | POP |

| 55555 | Maroon 5 | Day Light | 5000 | 15.00 | CD | POP |

| 66666 | Maroon 5 | Overexposed | 4000 | 15.00 | CD | POP |

| 77777 | Maroon 5 | One More Night | 4000 | 15.00 | CD | POP |

| 88888 | Maroon 5 | Moves Like Jaggar | 10000 | 15.00 | CD | POP |

| 88889 | Maroon 5 | Moves Like Jaggar | 1000 | 10.00 | MP3 | POP |

| 99999 | Maroon 5 | Hands All Over | 9000 | 15.00 | CD | POP |

Customer Table: The 3rd table in our database is the customer table. It contains CustID, DiscCode. The key is CustID. Normalization: The table is in the 1st and 2nd Normal Forms because it is an RDB and the left sides of the FDS do not contain subsets of the key. FDS {CustID} {CustName, Phone} Corrections: The table is in BCNF because CustId is a superkey. No changes necessary. Tables: mysql> select * from Customer;

+--------+----------+

| CustID | DiscCode |

+--------+----------+

| 11222 | 111 |

| 11333 | 111 |

| 11444 | 111 |

| 22111 | 505 |

| 22222 | 376 |

| 33111 | 505 |

| 33222 | 111 |

| 33333 | 111 |

| 44111 | 111 |

| 55222 | 999 |

| 77111 | 376 |

+--------+----------+

Sale Table: The 4th table in our database is the Sales table which contains SaleID, EmployID, ItemId ,

Quantity, CustID, Date, and SalePrice. The key is SaleID.

Page 8: Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music ...zshen/Webfiles/notes/... · Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

pg. 8

Normalization: The table is in the 1st and 2nd Normal Forms because it is an RDB and the left side of the FD implication is not a subset of the key. FDS {SaleID } {EmployID, ItemID, Quantity, CustID, Date, SalePrice}. Corrections: The table is in BCNF because SaleID is a superkey. No changes necessary. Tables:

+--------+----------+--------+----------+--------+------------+-----------+ | SaleID | EmployID | ItemID | Quantity | CustID | Date | SalePrice |

+--------+----------+--------+----------+--------+------------+-----------+

| 1 | 55555 | 11111 | 1 | 11222 | 2012-11-15 | 15.00 |

| 2 | 55111 | 33333 | 3 | 11333 | 2012-11-14 | 15.00 |

| 3 | 11333 | 77777 | 1 | 11444 | 2012-11-13 | 15.00 |

| 4 | 11333 | 66666 | 1 | 22111 | 2012-11-13 | 15.00 |

| 5 | 11222 | 55555 | 1 | 22222 | 2012-11-13 | 15.00 |

| 6 | 11222 | 88888 | 1 | 33111 | 2012-11-13 | 15.00 |

| 7 | 11222 | 99999 | 2 | 33222 | 2012-11-13 | 13.00 |

| 8 | 11333 | 11111 | 2 | 33333 | 2012-11-13 | 15.00 |

| 9 | 11333 | 11111 | 5 | 44111 | 2012-11-13 | 15.00 |

| 10 | 11333 | 11111 | 1 | 77111 | 2012-11-13 | 15.00 |

+--------+----------+--------+----------+--------+------------+-----------+

Returns Table: The 5th table in our database is the returns table. It contains ReturnID, OrgSaleID,

EmployID, ItemID, QuanRetur, AmtRefund, CustID, Date. The key ReturnID.

Normalization: The table is in 1st and 2nd Normal Forms. FDS {ReturnID} {OrgSaleID, EmployID, ItemID, QuanRetur, AmtRefund, CustID, Date} Corrections: The table is in BCNF because ReturnID is a superkey. No changes necessary. Tables: mysql> select * from Returns;

+----------+-----------+----------+--------+-----------+-----------+--------+------------+

| ReturnID | OrgSaleID | EmployID | ItemID | QuanRetur | AmtRefund | CustID | Date |

+----------+-----------+----------+--------+-----------+-----------+--------+------------+

| 1 | 1 | 11222 | 11111 | 1 | 15.00 | 11222 | 2012-11-15 |

| 2 | 2 | 11333 | 33333 | 1 | 15.00 | 11333 | 2012-11-15 |

| 3 | 3 | 11333 | 77777 | 1 | 15.00 | 11444 | 2012-11-15 |

| 4 | 4 | 11333 | 66666 | 1 | 15.00 | 22111 | 2012-11-15 |

| 5 | 5 | 11222 | 55555 | 1 | 15.00 | 22222 | 2012-11-15 |

| 6 | 6 | 11222 | 88888 | 1 | 15.00 | 33111 | 2012-11-15 |

Page 9: Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music ...zshen/Webfiles/notes/... · Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

pg. 9

| 7 | 7 | 11222 | 99999 | 1 | 13.00 | 33222 | 2012-11-15 |

| 8 | 8 | 11333 | 11111 | 1 | 15.00 | 33333 | 2012-11-15 |

| 9 | 9 | 11333 | 11111 | 1 | 15.00 | 44111 | 2012-11-15 |

| 10 | 10 | 11333 | 11111 | 1 | 15.00 | 77111 | 2012-11-15 |

+----------+-----------+----------+--------+-----------+-----------+--------+------------+

GUI Using the populated MySql information we designed an interface in HTML, with embedded Php script(s).

Please go to: http://turing.plymouth.edu/~jsmattson/PhPFiles/project/index.php

This GUI demonstrates many functions that would be necessary for a typical music store to carry on day-

to-day roles and tasks. In order to divide some of the security functions necessary for a particular

employee we have created two categories of employees.

You may view the Manger using Boss Psw: Hog

You may view the Cashier using Daisy Psw: Duke

The Manager submenu includes functions as add/delete

Employees as you can see in this screen shot:

Page 10: Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music ...zshen/Webfiles/notes/... · Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

pg. 10

add/drop inventory as necessary as shown in the below screen shot:

In both Manager and Cashier we have various reports that either would need to work with on day-to-

day operations. Some examples are the employee list, print monthly Top Five lists, print out the Most

Returned Inventory items and others. Within the Manager submenu, the Most Returned Inventory

items are a dynamic list that would portray the Top Five Returns.

MySQL queries For the Manager Submenu

This query allows the Manager to see a complete list of Inventory. The query uses ajax. When the link

for Inventory Admin is clicked it runs this java script:

function showHint(str){ var xmlhttp; if (str.length==0){ document.getElementById("content2").innerHTML="";

Page 11: Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music ...zshen/Webfiles/notes/... · Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

pg. 11

return; } if (window.XMLHttpRequest){ // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else{ // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("content2").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","ajaxSearch.php?q="+str,true); xmlhttp.send(); }

//When this script is run a number is passed for q it could be any of the below number for $query

if ($query == "1"){

echo "<p style=\"text-align:center;\">Current Inventory <form action=\"database_query.php\"

method=\"post\"><table style=\"float:right;\"><tr><td><input type=\"text\" name=\"drop\" size=\"4\"

maxlength=\"5\"></td><td><input type=\"submit\" name=\"sub_drop\" value=\"Delete

ItemID\"></td></tr></table></form><table style=\"float:left;\"><tr><td><form

action=\"addNewItem.php\" method=\"post\"><input type=\"submit\" name=\"add\" value=\"Add

New Item\"></form></td><td><form action=\"addInventory.php\" method=\"post\"><input

type=\"submit\" name=\"stock\" value=\"Add Stock\"></form></td></tr></table><br />";

display_db_query("SELECT ItemID, ArtManu, ItemName, NumStock, CurPrice, ForCat, Genre

FROM Inventory WHERE ItemID NOT IN (select I.ItemID from Inventory I where I.Retired='RETIRED');",

$global_dbh, TRUE, 'border="1" style="margin:0 auto;"');

}

This query allows the Manager to see a list of everything pertaining to current employees who have not

been retired (removed from the table). This also uses ajax and the above java script.

if ($query == "2"){

echo "<p style=\"text-align:center;\">Current Employees<form action=\"database_query.php\"

method=\"post\"><table style=\"float:right;\"><tr><td><input type=\"text\" name=\"delEmp\"

size=\"4\" maxlength=\"5\" value=\"EmpID\"></td><td><input type=\"submit\" name=\"sub_drop\"

Page 12: Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music ...zshen/Webfiles/notes/... · Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

pg. 12

value=\"Delete Employee\"></td></tr></table></form><table style=\"float:left;\"><tr><td><form

action=\"addEmployee.php\" method=\"post\"><input type=\"submit\" name=\"add\" value=\"Add

Employee\"></form></td><td><form action=\"chgSalary.php\" method=\"post\"><input

type=\"submit\" name=\"chgSalary\" value=\"Change Salary\"></form></td></tr></table><br />";

display_db_query("select E.EmpID, P.FirstName, P.LastName, E.Salary, E.EmpMgrID, Position

from Employee E, Person P where E.EmpID=P.PersonID AND E.EmpID NOT IN (SELECT D.EmpID FROM

Employee D WHERE D.Retired='RETIRED');", $global_dbh, TRUE, 'border="1" style="margin:0 auto;"');

}

This query allows the Manager to see a list pertaining to the sales. This also uses the same java script

and php process file called admin_ajax.php that is depicted above.

if ($query == "3"){

echo "<h2 style=\"text-align:center;\">Current Sales</h2><br />";

display_db_query("select * from Sale;", $global_dbh, TRUE, 'border="1" style="margin:0

auto;"');

}

This query allows the manager to see a list pertaining to Returns. This script uses the same as above,

when return totals is clicked it passes a “4” to the admin_ajax.php script which runs this function.

if ($query == "4"){

echo "<h2 style=\"text-align:center;\">Returns</h2><br />";

display_db_query("select * from Returns;", $global_dbh, TRUE, 'border="1" style="margin:0

auto;"');

}

This query allows the Manager to see a list of employee names. This is another similar format as above.

For this function to work it simply passes “5”.

if ($query == "5"){

echo "<h2 style=\"text-align:center;\">Employee Names</h2><br />";

display_db_query("select E.EmpID, P.FirstName, P.LastName from Person P, Employee E where

E.EmpID = P.PersonID;", $global_dbh, TRUE, 'border="1" style="margin:0 auto;"');

}

Page 13: Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music ...zshen/Webfiles/notes/... · Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

pg. 13

This query is used for the Manager to see a list of the top five returns. This uses a display function that

is used to display a dynamic list of the “Top Five Returns”. The function is specifically called

display_top_returns.

function display_top_returns($query_string,$connection,$num_top_sales){

$column_count=mysql_num_fields($result_id)

or die("display_db_query:" . mysql_error());

$num_rows = mysql_num_rows($result_id);

print("<table border=\"1\" style=\"margin:0 auto; margin-top:0px;\">\n");

echo

"<th>ReturnID</th><th>OrgSaleID</th><th>EmployID</th><th>ItemID</th><th>QuanRetur</th><th>a

mtRefund</th><th>CustID</th><th>Date</th>"

for($x=0; $x < $num_top_sales; $x++){

$row=mysql_fetch_row($result_id);

print("<Tr Align=left Valign=top>");

for ($column_num=0; $column_num< $column_count; $column_num++){

print("<Td style=\"width:100px; text-align:center;\">$row[$column_num]</Td>\n");

}

print("</Tr>\n");

}

print("</Table>\n");

}

//than ends the display_top_returns function then it is used here… as it is passed a “98” and 5 means

only five show.

if ($query == "98"){

echo "<p style=\"text-align:center; margin-bottom:0px;\">Top 5 Returns</p><br />";

display_top_returns("select * from Returns order by QuanRetur DESC;", $global_dbh, 5);

}

This query is used to add new employee to employee table and person table.

This query simply uses an html form which passes the data through the POST method to

proAddEmployee.php which processes the data as described below.

//data posted from addEmployee.php

$EmpID = $_POST['EmpID'];

$FirstName = $_POST['FirstName'];

$LastName = $_POST['LastName'];

$Salary = $_POST['Salary'];

$EmpMgrID = $_POST['EmplMgrID'];

$Position = $_POST['Position'];

$Address = $_POST['Address'];

Page 14: Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music ...zshen/Webfiles/notes/... · Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

pg. 14

$Town = $_POST['Town'];

$State = $_POST['State'];

$ZipCode = $_POST['ZipCode'];

$BDay = $_POST['BDay'];

$PhNum = $_POST['PhNum'];

$Email = $_POST['Email'];

This query is used to change date to a format accepted by MySQL.

$timestamp = strtotime($BDay);

$BDay = date("Y-m-d H:i:s", $timestamp);

//database connect

include '../inc/dbc.inc';

//insert posted data into database

mysql_query("INSERT INTO Person (PersonID, FirstName, LastName, Address, Town, State, ZipCode,

PhNum, BDay, Email) VALUES ($EmpID,'$FirstName','$LastName', '$Address', '$Town', '$State',

'$ZipCode', '$PhNum', '$BDay', '$Email');") or die(mysql_error());

mysql_query("INSERT INTO Employee (EmpID, Salary, EmpMgrID,Position) VALUES

($EmpID,$Salary,$EmpMgrID,'$Position');") or die(mysql_error());

//return to password.php page passing an emp=1 which should bring up the ajax employee query..

header('location: password.php?emp=1');//update stock for a specific inventory ItemID

This query is used to insert new items into inventory. This function uses the POST method to pass data

from the form and puts it into the database.

if (isset($_POST['NewItem']) == TRUE){

//posted data from addNewItem.php

$ItemID = $_POST['ItemID'];

$ArtManu = $_POST['ArtManu'];

$ItemName = $_POST['ItemName'];

$NumStock = $_POST['NumStock'];

$CurPrice = $_POST['CurPrice'];

$ForCat = $_POST['ForCat'];

$Genre = $_POST['Genre'];

Page 15: Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music ...zshen/Webfiles/notes/... · Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

pg. 15

//insert new item query

mysql_query("INSERT INTO Inventory (ItemID,ArtManu,ItemName,NumStock,CurPrice, ForCat,

Genre) VALUES ($ItemID, '$ArtManu','$ItemName',$NumStock, $CurPrice, '$ForCat', '$Genre')") or

die(mysql_error());

//this sends the page back to password.php passing a GET inv=1 which should call ajax to display

Employee list.

echo "<html><head></head><body><script>window.location =

'password.php?inv=1'</script></body></html>";

}

MySQL queries for CUSTOMERS

This query is used for the main Page to display the “Top Five sellers”. This calls the function

display_top_sales() function to produce the dynamic list.

function display_top_sales($query_string,$connection,$num_top_sales){

$result_id=mysql_query($query_string,$connection)

or die("display_db_query:" . mysql_error());

$column_count=mysql_num_fields($result_id)

or die("display_db_query:" . mysql_error());

$num_rows = mysql_num_rows($result_id);

print("<table border=\"0\" style=\"margin:0 auto; margin-top:0px;\">\n");

for($x=0; $x < $num_top_sales; $x++){

$row=mysql_fetch_row($result_id);

print("<Tr Align=left Valign=top>");

for ($column_num=0; $column_num< $column_count; $column_num++){

//if its column 3 add 100px to cell

if ($column_num == 2){

print("<Td style=\"width:100px; text-align:center;\">$row[$column_num]</Td>\n");

}

else if ($column_num != 4){

print("<Td style=\"padding-left:10px; padding-right:10px;\">$row[$column_num]</Td>\n");

}

if ($column_num == 4){

print("<Td style=\"width:100px; text-align:center;\"><form action=\"admin/admin_ajax.php\"

method=\"post\"><input type=\"submit\" name=\"buy\" value=\"BUY\"><input type=\"hidden\"

name=\"buy\" value=\"$row[$column_num]\"></form></Td>\n");

}

}

print("</Tr>\n");

Page 16: Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music ...zshen/Webfiles/notes/... · Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

pg. 16

}

print("</Table>\n");

}

This is the second half of the query.

if ($query == "99"){

echo "<p style=\"text-align:center; margin-bottom:0px;\">Top 5 Sellers</p><br />";

display_top_sales("select I.ArtManu, I.ItemName, I.ForCat, CurPrice, I.ItemID from Inventory I,

BestSellers B where I.ItemID=B.ItemID;", $global_dbh, 5);

}

This query is used when the customer presses the “buy” button. This uses php to interpret if the sale

can be allowed only if the number in stock is greater than zero.

if (isset($_POST['buy']) == TRUE){

$num = $_POST['buy'];

$result = mysql_query("SELECT ArtManu, ItemName, ForCat, Genre, CurPrice FROM Inventory

WHERE ItemID=$num;");

$zeroInv = mysql_query("SELECT NumStock, CurPrice FROM Inventory WHERE ItemID=$num");

//constraint for selling when the inventory is empty

$row=mysql_fetch_row($zeroInv);

if ($row[0] > 0){

$date = date("Y-m-d");

$price = $row[1];

mysql_query("UPDATE Inventory SET NumStock=NumStock-1 WHERE ItemID=$num;");

//putting the information into the sale table

mysql_query("INSERT INTO Sale (EmployID,ItemID,Quantity,CustID, Date, SalePrice)

VALUES ('11111',$num, 1,11222, '$date', $price)");

$notEmpty = 1;

}

//For Genre Ajax Search Question would be “What do you have in this genre…”

mysql_query("select ArtManu from Inventory;");

mysql_query("select ItemName from Inventory;");

mysql_query("select CurPrice from Inventory;");

mysql_query("select ItemID from Inventory;");

Page 17: Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music ...zshen/Webfiles/notes/... · Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

pg. 17

mysql_query("select Genre from Inventory;");

mysql_query("select ForCat from Inventory;");

//here is the php that goes with this…

//Put it into $storeArray.

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

$storeArray[] = $row['ArtManu'];

}

//Put it into $itemName.

while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {

$itemName[] = $row['ItemName'];

}

//Put it into $itemPrice.

while ($row = mysql_fetch_array($result3, MYSQL_ASSOC)) {

$itemPrice[] = $row['CurPrice'];

}

//Put it into itemID.

while ($row = mysql_fetch_array($result4, MYSQL_ASSOC)) {

$itemID[] = $row['ItemID'];

}

//Put it into genre.

while ($row = mysql_fetch_array($result5, MYSQL_ASSOC)) {

$genre[] = $row['Genre'];

}

//Put it into forMat.

while ($row = mysql_fetch_array($result6, MYSQL_ASSOC)) {

$forMat[] = $row['ForCat'];

}

//get from search box

$q=$_GET["q"];

//lookup all hints from array if length of q>0

Page 18: Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music ...zshen/Webfiles/notes/... · Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

pg. 18

if (strlen($q) > 0) {

$hint="";

for($i=0; $i<count($genre); $i++){

if (strtolower($q)==strtolower(substr($genre[$i],0,strlen($q)))){

if ($hint==""){

$hint="<tr><td>" . $storeArray[$i] . "</td><td>" . $itemName[$i] .

"</td><td>" . $itemPrice[$i] . "</td><td style=\"width:100px; text-align:center;\">" . $genre[$i] .

"</td><td style=\"width:100px; text-align:center;\">" . $forMat[$i] . "</td><td><form

action=\"admin/admin_ajax.php\" method=\"post\"><input type=\"submit\" name=\"buy\"

value=\"BUY\"><input type=\"hidden\" name=\"buy\" value=\"" . $itemID[$i] . "\"></form></td></tr>"

}

else{

$hint=$hint."<tr><td>" . $storeArray[$i] . "</td><td>" . $itemName[$i] .

"</td><td>" . $itemPrice[$i] . "</td><td style=\"width:100px; text-align:center;\">" . $genre[$i] .

"</td><td style=\"width:100px; text-align:center;\">" . $forMat[$i] . "</td><td><form

action=\"admin/admin_ajax.php\" method=\"post\"><input type=\"submit\" name=\"buy\"

value=\"BUY\"><input type=\"hidden\" name=\"buy\" value=\"" . $itemID[$i] . "\"></form></td></tr>";

}

}

}

}

// Set output to "no suggestion" if no hint were found

// or to the correct values

if ($hint == "") {

echo "Sorry. Nothing like that here.";

}

else{

$response=$hint;

echo "<table

style=\"width:100%;\"><th>Artist</th><th>Album</th><th>Price</th><th>Genre</th><th>Format</th>

" . $response . "</table>";

}

Page 19: Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music ...zshen/Webfiles/notes/... · Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

Jon Wolfgram Lorena Krenitsky Jeff Mattson CS 3600 A Music Store Database Final Write-Up

pg. 19

Summation

This project had many complexities. Our group benefited because our group member

had familiarity with Ajax which was a huge asset in creating a GUI that was customer friendly. It

also allowed a user to navigate the many pages to obtain information or to update the

database as needed. There are many other queries that had been designed into MySQL, but

the queries listed within this document depict the more complex aspects.

Our power point that we used to highlight the GUI further demonstrates the ease of

interaction and skill set that was needed to produce a finished product that met the criteria as

specified for this project. This power point is used to guide our group through the presentation

portion of our project. The power point will be attached along with this document for

inspection as well.