1

I have moved on to making my website online thus using 000webhost.

since 000webhost has a dedicated Mysql for their database I created one from there identically to the one I have (in Xampp).

The problem is the sign-up PHP I made isn't working anymore.

Here is my PHP for the sign-up.

<?php

session_start();

require "db_conn.php";
require_once "emailControl.php";

$error = array();
$username = "";
$email = "";

if (isset($_POST['signup-btn'])) {
    $username = $_POST['username'];

    $email = $_POST['email'];

    $password = $_POST['password'];

    $passwordConf = $_POST['passwordConf'];

    if (empty($username)) {
        $error['username'] = "Username required";
    }

    if (empty($email)) {
        $error['email'] = "Email required";
    }
    if (empty($password)) {
        $error['password'] = "Password required";
    }

    if ($password !== $passwordConf) {
        $error['password'] = "The passwords does 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) {
        $error['email'] = "Email already exists";
    }

    $usernameQuery = "SELECT * FROM users WHERE user_name=? LIMIT 1";
    $stmt = $conn->prepare($usernameQuery);
    $stmt->bind_param('s', $username);
    $stmt->execute();
    $result = $stmt->get_result();
    $userCount = $result->num_rows;
    $stmt->close();

    if ($userCount > 0) {
        $error['username'] = "Username already exists";
    }

    if (count($error) === 0) {
        $password = password_hash($password, PASSWORD_DEFAULT);
        $token = bin2hex(random_bytes(50));

        $verified = false;

        $sql = "INSERT INTO users (user_name, email, verified, token, password) VALUES (?, ?, ?, ?, ?)";
        $stmt = $conn->prepare($sql);
        $stmt->bind_param('ssbss', $username, $email, $verified, $token, $password);

        if ($stmt->execute()) {
            $user_id = $conn->insert_id;
            $_SESSION['id'] = $user_id;
            $_SESSION['username'] = $username;
            $_SESSION['email'] = $email;
            $_SESSION['verified'] = $verified;

            $_SESSION['message'] = "you are now logged in!";
            $_SESSION['validate'] = "An email will be send to verify your account! please check your email and click the link inside.";
            $_SESSION['validate1'] = "You are now verified! you can now enter and enroll for SHS in Theresian School of Cavite! Thank you and have a great day!";

            header('location: index.php');
            exit();
        } else {
            $error['db_error'] = "Database error: Failed to register";
        }
    }
}

I have deduced that the Data from the forms in the sign-up webpage isn't going through the database and just displays the else statement.

I don't understand why this is not working as when I tried to do this to the Mysql on my computer it works perfectly.

does anyone know why this is happening?

Dharman
  • 30,962
  • 25
  • 85
  • 135
someone
  • 11
  • 3
  • Silly question, but have you verified that the MySQL account has the correct privileges to `INSERT` records into the `users` table? – matigo May 02 '21 at 04:26
  • @matigo what privileges are you talking about? I'm still quite new to this MySQL thing. – someone May 02 '21 at 04:44
  • When a user account is created in MySQL you can specify whether it may perform the various actions. The account may be able to `SELECT`, but not `INSERT`, `UPDATE`, or `DELETE`. This should be confirmed. – matigo May 02 '21 at 04:47
  • @matigo I don't think I have set my MySQL to do those. How can I make it to do those? This is my first time reading about making the account in MySQL to insert, update, or delete. When i made the Xampp one it just works. – someone May 02 '21 at 04:59
  • Which else is displayed? What do you exactly see? – Dharman May 02 '21 at 13:03
  • @Dharman it basically just goes to the else statement which shows the db_error. – someone May 03 '21 at 02:36

0 Answers0