检查用户名和电子邮件已存在于 php mysql 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42460896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:36:12  来源:igfitidea点击:

check username and email already exists in php mysql

phpmysqlvalidation

提问by learner

check username and email already exists in php mysql,I have check every input is empty or not and if there is no empty then check username and password is already in database or not, if it is not in database then echo ALRIGHT, code works fine but the problem is, it doesn't print alrightwhen new entry is made.

检查用户名和电子邮件已存在于 php mysql 中,我已检查每个输入是否为空,如果没有空则检查用户名和密码是否已在数据库中,如果它不在数据库中,则 echo ALRIGHT,代码工作正常但问题是,当输入新条目时,它的打印效果不佳

Here is the code:

这是代码:

<?php
error_reporting(E_ALL & ~E_NOTICE);
require_once('dbcon.php');
if(isset($_POST["create"]))
{
    if (isset($_POST['username']) && !empty($_POST['username'])) {
        $username=mysqli_real_escape_string($conn,trim($_POST['username']));
    }else{
        $empty_username="Username Cannot be empty.";
        echo $empty_username.'<br>';
    }
    if (isset($_POST['email']) && !empty($_POST['email'])) {
        $email=mysqli_real_escape_string($conn,trim($_POST['email']));
    }else{
        $empty_email="Email cannot be empty.";
        echo $empty_email.'<br>';
    }

    if (isset($_POST['category']) && !empty($_POST['category'])) {
        $category=mysqli_real_escape_string($conn,trim($_POST['category']));
    }else{
        $empty_category="Category cannot be empty.";
        echo $empty_category.'<br>';
    }

    if (isset($_POST['password']) && !empty($_POST['password'])) {
        $psw=mysqli_real_escape_string($conn,trim($_POST['password']));
    }else{
        $empty_password="Password cannot be empty";
        echo $empty_password.'<br>';
    }

    if (isset($_POST['re_password']) && !empty($_POST['re_password'])) {
        $repsw=mysqli_real_escape_string($conn,trim($_POST['re_password']));
    }else{
        $empty_repassword="Retype password cannot be empty";
        echo $empty_repassword.'<br>';
    }

    $password=password_hash('$psw',PASSWORD_BCRYPT);
    $date=mysqli_real_escape_string($conn, trim('now()'));
    if($psw!=$repsw)
    {
        echo"password not Matched";
    }
        $sql="select * from account_info where (username='$username' or email='$email');";
        $res=mysqli_query($conn,$sql);
        if (mysqli_num_rows($res) > 0) {
        // output data of each row
        $row = mysqli_fetch_assoc($res);
        if ($username==$row['username'])
        {
            echo "Username already exists";
        }
        elseif($email==$row['email'])
        {
            echo "Email already exists";
        }
        else{
            echo "alright";
        }
    }
}
?>

回答by Arsalan Mithani

you've put this in wrong if condition

如果条件是你把它弄错了

$sql="select * from account_info where (username='$username' or email='$email');";
        $res=mysqli_query($conn,$sql);
        if (mysqli_num_rows($res) > 0) {
        // output data of each row
        $row = mysqli_fetch_assoc($res);
        if ($username==$row['username'])
        {
            echo "Username already exists";
        }
        elseif($email==$row['email']) // change it to just else
        {
            echo "Email already exists";
        }
        else{
            echo "alright"; // don't put it here
        }
    }

To print the alrightput your else outside the if and change elseif to just else like this :

要打印好的,请将您的 else 放在 if 之外并将 elseif 更改为 else ,如下所示:

$sql="select * from account_info where (username='$username' or email='$email');";

      $res=mysqli_query($conn,$sql);

      if (mysqli_num_rows($res) > 0) {
        // output data of each row
        $row = mysqli_fetch_assoc($res);
        if ($username==$row['username'])
        {
            echo "Username already exists";
        }
        else($email==$row['email'])
        {
            echo "Email already exists";
        }

       } else{ // if condition ends here if it is new entry, echo will work
            echo "alright";
        }

回答by shubham715

You only need to add else condition at right place

您只需要在正确的位置添加 else 条件

Try this:-

尝试这个:-

<?php
    error_reporting(E_ALL & ~E_NOTICE);
    require_once('dbcon.php');
    if(isset($_POST["create"]))
    {
        $error = 0;
        if (isset($_POST['username']) && !empty($_POST['username'])) {
            $username=mysqli_real_escape_string($conn,trim($_POST['username']));
        }else{
            $error = 1;
            $empty_username="Username Cannot be empty.";
            echo $empty_username.'<br>';
        }
        if (isset($_POST['email']) && !empty($_POST['email'])) {
            $email=mysqli_real_escape_string($conn,trim($_POST['email']));
        }else{
            $error =1;
            $empty_email="Email cannot be empty.";
            echo $empty_email.'<br>';
        }

        if (isset($_POST['category']) && !empty($_POST['category'])) {
            $category=mysqli_real_escape_string($conn,trim($_POST['category']));
        }else{
            $error = 1;
            $empty_category="Category cannot be empty.";
            echo $empty_category.'<br>';
        }

        if (isset($_POST['password']) && !empty($_POST['password'])) {
            $psw=mysqli_real_escape_string($conn,trim($_POST['password']));
        }else{
            $error = 1;
            $empty_password="Password cannot be empty";
            echo $empty_password.'<br>';
        }

        if (isset($_POST['re_password']) && !empty($_POST['re_password'])) {
            $repsw=mysqli_real_escape_string($conn,trim($_POST['re_password']));
        }else{
            $error = 1;
            $empty_repassword="Retype password cannot be empty";
            echo $empty_repassword.'<br>';
        }

        $password=password_hash('$psw',PASSWORD_BCRYPT);
        $date=mysqli_real_escape_string($conn, trim('now()'));
        if($psw!=$repsw)
        {
            echo "password not Matched";
        }

        if(!$error) {
            $sql="select * from account_info where (username='$username' or email='$email');";
            $res=mysqli_query($conn,$sql);
            if (mysqli_num_rows($res) > 0) {
            // output data of each row
            $row = mysqli_fetch_assoc($res);
            if ($username==$row['username'])
            {
                echo "Username already exists";
            }
            elseif($email==$row['email'])
            {
                echo "Email already exists";
            }
        }else { //here you need to add else condition
            echo "alright";
        }
        }
    }
    ?>

回答by learner

this can help you out for validating i only give you for username duplicate it it and make for email this ajax script is running on the same page no need to make an external file, what i mean is lets say u are validating on login.php this Ajax below stays on login.php all the scripts on the same page,change the PHP code to your specification. Broken down, what the oninput=username is saying is when the user starts typing run the function checkusername() and display result in

这可以帮助你验证我只给你用户名复制它并制作电子邮件这个ajax脚本在同一页面上运行不需要制作外部文件,我的意思是说你正在验证login.php下面的这个 Ajax 保留在 login.php 同一页面上的所有脚本,将 PHP 代码更改为您的规范。分解, oninput=username 所说的是当用户开始输入时运行函数 checkusername() 并显示结果

      <input type ="text" oninput="checkusername()" id="uname" placeholder="Your username for check" class="oshe">

       <span id="usernamestatus"></span>

//ajax script on the same page do not make a new file function checkusername
<script> function checkusername(){ 
var status = document.getElementById("usernamestatus");
 var u = document.getElementById("uname").value;
 if(u != ""){ status.innerHTML = '<b style="color:red;">checking...</b>'; var hr = new XMLHttpRequest(); 
hr.open("POST", "a.php", true); 
 hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); hr.onreadystatechange = function() { 
if(hr.readyState == 4 && hr.status == 200) {
 status.innerHTML = hr.responseText; 
} }
var v = "name2check="+u; hr.send(v); } } 
</script>



//the php script to search database
<?php
if(isset($_POST["name2check"]) && $_POST["name2check"] != ""){
include_once 'connect.php';
$username = ($_POST['name2check']); 
$sql_uname_check = mysqli_query($con, "SELECT matric FROM users WHERE matric='$username' LIMIT 1"); 
$uname_check = mysqli_num_rows($sql_uname_check);
if (strlen($username) < 2) {
echo '<b style="color:white;">2 - 35 characters please</b>';
exit();
}
if (!filter_var($username, FILTER_VALIDATE_EMAIL) === false) {
echo '<b style="color:white;">Email not allowed</b>';
exit();
}
if (is_numeric($username[0])) {
echo '<b style="color:white;">First character must be a letter</b>';
exit();
}
if ($uname_check < 1) {
echo '<strong style="color:white; text-transform:lowercase!important;">' . $username . ' is available </strong> ';
exit();
} else {
echo '<strong style="color:white; text-transform:lowercase!important;">' . $username . ' is taken </strong>';
exit();
}
}
?>