-
Notifications
You must be signed in to change notification settings - Fork 1
/
add_product.php
166 lines (130 loc) · 5.31 KB
/
add_product.php
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
include_once('header.php');
if (!isset($_SESSION['usertype']) || strcmp($_SESSION['usertype'], "admin") != 0) {
echo "<h1>Permission denied.</h1>";
exit();
}
?>
<div class="col-md-4">
<?php if (!isset($_POST['submit'])) { ?>
<form method="post" action="add_product.php" enctype="multipart/form-data" onsubmit="return positivePrice();">
<fieldset>
<div id="legend" class="">
<legend class="">Add Product</legend>
</div>
<div class="control-group">
<!-- Text input-->
<label class="control-label" for="input01">Product Name</label>
<div class="controls">
<input type="text" id="product_name" placeholder="Product Name" class="form-control" name="name">
<p class="label label-warning"></p>
</div>
</div>
<div class="control-group">
<!-- Select Basic -->
<label class="control-label">Product Type</label>
<div class="controls">
<select class="form-control" name="type">
<option>Monitor</option>
<option>Mouse</option>
<option>Keyboard</option>
<option>SSD</option>
<option>Memory</option>
</select>
</div>
</div>
<div class="control-group">
<!-- Prepended text-->
<label class="control-label">Price</label>
<div class="controls">
<div class="input-group">
<span class="input-group-addon">$</span>
<input class="form-control" placeholder="Price" id="price" type="number" name="price" required="">
</div>
<p class="label label-warning"></p>
</div>
</div>
<div class="control-group">
<!-- <label class="control-label">File Button</label>-->
<!-- File Upload -->
<div class="controls">
<div class="fileinput fileinput-new" data-provides="fileinput">
<div class="fileinput-preview thumbnail" data-trigger="fileinput" style="width: 200px; height: 150px;"></div>
<div>
<span class="btn btn-default btn-file"><span class="fileinput-new">Select image</span><span class="fileinput-exists">Change</span><input type="file" id="imagefile" required="" name="image"></span>
<a href="#" class="btn btn-default fileinput-exists" data-dismiss="fileinput">Remove</a>
</div>
</div>
</div>
</div><div class="control-group">
<!-- Textarea -->
<label class="control-label">Description</label>
<div class="controls">
<div class="textarea">
<textarea type="" class="form-control" name="description"> </textarea>
</div>
</div>
</div><div class="control-group">
<label class="control-label"></label>
<!-- Button -->
<div class="controls">
<button class="btn btn-primary" name="submit" type="submit">Add This One</button>
</div>
</div>
</fieldset>
</form>
<?php } else {
$connection = Database::dbConnect();
if (!is_image($_FILES["image"]["tmp_name"])) {
echo "<h1>Product Image need a image file.</h1>";
exit();
}
$hash_image = md5_file($_FILES["image"]["tmp_name"]);
if (!file_exists("upload/" . $hash_image))
{
move_uploaded_file($_FILES["image"]["tmp_name"],
"upload/" . $hash_image);
// echo "Stored in: " . "upload/" . md5_file($_FILES["image"]["tmp_name"]);
}
$statement = oci_parse($connection, 'insert into "PRODUCT" ("NAME", "TYPE", description, price, imageUrl) '.
"values (:bv_name, :bv_type, :bv_description, :bv_price, :bv_imageUrl)");
oci_bind_by_name($statement, "bv_name", $_POST['name']);
oci_bind_by_name($statement, "bv_type", $_POST['type']);
oci_bind_by_name($statement, "bv_price", $_POST['price']);
oci_bind_by_name($statement, "bv_description", $_POST['description']);
oci_bind_by_name($statement, "bv_imageUrl", $hash_image);
if (oci_execute($statement)) {
echo "<h1>Product Added.</h1>";
} else {
$e = oci_error($statement);
echo $e['message'];
}
?>
<?php } ?>
</div><!-- <div class="col-md-4">-->
<?php
/**
* Created by PhpStorm.
* User: vienna
* Date: 13-12-7
* Time: 下午9:28
*/
include_once('footer.php');
?>
<script>
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function positivePrice() {
if (!isNumber($("#price").val())) {
alert("Product Price Need input a number.");
return false;
}
$("#price").val(eval(price.value));
if (price.value <= 0) {
alert("Positive Product price needed.");
return false;
}
return true;
}
</script>