• Pizza delivery example
• users, cart, detail, status
• pizza
• user can be power user – update pizza
Images, database or filesystem
• options when you want to store images
• store data in your database– blob
• store data on filesystem– extra information about file
DataBase course notes 07 5
Advantage
• Filesystem over Database
• file operations perform faster since these are low-level OS functions
• easier to replicate files onto other servers
• database will not be bloated by extremely large files
• large files may slow down other connections
• Database over Filesystem
• all file data is stored in single location
• back up your images in single location
• filesystem you may lose file that have database record pointing to it
• opposite may also happen (have file without corresponding record)
DataBase course notes 07 6
Two examples, Pizza 1 & 1
• Pizza1• CRUD only for pizza
table in this example• storing images in
filesystem– in folder images
• same files in Pizza 1 & 2
• Pizza2• CRUD only for pizza
table in this example• storing images in
database
• same files in Pizza 1 & 2
DataBase course notes 07 7
PizzaExpres_Functions.php
<?php
function connectDB() {
$dbConn = mysql_connect("localhost","root")
or die("Unable to connect to MySQL database");
$selectDB = mysql_select_db("PizzaExpres",$dbConn);
}
?>
Pizzas.php
<html><head><title>Pizza Expres - PIZZAs</title></head><body>
<?php// **************************************************// main page here:include "PizzaExpres_Functions.php";connectDB();
</body></html>
Pizzas.php
$sqlQ = "SELECT Pizza_ID, PizzaName FROM Pizza ORDER BY PRICE ASC";
$result = mysql_query($sqlQ);print("<table border=\"2\">");$numRows = mysql_num_rows($result);$colNo = 2;$crtColNo = 1;for ($i = 1; $i <= $numRows; $i++) {….}print("</table>");
Pizzas.php
for ($i = 1; $i <= $numRows; $i++) {$crtRow = mysql_fetch_array($result);$vPizza_ID = $crtRow["Pizza_ID"];$vPizzaName = $crtRow["PizzaName"];if ($crtColNo == 1)
print("<tr>");print("<td>");
….}
Pizzas.php
for ($i = 1; $i <= $numRows; $i++) {
$crtRow = mysql_fetch_array($result);
if ($crtColNo == 1) print("<tr>"); print("<td>");
print("<a href = \"pizza_detail.php?Pizza_ID=$vPizza_ID\">$vPizzaName</a></td>");
if ($crtColNo == $colNo) {
print("</tr>"); $crtColNo = 1;
}
else {
$crtColNo = $crtColNo + 1;
}
}
Pizzas.php
• <html><head>• <title>Pizza Expres - PIZZAs</title></head><body>• <table border="2"><tr>
– <td><a href = "pizza_detail.php?Pizza_ID=1">
– PIZZA MARGHERITA Ø32cm</a></td>
– <td><a href = "pizza_detail.php?Pizza_ID=5">
– New PIZZA PROSCIUTTO FUNØ32cm</a></td></tr><tr>
– <td><a href = "pizza_detail.php?Pizza_ID=2">
– PIZZA PROSCIUTTO E FUNGHI Ø32cm</a></td>
– <td><a href = "pizza_detail.php?Pizza_ID=3">
– PIZZA HAWAI Ø32cm</a></td></tr><tr>
– …
• </table></body></html>
DataBase course notes 07 15
pizza_detail.php ? Pizza_ID = 1
• <html><head><title>Pizza Expres - PIZZA Details</title></head><body>
• <table border="2">• <table border="1">• <tr> <td>PIZZA MARGHERITA Ø32cm</td><td>13</td></tr>• <tr> <td colspan="2"><img
src="./Images/margherita.jpg"/></td></tr>• <tr> <td colspan="2">Sos de Rosii, Mozzarella, Busuioc - are un
blat subtire cu un excelent sos din pulpa de rosii preparat special dupa o reteta originala</td></tr>
• </table></td></table></body></html>
• Pizza1 – image stored in filesystem
DataBase course notes 07 17
pizza_detail.php
include "PizzaExpres_Functions.php";
connectDB();
$vPizza_ID = $_GET['Pizza_ID'];
$sqlQ = sprintf("SELECT Pizza_ID, PizzaName, Detail, Price, Image FROM Pizza WHERE Pizza_ID = '%d' ;",
$vPizza_ID);
$result = mysql_query($sqlQ);DataBase course notes 07 18
pizza_detail.php
print("\n<table border=\"2\">");
$crtRow = mysql_fetch_array($result);
$vPizzaName = $crtRow["PizzaName"];
$vDetail = $crtRow["Detail"];
$vPrice = $crtRow["Price"];
$vImage = $crtRow["Image"];
Pizza1 – image stored in filesystem in database stored character string with file name
DataBase course notes 07 19
pizza_detail.php
print("<table border=\"1\">"); print("<tr> <td>$vPizzaName</td><td>$vPrice</td></tr>");
$vImage2 = "./Images/" . $vImage;
print "<tr> <td colspan=\"2\"><img src=\"$vImage2\"/></td></tr>";
print("\n<tr> <td colspan=\"2\">$vDetail</td></tr>");
print("</table></td>");
print("</table>");
DataBase course notes 07 20
login.php
<html><head><title>Pizza Expres LogIn</title></head>
<body><form action="login_action.php" method="post"><table border="0"><tr><td>Login name: <td><input type="text" name="logName">
Create R Update Delete
• Not particularly happy with the solution
• but in a hurry I just want to show examples of CRUD operations
• paint a menu to choose– Create – Insert– Update – Update– Delete – Delete
• only Power Users have access to CRUD operations on pizza table
DataBase course notes 07 22
login.php<tr><td>Password: <td><input type="password" name="logPass">
<tr><td colspan="2" align="center"><input type="submit" name="oInsert" value="Insert"><input type="submit" name="oUpdate" value="Update"><input type="submit" name="oDelete" value="Delete">
</table></form>
</body></html>
login_action.php<?phpinclude "PizzaExpres_Functions.php";connectDB();
$logName = $_POST["logName"]; $logPass = $_POST["logPass"];
$sqlQ = sprintf("SELECT COUNT(*) AS Nr FROM tUser WHERE UserName = '%s' AND UserPass = '%s' AND Power = 1;",
$logName, $logPass);$result = mysql_query($sqlQ);$row = mysql_fetch_assoc($result); $Nr = $row['Nr'];
login_action.php
if ($Nr <> 1)print("Error: Non Power User!");
if ($oInsert && ($Nr == 1))header("Location: http://localhost/PizzaExpres/ins.php");
if ($oUpdate && ($Nr == 1))header("Location: http://localhost/PizzaExpres/up.php");
if ($oDelete && ($Nr == 1))header("Location: http://localhost/PizzaExpres/del.php");
?>
Insert - ins.php<html><head><title>Pizza Expres - INSERT</title></head>
<body><?php
function paintIns($pPizzaName, $pDetail, $pPrice, $pImage, $pError) {
function validIns($pPizzaName, $pDetail, $pPrice, $pImage) {
function insertIns($pPizzaName, $pDetail, $pPrice, $pImage) {
Insert - ins.php
// **************************************************// main page here:include "PizzaExpres_Functions.php";connectDB();
if($ins) {
?></body></html>
Insert - ins.phpif($ins) {
$vPizzaName = $_POST["PizzaName"];$vDetail = $_POST["Detail"];$vPrice = (real)$_POST["Price"]; move_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'],"c:/tmp/latest.img");$instr = fopen("c:/tmp/latest.img","rb");$vImage = addslashes(fread($instr,filesize("c:/tmp/latest.img")));
}
if($ins) {$vError = validIns($vPizzaName, $vDetail, $vPrice, $vImage);if ($vError == "") {
$insError = insertIns($vPizzaName, $vDetail, $vPrice, $vImage);if ($insError == 0) {
print("Pizza was Inserted in database!<br>");print("<a href=\"ins.php\">INSert</a>");
} else { print("Unexpected Error when Trying to Insert Pizza in database!");
}} else { paintIns($vPizzaName, $vDetail, $vPrice, $vImage, $vError);}
} else { paintIns("","",0,"","");}
Programming Assignment
• DataBase Application (any database appl. will work)
• expect your work at E-Mail address
• Calin.Cenan @ cs.utcluj.ro
• please state your name and group in your mail – especially if your mail address instead of Name@somewhere is something like Shmecherul …
DataBase course notes 07 32
Programming Assignment
• I will expect for programming assignment written in php with MySQL database
• tell me if you expect from me something different that:
• for example check URL– I must have access to database
DataBase course notes 07 33
Programming Assignment
• look for something.sql – Create Database something– Execute SQL file (create database, populate
database)
• look for some folder– Move folder in www, look to execute
something index.php, ….
DataBase course notes 07 34
Programming Assignment
• I will asses mainly the database• expect around ten tables in the database• there is no database whiteout data
– expect around ten rows of in each table– expect to see some views in the database
• data should be readable or better credible, for example book author could be
• Qwerty, or abc – un readable, unacceptable• Nicolae Guta – readable but not credible
DataBase course notes 07 35
Programming Assignment
• I will asses mainly the functionality– Fancy web design, as Ian Fleming’s James
Bond says is “For Your Eyes Only”
• Expect mainly presentation – view– presentations pages with content obtained
from database, search, filter, categories, …
• Expect at least 1 table – at most 2-3 table with update operations– register user, CRUD for main products, …
DataBase course notes 07 36
Programming Assignment
• Cannot trick me on number of tables
• Computers have motherboards, memories, video boards– Each have their own characteristics
• Computer table is related to Motherboards table, Memories table, Video Boards table
• Count 1 table not 4 tables
DataBase course notes 07 37
Programming Assignment
• All names should be one single language– preferable english
DataBase course notes 07 38
Programming Assignment -example of acceptable domains• E-Commerce – amazon, tennis-
warehouse, eMag, GSM, ….
• Movies, music
• Real-estate
• Car Rental
• Air ticket reservation
DataBase course notes 07 39
Programming Assignment -debatable? domains
• MMORPG
• don’t have expertise (in gamming other then Tetris and Pac-man) to asses if the database and application is correct
• have to accept everything students say– database application it is exactly what is
needed – only these 2-3 table model the domain
DataBase course notes 07 40