-
Notifications
You must be signed in to change notification settings - Fork 0
/
createCusPurchases.php
162 lines (127 loc) · 5.27 KB
/
createCusPurchases.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
<?php
// Use an HTML form to create a new entry in the Customer table.
// When SUBMIT button pressed, open new PDO (PHP data object) connection,
// then send INSERT SQL statement with the users inputted values
if (isset($_POST['create'])) {
// config.php holds the server information
// change config file to local host (the one you made in Hazra's tutorial)
// common.php maintains special characters used in html that would otherwise
// not be recognized as HTML, by calling method escape(<html>)
require "config.php";
require "common.php";
try {
//open connection with information from config.php
$connection = new PDO($dsn, $username, $password, $options);
// create variables from users form inputs. In PHP, values are placed
// into $_POST array
$new_user = array(
"CustomerID" => $_POST['CustomerID'],
"EquipmentID" => $_POST['EquipmentID'],
);
// create an SQL statement to insert users input
$sql = sprintf(
"INSERT INTO %s (%s) values (%s)",
"Rents",
implode(", ", array_keys($new_user)),
":" . implode(", :", array_keys($new_user))
);
// send the SQL to the server
$statement = $connection->prepare($sql);
$statement->execute($new_user);
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
} else if (isset($_POST['view'])){
try {
require "config.php";
require "common.php";
$connection = new PDO($dsn, $username, $password, $options);
$sql = "SELECT *
FROM Rents";
$statement = $connection->prepare($sql);
$statement->bindParam(':CustomerID', $CustomerID, PDO::PARAM_STR);
$statement->execute();
$result = $statement->fetchAll();
if ($result && $statement->rowCount() > 0) {
echo "<table><tr><th class='border-class'>CustomerID</th>
<th class='border-class'>EquipmentID</th>";
// output data of each row
foreach($result as $row) {
echo "<tr><td class='borderclass'>".$row["CustomerID"]."</td><td class='borderclass'>".$row["EquipmentID"]."</td></tr>";}
echo "</table>";
} else {
echo "0 results";
}
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
} else if (isset($_POST['divide'])){
try {
require "config.php";
require "common.php";
$connection = new PDO($dsn, $username, $password, $options);
$sql = "SELECT CustomerID
FROM Customer
WHERE NOT EXISTS
(SELECT *
from Equipment
WHERE NOT EXISTS
(SELECT CustomerID
FROM Rents
WHERE Equipment.EquipmentID = Rents.EquipmentID AND Customer.CustomerID=Rents.CustomerID))";
$statement = $connection->prepare($sql);
$statement->bindParam(':CustomerID', $CustomerID, PDO::PARAM_STR);
$statement->execute();
$result = $statement->fetchAll();
if ($result && $statement->rowCount() > 0) {
echo "<table><tr><th class='border-class'>CustomerID</th>";
// output data of each row
foreach($result as $row) {
echo "<tr><td class='borderclass'>".$row["CustomerID"]."</td></tr>";}
echo "</table>";
} else {
echo "0 results";
}
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
}
?>
<!-- include website title/headers/etc, a "successfully added" message,
and the input form itself.-->
<?php include "templates/header.php"; ?>
<?php if (isset($_POST['Create Equipment Rental']) && $statement) { ?>
> <?php echo $_POST['EquipmentID']; ?> successfully added.
<?php } ?>
<h2 style="color:white;">Equipment Rentals</h2>
<form method="post">
<p>
<input type="submit" name = "view" value="View Equipment Rentals"></p>
<p>
<input type="submit" name = "divide" value="View Customers that have Rented All Equipment"></p>
<p>
<label for="CustomerID">CustomerID</label>
<input type="text" name="CustomerID" id="CustomerID">
<label for="EquipmentID">EquipmentID</label>
<input type="text" name="EquipmentID" id="EquipmentID">
<input type="submit" name="create" value="Create Equipment Rental">
</p>
<!-- <p>-->
<!-- <label for="CustomerIDUp">CustomerID to Update</label>-->
<!-- <input type="text" name="CustomerIDUp" id="CustomerIDUp">-->
<!---->
<!-- <label for="EquipmentIDUp">EquipmentID to Update</label>-->
<!-- <input type="text" name="EquipmentIDUp" id="EquipmentIDUp">-->
<!---->
<!-- <input type="submit" name = "update" value="Update Equipment Rental">-->
<!-- </p>-->
<!---->
<!-- <p>-->
<!-- <label for="CustomerIDDel">CustomerID to Delete</label>-->
<!-- <input type="text" name="CustomerIDDel" id="CustomerIDDel">-->
<!-- <input type="submit" name = "delete" value="Delete Equipment Rental">-->
<!-- </p>-->
</form>
<a href="createBuys.php">Manage Ticket Purchase</a>
<a href="indexCustomer.php">Back to Customer Management</a>
<?php include "templates/footer.php"; ?>