0

I'm trying to create a registration system using PHP + SQL. The code below work fine locally using a xampp installation, but when I try to run it on a remote host I can't get the registration to work. Specifically, even though the database is empty, when I try to register a new user, nothing happens even though I get the registration success echo message meaning PHP believes that all code and queries needed to register a user have been successfully run. However, the specific table remains empty.

    <?php
session_start();
include_once 'database_connection.php';
?>
<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width; initial-scale=1.0">
        <meta charset="utf-8">
        <title>Register</title>
        <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css">
    </head>
    <body>
        <div class="register">
            <h1>Register</h1>
            <form action="register.php" method="post" autocomplete="off">
                <label for="username">
                    <i class="fas fa-user"></i>
                </label>
                <input type="text" name="username" placeholder="Username" id="username" required>
                <label for="password">
                    <i class="fas fa-lock"></i>
                </label>
                <input type="password" name="password" placeholder="Password" id="password" required>
                <label for="email">
                    <i class="fas fa-envelope"></i>
                </label>
                <input type="email" name="email" placeholder="Email" id="email" required>
                <input type="submit" value="Register">
            </form>
        </div>
    </body>
</html>
<?php
if (!isset($_POST['username'], $_POST['password'], $_POST['email'])) 
{
    exit('Please fill in the registration form!');
}
if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['email'])) 
{
    exit('Please fill in all fields!');
}
if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
{
    exit('Invalid email!');
}
if (preg_match('/^[a-zA-Z0-9]+$/', $_POST['username']) == 0) 
{
    exit('Username is not valid!');
}
if (strlen($_POST['password']) > 30 || strlen($_POST['password']) < 6) 
{
    exit('Password must be between 6 and 30 characters long!');
}
if ($stmt = $connection->prepare('SELECT doctor_id, password FROM doctors WHERE username = ?')) 
{
    $stmt->bind_param('s', $_POST['username']);
    $stmt->execute();
    $stmt->store_result();
    if ($stmt->num_rows > 0) 
    {
        echo 'This Username is already taken. Please choose another one!';
    } 
    else 
    {
if ($stmt = $connection->prepare('INSERT INTO doctors (username, password, email) VALUES (?, ?, ?)'))
{
    $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
    $stmt->bind_param('sss', $_POST['username'], $password, $_POST['email']);
    $stmt->execute();
    echo 'Success! Registration complete! You may now login!';
} 
else
{
    echo 'SQL problem!';
}
    }
    $stmt->close();
} 
else
{
    echo 'SQL problem!';
}
$connection->close();
?>
ADbeat
  • 1
  • 3

0 Answers0