检查用户名和电子邮件已经存在于php mysql中 [英] check username and email already exists in php mysql

查看:97
本文介绍了检查用户名和电子邮件已经存在于php mysql中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检查用户名和电子邮件已经存在于php mysql中,我已检查每个输入是否为空,如果没有空,则检查用户名和密码是否已在数据库中,如果不在数据库中,则回显 > ALRIGHT ,代码可以正常工作,但是问题是,在输入新条目时,它不会打印 ALTIM .

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 alright when new entry is made.

这是代码:

<?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";
        }
    }
}
?>

推荐答案

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

You only need to add else condition at right place

尝试以下操作:-

<?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";
        }
        }
    }
    ?>

这篇关于检查用户名和电子邮件已经存在于php mysql中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆