尝试检查php数据库中是否存在用户时出错

bmvo0sr5  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(330)

我试图创建一个限制,当你把一个用户名或电子邮件,已经存在于数据库中,显示一条消息,说已经存在。但当我尝试时,它只是忽略了代码。

<?php

include 'config.php';

$sql_en = mysqli_query($conn, "SELECT `password`, `method` FROM `encryption`");

if (mysqli_num_rows($sql_en) > 0) {
    while($row = mysqli_fetch_assoc($sql_en)) {
        $password = $row["password"];
        $method = $row["method"];
    }
} else {
    echo "Error to find the keys to encrypt!";
}

if (isset($_POST["u_btn"])) {
    $u_name = $_POST["u_name"];
    $u_email = $_POST["u_email"];
    $u_pass = $_POST["u_pass"];

    if (empty($u_name) || empty($u_email) || empty($u_pass) ) {

        echo "Fill out all the fields!";
    } else {

        $check = mysqli_query($conn, "SELECT * FROM users where u_email = '$u_email' AND u_name = '$u_name'");

        $row = mysqli_fetch_array($check);

        if ($row["u_email"] == $u_email || $row["u_name"] == $u_name) {

            echo "Username already exists!";

            header('Location: register.php?err=true');
        } else {

        $encrypt_pass = $u_pass;

        // Must be exact 32 chars (256 bit)
        $password = substr(hash('sha256', $password, true), 0, 32);

        // IV must be exact 16 chars (128 bit)
        $iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);

        $encrypted = base64_encode(openssl_encrypt($encrypt_pass, $method, $password, OPENSSL_RAW_DATA, $iv));

        $insert = mysqli_query($conn,"INSERT INTO `users` (`u_name`, `u_email`, `u_pass`) VALUES ('$u_name', '$u_email', '$encrypted')");   

        header('Location: profile.php');
    }
    }
}

?>

<?php if (isset($_GET['err1'])) { ?>
    <div class="alert">
        <p>Login Failed! That username or email already exists! Do you wanto to go to the login page? <a href="login.php">Login</a>.</p>
    </div><?php } ?>

<?php if (isset($_GET['err2'])) { ?>
    <div class="alert">
        <p>Login Failed! That username or email already exists! Do you wanto to go to the login page? <a href="login.php">Login</a>.</p>
    </div><?php } ?>

<form action="register.php" method="post">
    <label>Name</label>
    <input type="text" name="u_name" value="" ></input><br />
    <label>Email</label>
    <input type="email" name="u_email" value="" ></input><br />
    <label>Password</label>
    <input type="password" name="u_pass" value="" ></input><br />
    <input type="submit" name="u_btn" value="Sing Up"></input>
    <input type="button" onclick="window.location='login.php';" value="Login"></input>
</form>

问题是,如果我把我在数据库中的电子邮件和名称,代码工作,但如果我只把名称或电子邮件,不检查它是否已经存在。

bihw5rsg

bihw5rsg1#

更改:

$check = mysqli_query($conn, "SELECT * FROM users where u_email = '$u_email' AND u_name = '$u_name'");

$check = mysqli_query($conn, "SELECT * FROM users where u_email = '$u_email' OR u_name = '$u_name'");

所以你把所有的东西都匹配起来,然后检查每个条目

相关问题