/
update_item.php
118 lines (113 loc) · 3.81 KB
/
update_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="_script.js"></script>
<link rel="stylesheet" type="text/css" href="_main.css">
<link rel="logo_favicon.jpg" href="/favicon.ico"/>
<title>Aptaris - Advertisement Event System</title>
<div class="header"><a href="index.html">
<img src="images/logo_100.jpg" alt="logo" />
<h1>Advertisement Event System - Update an Item</h1></a>
<br/><hr/>
</div>
</head>
<body>
<center>
<?php
require ('db_connect.inc');
connect();
//Update item in the database
updateItem();
function updateItem() {
$itemNumber = $_POST['itemNumber'];
$itemNum = $_POST['itemNum'];
$description = $_POST['itemDescription'];
$category = $_POST['category'];
$deptName = $_POST['departmentName'];
$purchaseCost = $_POST['purchaseCost'];
$retailPrice = $_POST['retailPrice'];
$updateStatement = "Update Item SET ItemNumber = '".$itemNum."', ItemDescription = '".$description."', Category = '".$category."', DepartmentName = '".$deptName."', PurchaseCost = '".$purchaseCost."', FullRetailPrice = '".$retailPrice."' WHERE ItemNumber = '".$itemNumber."'";
// Execute the query--it will return either true or false
$result = mysql_query($updateStatement);
$message = "";
if(!$result) {
$message = "Error in updating Item: $itemNum, $description";
} else {
$message = "Data for Item: $itemNum updated successfully";
}
recalculateSalePrice($itemNum);
showItemUpdateResult($message, $itemNum, $description, $category, $deptName, $purchaseCost, $retailPrice);
}
function recalculateSalePrice($itemNum) {
$selectStatement = "Select * from PromotionItem Where ItemNumber = '".$itemNum."'";
$myResult = mysql_query($selectStatement);
while ($row = mysql_fetch_assoc($myResult)){
$promoCode = $row['PromoCode'];
$selectItemSql = "Select * from Item Where ItemNumber = '".$itemNum."'";
$selectPromotionSql = "Select * from Promotion Where PromoCode = '".$promoCode."'";
$itemResult = mysql_query($selectItemSql);
$promotionResult = mysql_query($selectPromotionSql);
$item = mysql_fetch_assoc($itemResult);
$promotion = mysql_fetch_assoc($promotionResult);
$itemPrice = $item['FullRetailPrice'];
$amountOff = $promotion['AmountOff'];
$discountType = $promotion['PromoType'];
if($discountType = "Dollar"){
$discountPrice = $itemPrice - $amountOff;
}
else{
$discountPrice = $itemPrice*(1-$amountOff);
}
$updateSql = "Update PromotionItem SET SalePrice = '".$discountPrice."' Where PromoCode = '".$promoCode."' AND ItemNumber = '".$itemNum."'";
$result1 = mysql_query($updateSql);
}
return;
}
function showItemUpdateResult($message, $itemNum, $description, $category, $deptName, $purchaseCost, $retailPrice) {
// If the message is non-null and not an empty string print it
// message contains the lastname and firstname
if ($message != "") {
echo <<<EOD
<h2 class='text-center'>$message</h2>
<table>
<tr>
<td>Item Number:</td>
<td>$itemNum</td>
</tr>
<tr>
<td>Department Name:</td>
<td>$deptName</td>
</tr>
<tr>
<td>Category:</td>
<td>$category</td>
</tr>
<tr>
<td>Purchase Cost:</td>
<td>$purchaseCost</td>
</tr>
<tr>
<td>Retail Price:</td>
<td>$retailPrice</td>
</tr>
<tr>
<td>Description:</td>
<td>$description</td>
</tr>
</table>
EOD;
} else {
echo "<h2>Error in inserting promotion</h2>";
}
}
?>
<p>
<a href="index.html"><button name="menu" class="button">Return to Main Menu</button></a>
<a href="update_item_search_view.html"><button name="update" accesskey="S" class="button">Update another item</button></a>
</p>
</center>
</body>
</html>