/
update_promotion.php
109 lines (103 loc) · 3.6 KB
/
update_promotion.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
<!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="icon" type="image/png" href="favicon.png">
<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 a Promotion</h1></a>
<br/><hr/>
</div>
</head>
<body>
<center>
<?php
require ('db_connect.inc');
connect();
//Update promotion in the database
updatePromotion();
function updatePromotion() {
$promoCode = $_POST['promoCode'];
$promoName = $_POST['promoName'];
$description = $_POST['promoDescription'];
$amountOff = $_POST['amountOff'];
$promoType = $_POST['promoType'];
if (($promoType == 'Percent') && ($amountOff >= 1)) {
$amountOff = str_replace("%", "", $amountOff);
$amountOff = $amountOff/100;
}
$updateStatement = "Update Promotion SET Name = '".$promoName."', Description = '".$description."', AmountOff = '".$amountOff."', PromoType = '".$promoType."' WHERE PromoCode = '".$promoCode."'";
// Execute the query--it will return either true or false
$result = mysql_query($updateStatement);
$message = "";
if(!$result) {
$message = "Error in updating Promotion: $promoCode, $description";
} else {
$message = "Data for Promotion: $promoCode updated successfully";
}
recalculateSalePrice($promoCode);
showItemUpdateResult($message, $promoCode, $description, $promoName, $amountOff, $promoType);
}
function recalculateSalePrice($promoCode) {
$selectStatement = "Select * from PromotionItem Where PromoCode = '".$promoCode."'";
$myResult = mysql_query($selectStatement);
while ($row = mysql_fetch_assoc($myResult)){
$itemNum = $row['ItemNumber'];
$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, $promoCode, $description, $promoName, $amountOff, $promoType) {
// 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>Description:</td>
<td>$description</td>
</tr>
<tr>
<td>Amount Off:</td>
<td>$amountOff</td>
</tr>
<tr>
<td>Promo Type:</td>
<td>$promoType</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_promotion_search_view.html"><button name="update" accesskey="S" class="button">Update another promotion</button></a>
</p>
</center>
</body>
</html>