-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuy.php
More file actions
158 lines (113 loc) · 4.97 KB
/
Copy pathbuy.php
File metadata and controls
158 lines (113 loc) · 4.97 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
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
<?php
include("session.php");
include("config.php");
include("redirect.php");
?>
<!DOCTYPE html>
<html>
<head>
<?php include("bootstrap.php"); ?>
<title>Buy</title>
</head>
<body bgcolor = "#FFFFFF">
<!-- start cntainer div -->
<div class="container">
<!-- main content goes here -->
<div class="row">
<?php
include("menu.php");
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['buy'])) {
$adid=mysqli_real_escape_string($conn,$_POST['buy']);
$sql = "SELECT * FROM ads WHERE AdID = '$adid'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$buyerID=$_SESSION['userID'];
$card=$_SESSION['card'];
$sellerID=$row['ownerID'];
$is_item=1;
$purchaseType='Store Ad';
$price=$row['price'];
$date = date('Y-m-d');
$description=$row["description"];
$AdID= $row["AdID"];
$_SESSION['add_to_rate']=$AdID;
//$TID= 1;
//register the transaction
$sql = "INSERT INTO Transactions (`purchaseType`, `date`, `bill`, `is_item`, `buyerID`, `sellerID`, `card`)
VALUES('$purchaseType', '$date', '$price', '$is_item', '$buyerID', '$sellerID', '$card')";
if(mysqli_query($conn, $sql)){
echo "Transaction Completed!<br>";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($sql);
}
$sql1= "SELECT MAX(TID) FROM Transactions";
$result1 = $conn->query($sql1);
$row1 = $result1->fetch_assoc();
$TID= $row1['MAX(TID)'];
//register sold item into soldItems table;
$sql2 = "INSERT INTO soldItems (`description`, `AdID`, `TID`)
VALUES ('$description', '$AdID', '$TID')";
if(mysqli_query($conn, $sql2)){
echo "Item stored into sold items!<br>";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($sql2);
}
}
}
?>
<divclass="well well-sm">
<h2>Please rate the item that you just bought:</h2>
<form action="" name="display" method="POST">
<br>
<select name = "rating" class="custom-select">
<option value="1" > 1</option>
<option value="2"> 2</option>
<option value="3"> 3</option>
<option value="4"> 4</option>
<option value="5"> 5</option>
</select>
</br></br>
<input type="submit" name="rate" class="btn btn-info" value="Rate" />
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['rate'])) {
$rating = mysqli_real_escape_string($conn,$_POST['rating']);
//get the transaction id for the last sold item
$sql1= "SELECT MAX(TID) FROM Transactions";
$result1 = $conn->query($sql1);
$row1 = $result1->fetch_assoc();
$TID= $row1['MAX(TID)'];
//register rating in sold items as individual
$sql="UPDATE soldItems
SET rating = $rating
WHERE TID = '$TID'";
if($result = $conn->query($sql))
//register rating in ads table
$adId=$_SESSION['add_to_rate'];
$sql="SELECT COUNT(*) as items, SUM(rating) as sum from soldItems
where AdID='$adId' and rating is not NULL";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$items=$row['items'];
$sum=$row['sum'];
$total_rating=$sum/$items;
$total_rating=round($total_rating, 2);
//register rating in sold items as individual
$sql="UPDATE ads
SET rating = $total_rating
WHERE AdID = '$adId'";
if($result = $conn->query($sql)){
echo "<h3>Rating was succesful</h3>";
echo "</br> <h3>This Item now is rated ".$total_rating." / 5 Stars</h3>";
}
}
}
?>
<h2><a href='viewAds.php'>Keep Shopping</a></h2>
</div>
</div>
</div>
</body>
</html>