0

我已经通过“include.php”将我的 PHP 与我的数据库链接起来,它正在连接到数据库。但是它没有使用以下注册 PHP 在表中创建数据。

    <?php
    if(isset($_POST['doRegister'])){
        $username = $_POST['username'];
        $password = $_POST['password'];
        $verpassword = $_POST['verpassword'];
        $email = $_POST['email'];

        if(empty($username) || empty($password) || empty($verpassword) || empty($email)){
            echo "Please fill in all fields!";
        } else {
            if($password == $verpassword){
                include("include/db.php");

                $result = $mysql->query("SELECT * FROM `users` WHERE `username`='$username'");
                $numrows = $result->rowCount();

                if($numrows != 0){

                    $res = $mysql->query("SELECT * FROM `users` WHERE `email`='$email'");
                    $nums = $res->rowCount();

                    if($nums != 0){

                        $stmt = $mysql->prepare("INSERT INTO `users` VALUES (:id,:username,:password,:email,:salt,:isBanned,:isAdmin)");
                        $newsalt = "$2a$07$".uniqid(mt_rand(), true)."$";
                        $enc_pass = crypt($password, $newsalt);
                        $stmt->execute(array(':id' => '',':username' => $username,':password' => $enc_pass,':email' => $email,':salt' => $newsalt,':isBanned' => '0',':isAdmin' => '0',));
                echo "You have signed up successfully!";

                    } else {
                        echo "That username is already taken!";   
                    }

                } else {
                    echo "That email has already been registered! <a href='login.php'>login?</a>";   
                }


            } else {
                echo "Passwords do not match!";
            }
        }

    }
?>

这是我的数据库连接php。

<?php
try {
        $mysql = new pdo('mysql:host=*;dbname=*','*','*');   

} catch (Exception $e){
      die("Can't connect!".$e->getMessage());
  }

?>
4

1 回答 1

1

你的两个if条件都是inverse应该的。只有当用户名和电子邮件已经存在时,您当前的代码才会尝试添加新记录。

if($numrows != 0){   

应该

if($numrows == 0){

情况也是如此

if($nums != 0){

它应该是

if($nums == 0){
于 2013-11-04T13:19:21.710 回答