-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.php
More file actions
121 lines (111 loc) · 3.25 KB
/
create.php
File metadata and controls
121 lines (111 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
require_once 'lib/database.php';
function setupProductConnection(){
try{
$dbh = new Database();
return $dbh;
}
catch(PDOException $e){
echo '<pre class = "bg-danger">';
echo 'Connection failed: ' . $e->getMessage();
echo '</pre>';
return FALSE;
}
}
function dropTableByName($tname){
global $dbh;
$sql = "DROP TABLE IF EXISTS $tname";
$status = $dbh->exec($sql);
if($status === FALSE){
echo '<pre class ="bg-danger">';
print_r($dbh->errorInfo());
echo '</pre>';
}
}
function createTableIngredient(){
$sql = "CREATE TABLE ingredient (
id INTEGER PRIMARY KEY ASC,
i_name varchar(50),
unit varchar(10),
price decimal(10,2),
description varchar(50),
longdescription varchar(500),
time varchar(50),
imgURL varchar(255))";
createTableGeneric($sql);
}
function createTableComment(){
$sql = "CREATE TABLE comment (
id INTEGER PRIMARY KEY ASC,
c_name varchar(50),
rating int(2),
words varchar(500),
ingredient_name varchar(255))";
createTableGeneric($sql);
}
function createTableImage(){
$sql = "CREATE TABLE images(
img_id INTEGER PRIMARY KEY ASC,
name varchar(255),
type varchar(255),
size int(10),
ext varchar(5)
)";
createTableGeneric($sql);
}
function createTableGeneric($sql){
global $dbh;
$status = $dbh->exec($sql);
if($status === false){
echo '<pre class="bg-danger">';
print_r ( $dbh->errorInfo () );
echo '</pre>';
}
}
function loadProductsIntoEmptyDatabase(){
global $dbh;
require_once "data/list.php";
$ingredients = getIngredientsFromFile();
$comments = getCommentsFromFile();
$sql_ingredient = "INSERT INTO ingredient(i_name, unit, price, description, longdescription, time, imgURL, id) VALUES (:name, :unit,:price, :description, :longdescription, :time,:imgURL, :id)";
$sql_comment = "INSERT INTO comment(c_name, rating, words, ingredient_name, id) VALUES(:name, :rating, :words, :ingredient_name, :id)";
$ing_stm = $dbh->prepare($sql_ingredient);
$com_stm = $dbh->prepare($sql_comment);
foreach($ingredients as $current_ingredient){
testedInsertIngredient($current_ingredient,$ing_stm);
}
foreach($comments as $current_comment){
testedInsertComment($current_comment, $com_stm);
}
}
function testedInsertIngredient($ingredient, $stmt){
global $dbh;
if(!$stmt->execute(array(
':name'=>$ingredient['Name'],
':unit'=>$ingredient['Unit'],
':price'=>$ingredient['Price'],
':description'=>$ingredient['Description'],
':longdescription'=>$ingredient['Long_Description'],
':time'=>$ingredient['Time'],
':imgURL'=>$ingredient['IMGURL'],
':id'=>$ingredient['ID']
))){
echo '<pre class="bg-danger">';
print_r($dbh->errorInfo());
echo '</pre>';
}
}
function testedInsertComment($comment, $stmt){
global $dbh;
if(!$stmt->execute(array(
':name'=>$comment['Commenter Name'],
':rating'=>$comment['Rating'],
':words'=>$comment['Words'],
':ingredient_name'=>$comment['Ingredient Name'],
':id'=>$comment['ID']
))){
echo '<pre class="bg-danger">';
print_r($dbh->errorInfo());
echo '</pre>';
}
}