-
Notifications
You must be signed in to change notification settings - Fork 0
/
budgetconnect.php
67 lines (64 loc) · 2.81 KB
/
budgetconnect.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
<?php session_start();
if(!isset($_SESSION['user_name']))
header("Location:login.php");
?>
<?php
$amount = $_POST['amount'];
$category = $_POST['category'];
$mysqli = new mysqli('localhost', 'root', '', 'safespend-2');
if ($mysqli->connect_error) {
die('Connection Failed: ' . $mysqli->connect_error);
} else {
session_start();
$email = $_SESSION['user_name'];
$query = "SELECT max(BID) FROM budget";
if ($result = $mysqli->query($query)) {
if ($row = $result->fetch_assoc()) {
$BID = $row['max(BID)'];
$BID = $BID + 1;
} else {
$BID = 1;
}
}
$query = "select * from Budget join keeps on BID=Budget_ID where Emailkeeps='$email' and category = '$category'";
$result = $mysqli->query($query);
if (mysqli_num_rows($result) == 0) {
$stmt = $mysqli->prepare("insert into budget (BID,Total_amount,category) values(?,?,?)");
$stmt->bind_param("iis", $BID, $amount, $category);
$stmt->execute();
$stmt->close();
$stmt = $mysqli->prepare("insert into keeps (Emailkeeps,Budget_ID) values(?,?)");
$stmt->bind_param("si", $email, $BID);
$stmt->execute();
$stmt->close();
$query = "select * from Transaction join performs on TID=Transaction_ID where Emailperforms='$email' and category='$category' and Type='Debit'";
$result = $mysqli->query($query);
if (mysqli_num_rows($result) > 0) {
$stmt = $mysqli->prepare("update budget set Spent_amount = (select sum(amount) from Transaction join performs on TID=Transaction_ID where category='$category' and Emailperforms='$email' and Type='Debit') where category='$category' and BID='$BID'");
$stmt->execute();
$stmt->close();
}
$query = "select * from Transaction join performs on TID=Transaction_ID where Emailperforms='$email' and category='$category' and Type='Credit'";
$result = $mysqli->query($query);
if (mysqli_num_rows($result) > 0) {
$stmt = $mysqli->prepare("update budget set Total_amount = (select sum(amount) from Transaction join performs on TID=Transaction_ID where category='$category' and Emailperforms='$email' and Type='Credit')+$amount where category='$category' and BID='$BID'");
$stmt->execute();
$stmt->close();
}
}
else
{
$query = "select * from Budget join keeps on BID=Budget_ID where Emailkeeps='$email' and category = '$category'";
$result = $mysqli->query($query);
$rows = $result->fetch_assoc();
$BID = $rows['BID'];
$current_budget = $rows['Total_amount'];
$total_amount = $amount + $current_budget;
$stmt = $mysqli->prepare("UPDATE Budget set Total_amount =$total_amount where BID='$BID'");
$stmt->execute();
$stmt->close();
}
$mysqli->close();
header('Location: budget-index.php');
}
?>