-
Notifications
You must be signed in to change notification settings - Fork 2
/
edit_item.php
94 lines (64 loc) · 3.01 KB
/
edit_item.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
<?php
require "vendor/autoload.php";
$id = strtolower($_POST["item_id"]);
$name = strtolower($_POST["name"]);
$ing = strtolower($_POST["ingredients"]);
$price = strtolower($_POST["price"]);
$hasimage = false;
if (!empty($_FILES['image']['name'])) {
$target_dir = "menu/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
$hasimage = true;
//echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
$var = array('error' => True, 'msg' => "Failed to upload file");
echo json_encode($var);
exit();
}
}
try {
$m = new MongoDB\Client("mongodb://vinay0410:[email protected]:23725/pizza");
$db = $m->pizza;
$collection = $db->menu;
} catch (Exception $e) {
$var = array('error' => True, 'msg' => "Couldn't Connect to Database");
echo json_encode($var);
exit();
}
$result = $collection->findOne(['_id' => new MongoDB\BSON\ObjectID( $id )]);
if ( ($result["name"] == $name) || (!$collection->findOne(array('name' => $name))) ) {
$document = array("name" => $name, "ingredients" => $ing, "price" => $price);
if ($hasimage) {
$document["path"] = $target_file;
if (isset($result["path"]) and (strpos($result_path, 'no_image') === false)) {
unlink($result["path"]);
}
}
$collection->updateOne(array('_id' => new MongoDB\BSON\ObjectID($id)), array('$set'=>$document));
$result = $collection->findOne(array('_id' => new MongoDB\BSON\ObjectID($id)));
?>
<div class="flipper agile-products">
<div class="front">
<img src="<?php echo $result['path']; ?>" class="img-responsive" alt="img">
<div class="gallery-des">
<h3><?php echo $result["name"]; ?></h3>
</div>
</div>
<div class="back">
<input type="hidden" name="item_id" id="item_id" value="<?php echo $result["_id"]; ?>">
<h4 class="editable"><?php echo $result["name"]; ?></h4>
<p class="editable"><?php echo $result["ingredients"]; ?></p>
<h6 class="editable"><?php echo $result["price"]; ?><sup>$</sup></h6>
<button type="button" name="edit_modal" class="btn btn-default btn-space" onclick="editable(this);"><span class="glyphicon glyphicon-pencil"></span> </button>
<button type="button" class="btn btn-danger btn-space bottomright" onclick="delete_item(this);"><span class="glyphicon glyphicon-remove"></span> </button>
<!--ending button for cart -->
</div>
<!--back -->
</div>
<?php
} else {
$var = array('error' => True, 'msg' => 'Item Name Already exists');
echo json_encode($var);
}
?>