-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer.php
110 lines (92 loc) · 4.45 KB
/
transfer.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Transfer</title>
<link rel="stylesheet" href="bank.css">
<link rel="stylesheet" href="customer.css">
</head>
<body>
<?php
$DBname = "CityBank";
$con = mysqli_connect("localhost", "root", "");
$db = mysqli_select_db($con, $DBname);
if (isset($_GET['Email'])) {
$SenderEmail = $_GET['Email'];
$query = "SELECT * FROM CUSTOMERS WHERE Email='$SenderEmail'";
$result = $con->query($query) or die($con->error . __LINE__);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$SenderName = $row['Name'];
echo '<div class="customer-container">';
$query = "SELECT Name, Email FROM CUSTOMERS WHERE Email != '$SenderEmail'";
$result = $con->query($query) or die($con->error . __LINE__);
if ($result->num_rows > 0) {
echo '<form method="post" action="transfer.php">';
echo '<div class="transfer">';
echo '<div class="transfer-label">';
echo '<label for="transfer-to">Transfer To : </label>';
echo '<select id="transfer-to" name="ReceiverEmail">';
while ($row = $result->fetch_assoc()) {
echo '<option id="drop-opt" value="' . $row['Email'] . '">' . $row['Name'] . " ("
. $row['Email'] . ')</option>';
}
echo '</select>';
echo '</div>';
echo '</div>';
echo '<div class="transfer">';
echo '<div class="transfer-label">';
echo '<label for="transfer-amount">Enter Amount : </label>';
echo '<input type="number" id="transfer-amount" name="Amount" min="1" required>';
echo '</div>';
echo '</div>';
echo '<input type="hidden" name="SenderEmail" value="' . $SenderEmail . '">';
echo '<input type="hidden" name="SenderName" value="' . $SenderName . '">';
echo '<button type="submit">Transfer</button>';
echo '</form>';
} else {
echo '<p>No eligible customers to transfer to.</p>';
}
echo '</div>';
} else {
echo '<p>Customer not found.</p>';
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$SenderEmail = $_POST['SenderEmail'];
$ReceiverEmail = $_POST['ReceiverEmail'];
$Amount = $_POST['Amount'];
$query = "SELECT CurrentBalance FROM CUSTOMERS WHERE Email='$SenderEmail'";
$result = $con->query($query) or die($con->error . __LINE__);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$SenderBalance = $row['CurrentBalance'];
if ($SenderBalance >= $Amount) {
$query = "UPDATE CUSTOMERS SET CurrentBalance = CurrentBalance - $Amount WHERE Email='$SenderEmail'";
$result = $con->query($query) or die($con->error . __LINE__);
$query = "UPDATE CUSTOMERS SET CurrentBalance = CurrentBalance + $Amount WHERE Email='$ReceiverEmail'";
$result = $con->query($query) or die($con->error . __LINE__);
$query = "INSERT INTO TRANSFERS (SenderEmail, ReceiverEmail, Amount) VALUES ('$SenderEmail', '$ReceiverEmail', $Amount)";
$result = $con->query($query) or die($con->error . __LINE__);
echo '<div class="custom-alert">';
echo '<div class="custom-alert-text">';
echo '<span>Transfer successful!</span>';
echo '<button onclick="window.location.href=\'customers.php\'" id="ok">OK</button>';
echo '</div>';
echo '</div>';
} else {
echo '<div class="custom-alert">';
echo '<div class="custom-alert-text">';
echo '<span>Not Enough Money to Transfer!</span>';
echo '<button onclick="window.location.href=\'transfer.php?Email=' . $SenderEmail . '\'">OK</button>';
echo '</div>';
echo '</div>';
}
} else {
echo '<p>Customer not found.</p>';
}
}
?>
</body>
</html>