2016-07-15 39 views
2

我有以下注冊php腳本。我想要得到一個json響應 {「result」:「success」,「message」:「123」}在我的json響應中獲得null值。

其中123是註冊用戶的id。我想要這個ID,以便稍後用戶可以將數據發佈到其他表。

但是我明白了。

{"result":"fail","message":null} 

這是我的腳本。

<?php 
    session_start(); 
    require "init.php"; 
    header('Content-type: application/json'); 
    $id = $_POST['id']; 
    $email = $_POST['email']; 
    $user_name = $_POST['user_name']; 

    $user_pass = $_POST['user_pass']; 
    $passwordEncrypted = sha1($user_pass); 

    $confirmPass = $_POST['confirm_pass']; 
    $confPasswordEncrypted = sha1($confirmPass); 

    $msg = "Congratulations. You are now registered to the most amazing app 
    ever!";    

     if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ 

      $don = array('result' =>"fail","message"=>"Please enter a valid email"); 

     }  

if($email && $user_name && $user_pass && $confirmPass && filter_var($email, FILTER_VALIDATE_EMAIL)){ 


    $sql_query = "select * from user_info WHERE email ='".mysqli_real_escape_string($con, $email)."' or user_name 
    ='".mysqli_real_escape_string($con, $user_name)."'"; 

    $result = mysqli_query($con, $sql_query); 

    $results = mysqli_num_rows($result); 

    if ($results){ 
     $don = array('result' =>"fail","message"=>"Email or username exists."); 

    }else{ 
     //This is where I am trying to get the id 
     while($row = mysqli_fetch_array($result)) {    
      $posts['id'] = $row['id']; 


     } 

     $sql_query = "insert into user_info values('$id','$email','$user_name','$passwordEncrypted','$confPasswordEncrypted');"; 

     if(mysqli_query($con,$sql_query)){ 
      $_SESSION['id'] = mysqli_insert_id($con); 
      //And this is the json response I was talking about 
      $don = array('result' =>"success","message"=>$posts['id']); 
      mail($email,"Well done. You are registered to my sample app!",$msg); 

     } 
    } 
}else if(!$email){ 


     $don = array('result' =>"fail","message"=>"Please enter a valid email");    


    }else if(!$user_name){ 

     $don = array('result' =>"fail","message"=>"Please enter your username"); 

    }else if(!$user_pass){ 

     $don = array('result' =>"fail","message"=>"Please enter a password"); 

    }else if(!confirmPass){ 

     $don = array('result' =>"fail","message"=>"Please confirm your  
     password"); 

    }  

    echo json_encode($don); 

?> 
+0

請問downvoter請注意評論。 – Oli

回答

5

變化

$don = array('result' =>"success","message"=>$posts['id']); 

$don = array('result' =>"success","message"=>$_SESSION['id']); 

$帖[ '身份證']總是空的行不插入到數據庫中。刪除該代碼。

+0

謝謝你。它正在工作:) – Theo

1

變化

$don = array('result' =>"success","message"=>$posts['id']); 

要:

$don = array('result' =>"success","message"=>mysqli_insert_id($con)); 

的問題是,你指的$posts['id']這始終是null您嘗試在這裏設置:

​​

請注意,如果$result不包含任何行,我們只能達到while。因此,mysqli_fetch_array($result)是錯誤的,並且此循環從不執行。事實上,這個循環在這個腳本中是無用的,應該被刪除。

這超出了你的問題的範圍,但你應該考慮以下幾點:

  1. 什麼的$confirmPass點?你永遠不會檢查密碼和確認通過匹配。
  2. 鑑於兩者應該相同,您爲什麼要存儲密碼和確認密碼?
  3. 不應該在SQL查詢中使用用戶提供的值而不用轉義它們,或者甚至更好,不使用預處理語句。您的SELECT查詢至少會轉義值,但您的INSERT查詢不會。這讓您打開SQL注入攻擊。
  4. sha1($user_pass)不是散列pwd存儲的好方法。改用PHP的password_hashpassword_verify函數。 See the guide
-1

您不需要以下行,因此請刪除它們,因爲您在選擇查詢之前使用它們。

//This is where I am trying to get the id 
while($row = mysqli_fetch_array($result)) {    
    $posts['id'] = $row['id']; 

} 

您可以

$don = array('result' =>"success","message"=> mysqli_insert_id($con)); 

更換

$don = array('result' =>"success","message" => $posts['id']); 

無需會議的爲好。

+0

如果您要檢查他的代碼,他在選擇查詢之前提取行。該代碼是沒用的。 – Deep