Jump to content

Database Error


Recommended Posts

I'm following a tutorial on how to create a signup and a login page for my website I keep on receiving this error message "Database error"

Below is the authentication controller source code

//error_reporting (E_ALL ^ E_NOTICE); 
require 'config/dbb.php';
$errors = array();
$username = "";
$email = "";
//if user clicked on the sign up button
if (isset($_POST['signup-btn'])){
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $passwordConf = $_POST['passwordConf'];
    // validation
    if (empty($username)){
        $errors['username'] = "Username required";
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
        $errors['email'] = "Email address is invalid";
    if (empty($email)){
        $errors['email'] = "Email required";
    if (empty($password)){
        $errors['password'] = "Password required";
    if ($password !== $passwordConf){
        $errors['password'] = "The two passwords do not match";
    $emailQuery = "SELECT * FROM users WHERE email=LIMIT 1";
    $stmt = $conn->prepare($emailQuery);
    $result = $stmt->get_result();
    $userCount = $result->num_rows;
    if ($userCount > 0){
        $errors['email'] = "Email already exists";
    if (count($errors) === 0) {
        $password = password_hash($password, PASSWORD_DEFAULT);
        $token = bin2hex(random_bytes(50));
        $verified = false;
    $sqlQuery = "INSERT INTO users (username, email, verified, token, passwordVALUES (?, ?, ?, ?, ?)";
    $stmt = $conn->prepare($sqlQuery);
    if ($stmt->execute()) {
        //login user
        $user_id = $conn->insert_id;
        $_SESSION['id'] = $user_id;
        $_SESSION['username'] = $username;
        $_SESSION['email'] = $email;
        $_SESSION['verified'] = $verified;
        //set flash message
        $_SESSION['message'] = "You are now logged in!";
        $_SESSION['alert-class'] = "alert-success";
    } else
      $errors['db_error'] = "Database error: failed to register";
And below are the source codes for the signup page
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...