46 lines
1.6 KiB
PHP
46 lines
1.6 KiB
PHP
<?php
|
|
// Simpan dengan nama file: register.php
|
|
include 'Config.php';
|
|
|
|
if (isset($_POST['btn-register'])) {
|
|
$username = $_POST['username'];
|
|
$email = $_POST['email'];
|
|
$password = $_POST['password'];
|
|
$confirm = $_POST['confirm_password'];
|
|
|
|
if (empty($username) || empty($email) || empty($password) || empty($confirm)) {
|
|
echo "<script>alert('Semua data harus diisi!'); window.location='index.html';</script>";
|
|
exit;
|
|
}
|
|
|
|
if ($password !== $confirm) {
|
|
echo "<script>alert('Password dan Konfirmasi tidak cocok!'); window.location='index.html';</script>";
|
|
exit;
|
|
}
|
|
|
|
// Cek Username/Email di tabel 'users'
|
|
$stmt = mysqli_prepare($conn, "SELECT id FROM users WHERE username = ? OR email = ?");
|
|
mysqli_stmt_bind_param($stmt, "ss", $username, $email);
|
|
mysqli_stmt_execute($stmt);
|
|
mysqli_stmt_store_result($stmt);
|
|
|
|
if (mysqli_stmt_num_rows($stmt) > 0) {
|
|
echo "<script>alert('Username atau Email sudah terpakai!'); window.location='index.html';</script>";
|
|
exit;
|
|
}
|
|
mysqli_stmt_close($stmt);
|
|
|
|
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
|
|
|
|
// Insert ke tabel 'users'
|
|
$stmtInsert = mysqli_prepare($conn, "INSERT INTO users (username, email, password) VALUES (?, ?, ?)");
|
|
mysqli_stmt_bind_param($stmtInsert, "sss", $username, $email, $hashed_password);
|
|
|
|
if (mysqli_stmt_execute($stmtInsert)) {
|
|
echo "<script>alert('Registrasi Berhasil! Silakan Login.'); window.location='index.html';</script>";
|
|
} else {
|
|
echo "Error: " . mysqli_error($conn);
|
|
}
|
|
mysqli_stmt_close($stmtInsert);
|
|
}
|
|
?>
|