forked from zanericks/CSE110-Team-5-Retail-Account
/
account.php
58 lines (49 loc) · 1.31 KB
/
account.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
<?php
require('rules.php');
require('helper.php');
//Debits an account, returns true if successful, else false
function debit( $debitAmount, $accountNum)
{
$accdb = "accounts";
$acconn = connRTServer( $accdb);
$sql = "select balance from accounts where accNum = " . $accountNum . ";";
$result = $acconn->query($sql);
$currBal = $result->fetch_row();
//Check if can be debited
if(canDebit($currBal[0], $debitAmount))
{
$newBal = $currBal[0] - $debitAmount;
$sql2 = "update accounts set balance = " . $newBal . " where accNum = " . $accountNum . ";";
$acconn->query($sql2);
return TRUE;
}
else
{
return FALSE;
}
$acconn->close();
}
//Credits an account
function credit( $creditAmount, $accountNum)
{
$accdb = "accounts";
$conn = connRTServer( $accdb);
$sql = "select balance from accounts where accNum = " . $accountNum . ";";
$result = $conn->query($sql);
$currBal = $result->fetch_row();
$newBal = $creditAmount + $currBal[0];
$sql2 = "update accounts set balance = " . $newBal . " where accNum = " . $accountNum . ";";
$conn->query($sql2);
$conn->close();
}
//Transfers money from one account to another, returns true on success
function transfer( $toAccount, $fromAccount, $amount)
{
if(!debit( $amount, $fromAccount))
{
return false;
}
credit( $amount, $toAccount);
return true;
}
?>