2017-04-10 24 views
-2

我正在嘗試製作註冊表單,任何人都可以檢查並告訴我這裏有什麼問題?註冊功能不能正常顯示{「error」:true,「message」:「必填字段丟失」}

我使用郵差測試它,它總是向我展示以下錯誤:

{"error":true,"message":"Required fields are missing"} 

我DbOperations.php

  //For Employee Registration 

     public function emplReg($name, $surname, $username, $user_pass, $address, $pin, $mail, $phone){ 
      $password = md5($user_pass); 
      $stmt = $this->con->prepare("INSERT INTO `emp_data` (`name`, `surname`, `username`, `password`, `address`, `pin`, `mail`, `phone`) VALUES ('?', '?', '?', '?', '?', '?', '?', '?');"); 
      $stmt->bind_Param("ssssssss",$name,$surname,$username,$user_pass,$address,$pin,$mail,$phone); 
      if ($stmt->execute()) { 
       return 1; 
      }else{ 
       return 2; 
      } 
     } 

和我emplReg.php

<?php 
require_once '../include/DbOperations.php'; 

$response = array(); 

if($_SERVER['REQUEST_METHOD']=='POST'){ 
    if(
     isset($_POST['reg_name']) and isset($_POST['reg_surname']) and isset($_POST['reg_username']) and isset($_POST['reg_password']) and isset($_POST['reg_address']) and isset($_POST['reg_pin']) and isset($_POST['reg_mail']) and isset($_POST['reg_phone']) 
     ){ 
     //operate the data further 

     $db = new DbOperations(); 

     $result = $db->emplReg($_POST['reg_name'], 
            $_POST['reg_surname'], 
            $_POST['reg_username'], 
            $_POST['reg_password'], 
            $_POST['reg_address'], 
            $_POST['reg_pin'], 
            $_POST['reg_mail'], 
            $_POST['reg_phone'] 
           ); 
     if($result == 1){ 
      $response['error'] = false; 
      $response['message'] = "User register successfully"; 
     }elseif($result == 2){ 
      $response['error'] = true; 
      $response['message'] = "Something wrong, try again"; 
     } 

    }else{ 
     $response['error'] = true; 
     $response['message'] = "Required fields are missing"; 
    } 
}else{ 
    $response['error'] = true; 
    $response['message'] = "Invalid Request"; 
} 

echo json_encode($response); 

?> 

https://www.dropbox.com/s/m0ngx8hqy6vr3t8/Untitled.png?dl=0

+0

您是否通過郵遞員發送了所有必填字段? –

+0

該錯誤消息僅在一個地方發生 - 在第二個if條件的else中。因此,合乎邏輯的結論是,不檢查它檢查的所有$ _POST值。 – CBroe

+0

@PavloZhukov是... –

回答

1

我發現三件事情你做錯了什麼......

    在郵遞員
  1. 您使用x-www-form-urlencoded而不是form-data
  2. in bind_Param change $user_pass to $password
  3. 並從所有佔位符中刪除引號'?'?
+0

謝謝!讓我試試這個一次... –