2017-02-25 83 views
-1

檢查用戶名和電子郵件已經存在於php mysql中,我檢查每個輸入是否爲空或如果沒有空然後檢查用戶名和密碼是否已經在數據庫中,如果它不在數據庫中,然後回顯ALRIGHT,代碼工作正常,但問題是,它不打印好吧當新的條目進行。檢查用戶名和電子郵件已存在於php mysql

下面是代碼:

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

好吧,有什麼問題了嗎? –

+0

執行代碼的結果是什麼,以及您希望得到的結果 – SaggingRufus

+0

幾乎可以工作,但是當我提供新的用戶名和電子郵件時,不會打印出「沒問題」的數據庫。對我的英語來說, – learner

回答

0

您只需要添加其他cond銀行足球比賽在正確的地方

試試這個: -

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

它顯示「正常」,但我不打印如果所有的輸入都是空白的。即使所有輸入都是空白,此代碼也會打印「正常」。輸出:-Username不能爲空。電子郵件不能爲空。密碼不能爲空重新輸入密碼不能爲空好吧 – learner

+0

我想檢查所有輸入是否檢查和$ psw!= $ repsw以及用戶名和電子郵件是否在數據庫中。只有當所有這些條件都滿足時纔打印ALRIGHT – learner

+0

現在查看我的回答我更新的代碼。它的工作原理是 – shubham715

2

你把這個錯的,如果條件

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

要打印好嗎把你別的之外,如果和elseif的改變,只是別人像這樣:

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

是的。比我的評論更好地解釋+1 –

+0

我沒有看到你的評論,我正在測試他的代碼,可能我們一起發佈:) –

+0

你可以拆分假互聯網點 – SaggingRufus

0

這可以幫助你來驗證我只給你的用戶名重複它,並讓這個AJAX腳本是在同一頁上運行電子郵件不需要創建外部文件,我的意思是讓我們說你在login.php上進行了驗證,下面的這個Ajax保留在login.php中同一頁面上的所有腳本,將PHP代碼更改爲您的規範。看,什麼oninput =用戶名的意思是,當用戶開始輸入運行功能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(); 
} 
} 
?> 
+0

他忘了提及他的實際問題,這不是他正在尋找的,請閱讀評論。 –

+0

我很難表達我的問題,因爲英語不是我的第一語言。對不起, – learner

+0

我已經在您的問題中添加了您的實際問題,它在審批隊列中,一經您或任何同行審查員批准後即會顯示。 –

相關問題