36274-vm/register.php
Flatlogic Bot 76b95bec18 v1
2025-11-25 15:04:51 +00:00

143 lines
6.6 KiB
PHP

<?php
require_once __DIR__ . '/db/config.php';
$pageTitle = "Register - Zeal Music";
$errors = [];
$successMessage = "";
// Fetch packages from the database
try {
$pdo = db();
$stmt = $pdo->query("SELECT id, name, price, features FROM packages ORDER BY price");
$packages = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$errors[] = "Database error: " . $e->getMessage();
$packages = [];
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = trim($_POST['username'] ?? '');
$email = trim($_POST['email'] ?? '');
$password = $_POST['password'] ?? '';
$password_confirm = $_POST['password_confirm'] ?? '';
$package_id = $_POST['package_id'] ?? '';
// Validation
if (empty($username)) $errors[] = 'Username is required.';
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) $errors[] = 'A valid email is required.';
if (strlen($password) < 8) $errors[] = 'Password must be at least 8 characters long.';
if ($password !== $password_confirm) $errors[] = 'Passwords do not match.';
if (empty($package_id)) $errors[] = 'Please select a package.';
// Check if user already exists
if (empty($errors)) {
try {
$pdo = db();
$stmt = $pdo->prepare("SELECT id FROM users WHERE username = :username OR email = :email");
$stmt->execute(['username' => $username, 'email' => $email]);
if ($stmt->fetch()) {
$errors[] = 'Username or email already exists.';
}
} catch (PDOException $e) {
$errors[] = "Database error: " . $e->getMessage();
}
}
// Insert new user
if (empty($errors)) {
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
try {
$pdo = db();
$stmt = $pdo->prepare("INSERT INTO users (username, email, password, role, package_id) VALUES (:username, :email, :password, 'artist', :package_id)");
$stmt->execute(['username' => $username, 'email' => $email, 'password' => $hashed_password, 'package_id' => $package_id]);
$successMessage = "Registration successful! You can now <a href='login.php'>log in</a>.";
} catch (PDOException $e) {
$errors[] = "Database error during registration: " . $e->getMessage();
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo htmlspecialchars($pageTitle); ?></title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body class="dark-theme">
<div class="page-container">
<!-- Simplified Sidebar for Auth Pages -->
<nav class="sidebar">
<div class="sidebar-header">
<a href="index.php" class="brand">Zeal Music</a>
</div>
</nav>
<!-- Main Content -->
<main class="main-content">
<div class="content-wrapper">
<div class="upload-form-container">
<h2>Create Your Artist Account</h2>
<br>
<?php if (!empty($errors)):
foreach ($errors as $error):
?>
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
<?php endforeach;
endif; ?>
<?php if ($successMessage): ?>
<div class="alert alert-success"><?php echo $successMessage; ?></div>
<?php else: ?>
<form action="register.php" method="POST" class="upload-form">
<div class="form-group">
<label for="username">Username</label>
<input type="text" id="username" name="username" placeholder="Your unique username" required value="<?php echo htmlspecialchars($username ?? ''); ?>">
</div>
<div class="form-group">
<label for="email">Email Address</label>
<input type="email" id="email" name="email" placeholder="Your contact email" required value="<?php echo htmlspecialchars($email ?? ''); ?>">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" name="password" placeholder="Minimum 8 characters" required>
</div>
<div class="form-group">
<label for="password_confirm">Confirm Password</label>
<input type="password" id="password_confirm" name="password_confirm" placeholder="Enter your password again" required>
</div>
<div class="form-group">
<label>Select a Package</label>
<div class="package-selection">
<?php foreach ($packages as $package): ?>
<div class="package-option">
<input type="radio" id="package_<?php echo $package['id']; ?>" name="package_id" value="<?php echo $package['id']; ?>" required>
<label for="package_<?php echo $package['id']; ?>">
<strong><?php echo htmlspecialchars($package['name']); ?></strong> - $<?php echo htmlspecialchars($package['price']); ?>/month
<p><?php echo htmlspecialchars($package['features']); ?></p>
</label>
</div>
<?php endforeach; ?>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-lg">Register</button>
</div>
<p style="text-align: center; margin-top: 20px;">
Already have an account? <a href="login.php" style="color: #1DB954;">Log In</a>
</p>
</form>
<?php endif; ?>
</div>
</div>
</main>
</div>
</body>
</html>