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?