注册功能无法正常显示{"错误":true,"消息":"缺少必填字段"}

时间:2017-04-10 10:45:54

标签: php mysql database

我正在尝试登记表格,有人可以查看并告诉我这里有什么问题吗?

我正在使用Postman对其进行测试,并且它总是向我显示以下错误:

{"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

1 个答案:

答案 0 :(得分:1)

我发现你做错了三件事......

  1. 在邮递员中,您使用x-www-form-urlencoded代替form-data
  2. bind_aram中的
  3. $user_pass更改为$password
  4. 并从所有占位符'?' ?中删除引号。