Jump to content

PRINCE ADOMAKO

Members
  • Posts

    1
  • Joined

  • Last visited

Posts posted by PRINCE ADOMAKO

  1. 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

    <?php
    //error_reporting (E_ALL ^ E_NOTICE); 
    session_start();
     
    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);
        $stmt->bind_param('s'$email);
        $stmt->execute();
        $result = $stmt->get_result();
        $userCount = $result->num_rows;
        $stmt->close();
     
        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);
        $stmt->bind_param('ssbss'$username$email$verified$token$password);
     
        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";
            header('location:home.php');
            exit();
     
        } else
          $errors['db_error'] = "Database error: failed to register";
     
        }
    }
     
     
    And below are the source codes for the signup page
     
×
×
  • Create New...