Källkodsbrowser

user.php

139 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
require 'check_login.php';
require 
'db_connect.php';
$user $_SESSION['username'];

// Hämta saldo från databasen
function getSaldo($pdo$username) {
    
$stmt $pdo->prepare("
        SELECT SUM(CASE WHEN type='credit' THEN amount ELSE -amount END) AS saldo
        FROM trasaktioner
        WHERE username = ?
    "
);
    
$stmt->execute([$username]);
    
$result $stmt->fetch();
    return 
$result['saldo'] ?? 0;
}

$saldo getSaldo($pdo$user);

// Hantera insättning/uttag
if (isset($_POST['belopp'])) {
    
$belopp intval($_POST['belopp']);
    
$typ $_POST['typ'] === "uttag" "debit" "credit";

    if (
$typ === "debit" && $saldo $belopp) {
        
$fel "Otillräckligt saldo.";
    } else {
        
$stmt $pdo->prepare("INSERT INTO trasaktioner (username, amount, type) VALUES (?, ?, ?)");
        
$stmt->execute([$user$belopp$typ]);
        
header("Location: user.php");
        exit();
    }
}

// Hantera kontoradering (efter bekräftelse)
if (isset($_GET['delete']) && $_GET['delete'] === "1") {
    try {
        
// Ta bort användaren från databasen
        
$stmt $pdo->prepare("DELETE FROM users WHERE username = ?");
        
$stmt->execute([$user]);

        
// Ta bort alla transaktioner
        
$stmt $pdo->prepare("DELETE FROM trasaktioner WHERE username = ?");
        
$stmt->execute([$user]);

        
// Logga ut direkt efter borttagning
        
session_destroy();
        
header("Location: index.php?msg=Konto raderat permanent");
        exit();
    } catch (
\PDOException $e) {
        
error_log("Database error in user deletion: " $e->getMessage());
        
header("Location: user.php?error=Kunde inte radera kontot.");
        exit();
    }
}

// Hämta alla transaktioner för användaren (utan timestamp)
$stmt $pdo->prepare("SELECT amount, type FROM trasaktioner WHERE username = ? ORDER BY id DESC");
$stmt->execute([$user]);
$transaktioner $stmt->fetchAll();
?>
<!DOCTYPE html>
<html lang="sv">

<head>
    <meta charset="UTF-8">
    <title>Min bank</title>
    <style>
        body { font-family: Arial, sans-serif; background: #f0f2f5; margin: 0; }
        header, footer { background: #333; color: white; text-align: center; padding: 20px 15px; }
        .header-buttons { margin-top: 15px; display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
        .action-btn, .logout { color: white; text-decoration: none; padding: 8px 12px; border-radius: 5px; transition: 0.3s; }
        .action-btn { background: #1b91f8; } .action-btn:hover { background: #0268dc; }
        .delete-btn { background: #d64b3b; } .delete-btn:hover { background: #a9372a; }
        .logout { background: #555; } .logout:hover { background: #333; }
        main { max-width: 900px; margin: 20px auto; display: flex; flex-wrap: wrap; gap: 20px; }
        section { flex: 1; min-width: 280px; background: white; padding: 20px; border-radius: 10px; box-shadow: 0 0 6px rgba(0,0,0,0.1); }
        h2 { margin-top: 0; }
        form { display: flex; flex-direction: column; gap: 10px; }
        input[type="number"] { padding: 8px; border-radius: 5px; border: 1px solid #ccc; max-width: 200px; }
        form label { display: flex; align-items: center; gap: 5px; }
        button { max-width: 150px; padding: 8px; border-radius: 5px; border: none; background: #1b91f8; color: white; cursor: pointer; transition: 0.3s; }
        button:hover { background: #0268dc; }
        .error { color: red; }
        .transaction-list { max-height: 300px; overflow-y: auto; border-top: 1px solid #ddd; margin-top: 10px; padding-top: 10px; }
        .transaction-item.plus { color: green; }
        .transaction-item.minus { color: red; }
    </style>
    <script>
        function confirmDelete() {
            if (confirm("Är du säker på att du vill radera ditt konto? Detta går inte att ångra!")) {
                window.location = "user.php?delete=1";
            }
        }
    </script>
</head>

<body>
    <header>
        <h1>Välkommen <?= htmlspecialchars($user?></h1>
        <div class="header-buttons">
            <a href="change_password.php" class="action-btn">Byt lösenord</a>
            <a href="#" onclick="confirmDelete()" class="action-btn delete-btn">Ta bort konto</a>
            <a href="logout.php" class="logout">Logga ut</a>
        </div>
    </header>

    <main>
        <section>
            <h2>Saldo: <?= $saldo ?> kr</h2>
            <?php if (!empty($fel)) echo "<p class='error'>$fel</p>"?>
            <form method="post">
                <input type="number" name="belopp" min="1" placeholder="Belopp" required>
                <label><input type="radio" name="typ" value="insattning" checked> Insättning</label>
                <label><input type="radio" name="typ" value="uttag"> Uttag</label>
                <button>Utför</button>
            </form>
        </section>

        <section>
            <h2>Transaktioner</h2>
            <div class="transaction-list">
                <?php foreach ($transaktioner as $t):
                    
$klass $t['type'] === 'credit' "plus" "minus"?>
                    <div class="transaction-item <?= $klass ?>">
                        <?= $t['type'] === 'credit' '+' '-' ?><?= $t['amount'?> kr
                    </div>
                <?php endforeach; ?>
            </div>
        </section>
    </main>

    <footer>
        <p>&copy; 2025 Din Bank</p>
    </footer>
</body>

</html>