View sourcecode

The following files exists in this folder. Click to view.

account.php

113 lines UTF-8 Unix (LF)
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
<?php
require "config.php";
if (!isset(
$_SESSION['user_id'])) { header("Location: login.php"); exit; }

$account_id $_GET['id'] ?? 0;

$stmt $db->prepare("SELECT * FROM accounts WHERE id = ? AND user_id = ?");
$stmt->execute([$account_id$_SESSION['user_id']]);
$account $stmt->fetch(PDO::FETCH_ASSOC);

if (!
$account) { die("Ogiltigt konto."); }

if (
$_SERVER['REQUEST_METHOD'] === 'POST') {
    
$amount = (int)$_POST['amount'];
    
$desc $_POST['description'];
    
$action $_POST['action'];

    if (
$amount 0) {
        
$change = ($action === "deposit") ? $amount : -$amount;

        if (
$action === "withdraw" && $account['balance'] < $amount) {
            
$error "Du kan inte ta ut mer än saldot.";
        } else {
            
$db->prepare("UPDATE accounts SET balance = balance + ? WHERE id = ?")
               ->
execute([$change$account_id]);

            
$db->prepare("INSERT INTO transactions (account_id, amount, description) VALUES (?, ?, ?)")
               ->
execute([$account_id$change$desc]);

            
header("Location: account.php?id=$account_id");
            exit;
        }
    }
}

$stmt $db->prepare("SELECT * FROM transactions WHERE account_id = ? ORDER BY created_at DESC");
$stmt->execute([$account_id]);
$transactions $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="sv">
<head>
    <meta charset="UTF-8">
    <title><?= htmlspecialchars($account['account_name']) ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">

<div class="container py-5">
    <div class="row justify-content-center">
        <div class="col-md-7">

            <div class="card shadow-sm">
                <div class="card-header bg-primary text-white text-center">
                    <h3><?= htmlspecialchars($account['account_name']) ?></h3>
                </div>

                <div class="card-body">

                    <p class="lead text-center">
                        Saldo:
                        <strong class="text-success"><?= $account['balance'?> kr</strong>
                    </p>

                    <?php if (!empty($error)): ?>
                        <div class="alert alert-danger"><?= $error ?></div>
                    <?php endif; ?>

                    <form method="post" class="mb-4">
                        <div class="mb-3">
                            <label class="form-label">Belopp</label>
                            <input type="number" name="amount" class="form-control" required min="1">
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Beskrivning</label>
                            <input type="text" name="description" class="form-control">
                        </div>

                        <div class="d-grid gap-2">
                            <button name="action" value="deposit" class="btn btn-success">Sätt in</button>
                            <button name="action" value="withdraw" class="btn btn-warning">Ta ut</button>
                        </div>
                    </form>

                    <hr>

                    <h5>Transaktionshistorik</h5>

                    <ul class="list-group">
                        <?php foreach ($transactions as $t): ?>
                            <li class="list-group-item d-flex justify-content-between 
                                <?= $t['amount'] >= 'list-group-item-success' 'list-group-item-danger' ?>">
                                <span><?= htmlspecialchars($t['description']) ?></span>
                                <strong><?= $t['amount'?> kr</strong>
                            </li>
                        <?php endforeach; ?>
                    </ul>

                </div>

                <div class="card-footer text-center">
                    <a href="dashboard.php" class="btn btn-outline-primary btn-sm">Tillbaka</a>
                </div>
            </div>

        </div>
    </div>
</div>

</body>
</html>