-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaveItems.php
63 lines (55 loc) · 2.32 KB
/
saveItems.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
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
require 'php/databaseFunctions.php';
dbConnect();
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$i = 1;
while($i<=(int)$_POST['numPurchases']){
$sql = "INSERT INTO purchases (location, personName, amount, purchaseDate, account, budget)
VALUES ('" . $_POST["location" . $i] . "', '" .
$_POST["personName" . $i] . "', '" .
$_POST["amount" . $i] . "', '" .
$_POST["date" . $i] . "', '" .
$_POST["account" . $i] . "', '" .
$_POST["budget" . $i] . "');";
//write data to request table
if($conn->query($sql) == TRUE) {
echo "<p> New Purcase " . $_POST["location" . $i]. " Created Successfully </p>";
}else {
echo "Error: " .$sql ."<br />" . $conn->error;
}
//update account balance
$sql = "SELECT balance, type FROM accounts WHERE name='". $_POST["account" . $i] . "';";
$purchases = $conn->query($sql);
$row = $purchases->fetch_assoc();
if($row['type'] == "Checking/Savings"){
$newBal = $row['balance'] - $_POST["amount" . $i];
}
else{
$newBal = $row['balance'] + $_POST["amount" . $i];
}
$sql = "UPDATE accounts SET balance='" . $newBal . "' WHERE name='" . $_POST['account' . $i] . "';";
$conn->query($sql);
$i+=1;
}
$conn->close();
?>
<p> Returning to the home page in 5s </p> <?php header ("refresh:5; url=index.php"); ?>
</body>
</html>