簡體   English   中英

檢查用戶名和電子郵件已經存在於php mysql中

[英]check username and email already exists in php mysql

檢查用戶名和電子郵件已經存在於php mysql中,我已檢查每個輸入是否為空,如果沒有空,則檢查用戶名和密碼是否已在數據庫中,如果不在數據庫中,則回顯ALRIGHT ,代碼工作正常但問題是,當新的入口是由不正常的打印。

這是代碼:

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

如果情況不對,您就把這個錯了

$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
        }
    }

要打印好的,請將您的else放在if之外,然后將elseif更改為this:

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

您只需要在正確的位置添加其他條件

嘗試這個:-

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

這可以幫助您進行驗證,我只給您提供用戶名重復它,並使其在電子郵件中運行此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();
}
}
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM