用户不是';t注册后重新定向


User isn't re-directed after signing up

我有一个用PDO编写的注册脚本。用户应该在注册后重新定向,但他们却停留在同一页面上。

这是我的数据库连接:

$db_username = "username";
$db_password = "password";
$con = new PDO("mysql:host=localhost;dbname=database", $db_username, $db_password);

这是我的注册脚本:

<?php
if(isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
if (empty($username)) {
    $errorusername = 'Please enter a username';
}else{
    if (empty($password)) {
        $errorpassword = 'Please enter a password';
    }else{
        if (empty($email)) {
            $erroremail = 'Please enter an email.';
        }else{
            $password = md5($password);
            $checkusername = $stmt = $con->prepare("SELECT * FROM users WHERE username=':username'");
            if (mysqli_num_rows($checkusername) == 1)
            {
                echo "Username already exists.";
            }else{
                $status = 'Hello there!';
                $about = 'Hello!';
                $stmt = $con->prepare("INSERT INTO users (username, password, email, status, about) VALUES (:username, :password, :email, :status, :about)");
                $stmt->bindParam(':username', $_POST['username']);
                $stmt->bindParam(':password', md5($_POST['password']));
                $stmt->bindParam(':email', $_POST['email']);
                $stmt->bindParam(':status', $status);
                $stmt->bindParam(':about', $about);
                $stmt->execute();
                header('Location: index.php');
            }
        }
    }
}
}
?>
<form method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="email" name="email">
<?php echo $errorusername = !empty($errorusername) ? $errorusername : ''; ?>
<?php echo $errorpassword = !empty($errorpassword) ? $errorpassword : ''; ?>
<?php echo $erroremail = !empty($erroremail) ? $erroremail : ''; ?>
<input type="submit" name="submit">
</form>

此外,我只是刚刚将我的代码从MYSQLI切换到PDO-是否有任何明显的错误,因为我没有完全的经验。

使用下面的select语句-

if(isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
if(empty($username)) {
$errorusername = 'Please enter a username';
}else{
if(empty($password)) {
$errorpassword = 'Please enter a password';
}else{
if(empty($email)) {
$erroremail = 'Please enter an email.';
}else{
$password = md5($password);
$stmt = $con->prepare("SELECT * FROM users WHERE username=':username'");
$stmt->bindParam(':username', $username);
$stmt->execute();
$total = $stmt->rowCount();
if($total == 1)
{
echo "Username already exists.";
}else{
$status = 'Hello there!';
$about = 'Hello!';

$stmt = $con->prepare("INSERT INTO users (username, password, email, status, about) VALUES (:username, :password, :email, :status, :about)");
$stmt->bindParam(':username', $_POST['username']);
$stmt->bindParam(':password', md5($_POST['password']));
$stmt->bindParam(':email', $_POST['email']);
$stmt->bindParam(':status', $status);
$stmt->bindParam(':about', $about);
$stmt->execute();
header('Location: index.php');
}
}
}
}
}
?>
<form method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="email" name="email">
<?php echo $errorusername = !empty($errorusername) ? $errorusername : ''; ?>
<?php echo $errorpassword = !empty($errorpassword) ? $errorpassword : ''; ?>
<?php echo $erroremail = !empty($erroremail) ? $erroremail : ''; ?>
<input type="submit" name="submit">
</form>