duanli0162 2015-02-05 23:08
浏览 17
已采纳

试图阻止创建重复用户

I cannot for the life of me find out what is wrong with this. It is probably some hidden rule or silly mistake. This code is meant to prevent users from creating the same user or same email twice but only works for the username. Am I missing something or is it just a silly mistake? I am using two if statements to try and achieve this (as noted by comments) but it is not working.

Thanks in advance

$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['emailAddress'];


// Create connection
$conn = new mysqli($servername, $SQLusername, $SQLpassword, $DBname);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysql_connect_error());
}

$sql = "SELECT * FROM `Login` WHERE `Username` LIKE '$username' AND `Password` LIKE '$password';";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);

$emailDB = $row['Email'];
$usernameDB = $row['Username'];

// username validation
if ($username == $usernameDB)
{
    echo "Username '$username' is already taken!<br>";
    exit;
}
//E-mail validation
if ($email == $emailDB)
{
    echo "Email '$email' is already registered!<br>";
    exit;
}
  • 写回答

1条回答 默认 最新

  • dounao2829 2015-02-05 23:20
    关注

    You just need another query :))) :

    $sql = "SELECT * FROM `Login` 
           WHERE `Username` LIKE '$username' 
           OR `Email` LIKE '$email';";
    $result = mysqli_query($conn, $sql);
    $row = mysqli_fetch_assoc($result);
    
    $emailDB = $row['Email'];
    $usernameDB = $row['Username'];
    
    // username validation
    if ($username == $usernameDB)
    {
        echo "Username '$username' is already taken!<br>";
        exit;
    }
    //E-mail validation
    if ($email == $emailDB)
    {
        echo "Email '$email' is already registered!<br>";
        exit;
    }
    

    and you should better use:

    "SELECT * FROM `Login` 
               WHERE `Username` = '$username' 
               OR `Email` = '$email';"
    

    because if it not really equal, new user can be registered :-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择