Data can be saved into the database when i sign up for the first time. But when I want to sign up for a second account, the error appears "Database error: failed to register".
After deleting the data (the first account that i registered) directly from phpmyadmin, data can be saved again. However, the error appears again when i want to sign up for a second account.
I am not really sure what is causing the problem here.
authController.php
<?php
session_start();
require 'config/db.php';
$errors = array();
$username = "";
$email = "";
$contact = "";
$hostel = "";
//when user clicks sign up button
if (isset($_POST['signup-btn'])) {
$username = $_POST['username'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$hostel = $_POST['hostel'];
$password = $_POST['password'];
$passwordConf = $_POST['passwordConf'];
if (count($errors) === 0){
$password = password_hash($password, PASSWORD_DEFAULT);
$verified = false;
$sql = "INSERT INTO users (username, email, contact, hostel, verified, token, password) VALUES (?, ?, ?, ?, ?, ?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param('ssssdss', $username, $email, $contact, $hostel, $verified, $token, $password);
if ($stmt->execute()) {
$user_id = $conn->insert_id;
$_SESSION['id'] = $user_id;
$_SESSION['username'] = $username;
$_SESSION['email'] = $email;
$_SESSION['contact'] = $contact;
$_SESSION['hostel'] = $hostel;
$_SESSION['verified'] = $verified;
$_SESSION['message'] = "You are now logged in!";
$_SESSION['alert-class'] = "alert-success";
header('location: verify.php');
exit();
} else {
$errors['db_error'] = "Database error: failed to register";
}
}
}
?>
db.php
<?php
define ('DB_HOST', 'localhost');
define ('DB_USER', 'root');
define ('DB_PASS', '');
define ('DB_NAME', 'user-verification');
$conn = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
//if connection failed
if ($conn->connect_error){
die('Database error:' . $conn->connect_error);
}
echo "Connected successfully";
?>
signup.php
<div class="signup-form">
<form action="signup.php" method="post">
<h1 class="text-center"><b>Create Account</b></h1>
<?php if (count($errors) > 0): ?>
<div class="alert alert-danger">
<?php foreach($errors as $error): ?>
<li><?php echo $error; ?></li>
<?php endforeach; ?>
</div>
<?php endif; ?>
<div class="form-group">
<input type="text" name="username" placeholder="Name" value="<?php echo $username; ?>">
</div>
<div class="form-group">
<input type="email" name="email" placeholder="example@student.usm.my" value="<?php echo $email; ?>">
</div>
<div class="form-group">
<input type="tel" name="contact" placeholder="Contact Number e.g. 0121234567" value="<?php echo $contact; ?>">
</div>
<div class="form-group">
<select class="hostel" name="hostel" value="<?php echo $hostel; ?>">
<option selected hidden>Choose your Hostel</option>
<option value="alpha">Aplha</option>
<option value="omega">Omega</option>
</select>
</div>
<div class="form-group">
<input type="password" name="passwordConf" placeholder="Confirm password">
</div>
<div class="form-group">
<button type="submit" name="signup-btn" class="btn btn-primary btn-block btn-sign-in">Create Account</button:sumbit>
</div>
<p class="text-center">Already joined?<a href="login.php"> Sign in</a></p>
</form>
</div>