YOU ARE DOWNLOADING DOCUMENT

Please tick the box to continue:

Transcript
Page 1: DataBase course notes 07 DataBases php + MySQL 1.

DataBase course notes 07

DataBases

php + MySQL

1

Page 2: DataBase course notes 07 DataBases php + MySQL 1.

• Pizza delivery example

• users, cart, detail, status

• pizza

• user can be power user – update pizza

Page 3: DataBase course notes 07 DataBases php + MySQL 1.

MySQL DataBase diagram

Page 4: DataBase course notes 07 DataBases php + MySQL 1.

four basic functions - CRUD

• Create

• Read / Retrieve

• Update

• Delete

DataBase course notes 07 4

Page 5: DataBase course notes 07 DataBases php + MySQL 1.

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

Page 6: DataBase course notes 07 DataBases php + MySQL 1.

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

Page 7: DataBase course notes 07 DataBases php + MySQL 1.

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

Page 8: DataBase course notes 07 DataBases php + MySQL 1.

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);

}

?>

Page 9: DataBase course notes 07 DataBases php + MySQL 1.

C Retrieve UD

DataBase course notes 07 9

Page 10: DataBase course notes 07 DataBases php + MySQL 1.

Pizzas.php

<html><head><title>Pizza Expres - PIZZAs</title></head><body>

<?php// **************************************************// main page here:include "PizzaExpres_Functions.php";connectDB();

</body></html>

Page 11: DataBase course notes 07 DataBases php + MySQL 1.

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>");

Page 12: DataBase course notes 07 DataBases php + MySQL 1.

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>");

….}

Page 13: DataBase course notes 07 DataBases php + MySQL 1.

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;

}

}

Page 14: DataBase course notes 07 DataBases php + MySQL 1.

Pizzas.php

DataBase course notes 07 14

Page 15: DataBase course notes 07 DataBases php + MySQL 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

Page 16: DataBase course notes 07 DataBases php + MySQL 1.

pizza_detail.php ? Pizza_ID = 1

DataBase course notes 07 16

Page 17: DataBase course notes 07 DataBases php + MySQL 1.

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

Page 18: DataBase course notes 07 DataBases php + MySQL 1.

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

Page 19: DataBase course notes 07 DataBases php + MySQL 1.

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

Page 20: DataBase course notes 07 DataBases php + MySQL 1.

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

Page 21: DataBase course notes 07 DataBases php + MySQL 1.

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">

Page 22: DataBase course notes 07 DataBases php + MySQL 1.

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

Page 23: DataBase course notes 07 DataBases php + MySQL 1.

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>

Page 24: DataBase course notes 07 DataBases php + MySQL 1.

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'];

Page 25: DataBase course notes 07 DataBases php + MySQL 1.

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");

?>

Page 26: DataBase course notes 07 DataBases php + MySQL 1.

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) {

Page 27: DataBase course notes 07 DataBases php + MySQL 1.

Insert - ins.php

// **************************************************// main page here:include "PizzaExpres_Functions.php";connectDB();

if($ins) {

?></body></html>

Page 28: DataBase course notes 07 DataBases php + MySQL 1.

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")));

}

Page 29: DataBase course notes 07 DataBases php + MySQL 1.

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,"","");}

Page 30: DataBase course notes 07 DataBases php + MySQL 1.

Update – up.php

Page 31: DataBase course notes 07 DataBases php + MySQL 1.

Delete – del.php

Page 32: DataBase course notes 07 DataBases php + MySQL 1.

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

Page 33: DataBase course notes 07 DataBases php + MySQL 1.

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

Page 34: DataBase course notes 07 DataBases php + MySQL 1.

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

Page 35: DataBase course notes 07 DataBases php + MySQL 1.

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

Page 36: DataBase course notes 07 DataBases php + MySQL 1.

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

Page 37: DataBase course notes 07 DataBases php + MySQL 1.

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

Page 38: DataBase course notes 07 DataBases php + MySQL 1.

Programming Assignment

• All names should be one single language– preferable english

DataBase course notes 07 38

Page 39: DataBase course notes 07 DataBases php + MySQL 1.

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

Page 40: DataBase course notes 07 DataBases php + MySQL 1.

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

Page 41: DataBase course notes 07 DataBases php + MySQL 1.

BackUp and Restore work

• DevPHP (IDE, editor HTMP, php)

• mowes (apache web server, support php, MySQL database server)

• MySQL Workbench – front-end tool for DB

• 200 Mb – portable

• code – folder in www

• database – folder in data

DataBase course notes 07 41


Related Documents