2017-04-01 35 views
-1

後,我修改了代碼,把$_SESSION['just'] = $username;無法在mysql中列中插入數據

我試圖做一個註冊系統,一旦用戶被註冊,這將產生一個OTP以下問題解決了,我在本地主機上測試網站。一旦用戶點擊註冊。 我的activateaccount.php不工作,它應該將otpstatus字段分別設置爲NULL和1。即使OTP由用戶插入不正確,它仍然將它們路由到login.html

這裏是我的signup.php代碼

<?php 
session_start(); 
require 'php includes/db.ini.php'; 
$fill = "fill all the feilds"; 

$random = mt_rand(1000,1000000); 

if(isset($_POST['first'])){ 
    $first = trim(mysqli_real_escape_string($connection,$_POST['first'])); 
    } 
    if(isset($_POST['last'])){ 
     $last = trim(mysqli_real_escape_string($connection,$_POST['last'])); 
     } 
     if(isset($_POST['username'])){ 
      $username = trim(mysqli_real_escape_string($connection,$_POST['username'])); 
      } 
      //invalid email can be submited, and will be accepted but the account won't be activated if the email dosen't get verified 
      if(isset($_POST['email'])){ 
       $email = trim(mysqli_real_escape_string($connection,$_POST['email'])); 
       } 
       if(isset($_POST['password'])){ 
        $password = trim(mysqli_real_escape_string($connection,$_POST['password'])); 
       } 

$required = array('first', 'last', 'username', 'email', 'password'); 

$error = false; 

foreach($required as $field) { 
    if (empty($_POST[$field])) { 
     $error = true; 
    } 
    } 

    if (!$error) { 
     //checking if any of the required feilds are empty 

      $sql = "SELECT Email FROM registeredusers WHERE Email='$email'"; 
      $result = mysqli_query($connection,$sql); 
      $emailCheck = mysqli_num_rows($result); 
      $structure = "users/$username/profile photos/project photos"; 

       if ($emailCheck>0){ 
        echo "email exists"; 
       }else{ 
        if (file_exists($structure)){ 
        echo "username already exists"; 
        } 
         else{ 
          $hpwd = password_hash($password,PASSWORD_DEFAULT); 
          $username_lower = strtolower($username); 
          $sql = "INSERT INTO registeredusers (FirstName,LastName,UserName,Email,Password,otp,status) 
          VALUES ('$first','$last','$username','$email','$hpwd','$random','0')"; 
          mkdir($structure,0777,true); 
          $result = mysqli_query($connection,$sql); 
          $_SESSION['just'] = $username; 
          header("Location:activateaccount.html"); 

         } 
        }}else{ 
         echo "fill out all the feilds"; 
        } 

?> 

我認爲我創造不正常的會話或我可能正確這裏不使用它來是我activateaccount.php代碼

<?php 
session_start(); 
include 'php includes/db.ini.php'; 

$username = $_SESSION['just']; 
echo $username; 

if(isset($_POST['submit'])){ 

    $submitted_otp = trim(mysqli_real_escape_string($connection,$_POST['otp'])); 

} 

$required = array('otp'); 

$error = false; 

foreach($required as $field) { 
    if (empty($_POST[$field])) { 
     $error = true; 
    } 
    } 

    if (!$error) { 
     $sql = "SELECT UserName FROM registeredusers WHERE UserName='$username'"; 
     $result = mysqli_query($connection,$sql); 
     $row = mysqli_fetch_assoc($result); 
     $otp = $row['otp']; 
     echo $otp; 

     if($otp !== $submitted_otp){ 

      echo "the otp isn't correct"; 

     }else{ 
      $update = "UPDATE registeredusers SET status='1', otp=NULL"; 
      session_destroy(); 
      header("Location: login.html"); 

     }} 


?> 

的時間間隔後,我會破壞activateaccount.php頁面上的會話。有人能幫助我嗎。我甚至無法使用另一個文件回顯會話。

回答

0

下面的條件永遠不會通過if條件,它總是執行else條件。請檢查以下更新的條件

if($otp != $submitted_otp){ 

請改變這種情況。頭location.Change它下面

$_SESSION['just'] = $row['UserName']; 
header("Location:activateaccount.html"); 

添加mysql_fetch_assoc

$sql = "INSERT INTO registeredusers (FirstName,LastName,UserName,Email,Password,otp,status) 
     VALUES ('$first','$last','$username','$email','$hpwd','$random','0')"; 
     mkdir($structure,0777,true); 
     $result = mysqli_query($connection,$sql); 
     $row = mysqli_fetch_assoc($result); 
     $_SESSION['just'] = $row['UserName']; 
+0

後,會議將永遠不會設置我編輯的代碼,但現在它是所有說'的OTP是incorrect'我仍然認爲有什麼東西我的會話錯了,我仍然無法將其回顯出來。 @PramodPatil –

+0

你檢查過兩個值是否一樣? –

+0

是的,這就是'if($ otp!== $ submitted_otp){ \t \t echo「the otp is not correct」; \t \t}''''''''將比較用戶輸入與我分配給變量'$ otp'的數據庫中的值做比較,我仍然認爲我在signup.php上創建的會話出了問題,導致I我也無法回顯$ otp變量。 @PramodPatil –