admin.php hinzugefügt, sql verbessert

This commit is contained in:
fds2610 2026-01-04 23:04:09 +01:00
parent 5962259f73
commit d8fdf5e3c2
3 changed files with 73 additions and 28 deletions

71
html/admin.php Normal file
View file

@ -0,0 +1,71 @@
<?php
// Datenbankverbindung
require 'config.php';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $dbuser, $dbpass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Datenbankverbindung fehlgeschlagen: " . $e->getMessage());
}
// Wenn das Formular abgesendet wurde
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = trim($_POST['username'] ?? '');
$password = $_POST['password'] ?? '';
if (!empty($username) && !empty($password)) {
$hash = password_hash($password, PASSWORD_DEFAULT);
try {
$stmt = $pdo->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
$stmt->execute([$username, $hash]);
$success = "Benutzer '$username' wurde erfolgreich angelegt.";
} catch (PDOException $e) {
$error = "Fehler beim Anlegen des Benutzers: " . $e->getMessage();
}
} else {
$error = "Bitte Benutzername und Passwort eingeben.";
}
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin: Benutzer anlegen</title>
<style>
body { font-family: Arial, sans-serif; max-width: 500px; margin: 0 auto; padding: 20px; }
.form-group { margin-bottom: 15px; }
label { display: block; margin-bottom: 5px; }
input[type="text"], input[type="password"] { width: 100%; padding: 8px; }
button { padding: 10px 15px; background: #007bff; color: white; border: none; cursor: pointer; }
.success { color: green; }
.error { color: red; }
</style>
</head>
<body>
<h1>Benutzer anlegen</h1>
<?php if (isset($success)): ?>
<p class="success"><?php echo htmlspecialchars($success); ?></p>
<?php endif; ?>
<?php if (isset($error)): ?>
<p class="error"><?php echo htmlspecialchars($error); ?></p>
<?php endif; ?>
<form method="post">
<div class="form-group">
<label for="username">Benutzername:</label>
<input type="text" id="username" name="username" required>
</div>
<div class="form-group">
<label for="password">Passwort:</label>
<input type="password" id="password" name="password" required>
</div>
<button type="submit">Benutzer anlegen</button>
</form>
</body>
</html>

View file

@ -2,8 +2,8 @@
session_start(); session_start();
$host = 'localhost'; $host = 'localhost';
$db = 'finanzapp'; $db = 'finanzapp';
$user = 'root'; // Anpassen! $user = 'user'; // Anpassen!
$pass = ''; // Anpassen! $pass = 'password'; // Anpassen!
$charset = 'utf8mb4'; $charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset"; $dsn = "mysql:host=$host;dbname=$db;charset=$charset";

View file

@ -1,26 +0,0 @@
-- Datenbank erstellen
CREATE DATABASE IF NOT EXISTS finanzapp;
USE finanzapp;
-- Tabelle für Benutzer
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL
);
-- Tabelle für Einträge
CREATE TABLE IF NOT EXISTS entries (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
date DATE NOT NULL,
category VARCHAR(50),
subject VARCHAR(255) NOT NULL,
amount DECIMAL(10,2) NOT NULL,
payment_method VARCHAR(50),
FOREIGN KEY (user_id) REFERENCES users(id)
);
-- Testbenutzer einfügen (Passwort: "test123")
INSERT INTO users (username, password) VALUES ('testuser', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi');