Källkodsbrowser

user.php

147 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
140
141
142
143
144
145
146
147
<?php
require 'check_login.php';
require 
'db_connect.php';

$user_id $_SESSION['user_id'];
$username $_SESSION['username'];
$message $_GET['msg'] ?? '';
$error $_GET['error'] ?? '';

// Hämta köphistorik (ordrar och orderdetaljer)
try {
    
$stmt $pdo->prepare("
        SELECT 
            o.order_id,
            o.created,
            b.book_name,
            bo.quantity,
            bo.price
        FROM `order` o
        JOIN bookorder bo ON o.order_id = bo.order_order
        JOIN book b ON bo.book_book_id = b.book_id
        WHERE o.customer_id = ?
        ORDER BY o.created DESC
    "
);
    
$stmt->execute([$user_id]);
    
$transactions $stmt->fetchAll();

    
// Gruppera transaktioner per order
    
$orders = [];
    foreach (
$transactions as $t) {
        
$order_id $t['order_id'];
        if (!isset(
$orders[$order_id])) {
            
$orders[$order_id] = [
                
'created' => $t['created'],
                
'total' => 0,
                
'items' => []
            ];
        }
        
$item_total $t['quantity'] * $t['price'];
        
$orders[$order_id]['total'] += $item_total;
        
$orders[$order_id]['items'][] = [
            
'book_name' => $t['book_name'],
            
'quantity' => $t['quantity'],
            
'price' => $t['price'],
            
'total' => $item_total
        
];
    }

} catch (
\PDOException $e) {
    
error_log("Database error fetching user transactions: " $e->getMessage());
    
$orders = [];
    
$error "Kunde inte hämta din köphistorik.";
}

// Hantera kontoradering (efter bekräftelse)
if (isset($_GET['delete']) && $_GET['delete'] === "1") {
    try {
        
// Ta bort användaren från databasen (ON DELETE CASCADE tar hand om ordrar)
        
$stmt $pdo->prepare("DELETE FROM customer WHERE cus_id = ?");
        
$stmt->execute([$user_id]);
        
        
// Logga ut direkt efter borttagning
        
session_destroy();
        
header("Location: login_form.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();
    }
}

// Funktion för att visa meddelanden
function display_message($msg$type 'success') {
    if (
$msg) {
        return 
"<p class='message $type'>" htmlspecialchars($msg) . "</p>";
    }
    return 
'';
}

?>
<!DOCTYPE html>
<html lang="sv">
<head>
    <meta charset="UTF-8">
    <title>Min Sida - E-bookstore</title>
    <link rel="stylesheet" href="style.css">
    <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>
        <div class="store-header">
            <h1>Min Sida</h1>
            <div class="header-buttons">
                <a href="index.php" class="btn">Tillbaka till butiken</a>
                <a href="logout.php" class="logout">Logga ut</a>
            </div>
        </div>
    </header>

    <main>
        <?= display_message($message?>
        <?= display_message($error'error'?>

        <section class="user-info">
            <h2>Välkommen, <?= htmlspecialchars($username?></h2>
            <div class="user-actions">
                <a href="change_password.php" class="btn">Byt lösenord</a>
                <a href="#" onclick="confirmDelete()" class="btn delete-btn">Ta bort konto</a>
            </div>
        </section>

        <section class="transaction-history">
            <h2>Köphistorik</h2>
            <?php if (empty($orders)): ?>
                <p>Du har inte gjort några köp ännu.</p>
            <?php else: ?>
                <?php foreach ($orders as $order_id => $order): ?>
                    <div style="border: 1px solid #ccc; padding: 15px; margin-bottom: 15px; border-radius: 5px;">
                        <p><strong>Order #<?= $order_id ?></strong> (<?= date('Y-m-d H:i'strtotime($order['created'])) ?>)</p>
                        <ul style="list-style: none; padding: 0; margin: 10px 0;">
                            <?php foreach ($order['items'] as $item): ?>
                                <li class="transaction-item">
                                    <span><?= htmlspecialchars($item['book_name']) ?> (<?= $item['quantity'?> st)</span>
                                    <span class="amount"><?= number_format($item['total'], 2','' '?> kr</span>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                        <p style="text-align: right; font-weight: bold;">Totalt: <?= number_format($order['total'], 2','' '?> kr</p>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </section>
    </main>

    <footer>
        <p>&copy; 2025 E-bookstore</p>
    </footer>
</body>
</html>