【问题标题】:Can't add user to database无法将用户添加到数据库
【发布时间】:2019-12-09 12:56:02
【问题描述】:

我正在开发 android 应用程序并使用此脚本将用户添加到 MySQL 数据库问题是当我在 postman 和 android 应用程序上测试脚本时,我无法将用户添加到数据库,响应总是:

{"OKCode":0,"message":"必填字段缺失"}

这里是 php 脚本:

    <?php

      header('Content-Type: text/html; charset=utf-8'); 
      // array for JSON response
       $response = array();

       // include db connect class
       require_once __DIR__ . '/db_connect.php';

       // check for post data
       if (
            isset($_POST['student_name']) &&
            isset($_POST['id_school']) &&
            isset($_POST['u_password']) &&
            isset($_POST['id_number']) &&
            isset($_POST['licence']) &&
            isset($_POST['phone'])&&
            isset($_POST['username'])
         ) {

         $student_name  = $_POST['student_name'];
         $id_school     = $_POST['id_school'];
         $u_password    = $_POST['u_password'];
         $id_number     = $_POST['id_number'];
         $licence       = $_POST['licence'];
         $phone         = $_POST['phone'];
         $username      = $_POST['username'];


         $id ;

         // connecting to db
         $db = new DB_CONNECT();
         $con = $db->_connect();
         if (!userExists($con, $username)) {
         if (insertIntoUser($con, $username, $id_school, $u_password)) {
        # when the student insert into user add The student to Student 
       table
        # whit the last update id 
        if (insertIntoStudent($con, $id, $id_school, $student_name, $licence, $id_number, $phone)) {
            $response["OKCode"] = 1;
            $response["message"] = "done";

        } else {

            $response["OKCode"] = 0;
            $response["message"] = "fail";
        }
    } else {
        $response["OKCode"] = 0;
        $response["message"] = "fail";
    }

}else {
    $response["OKCode"] = 0;
    $response["message"] = "This account is already in users";
}
}else {
$response["OKCode"] = 0;
$response["message"] = "Required field(s) is missing";
}
echo json_encode($response, JSON_UNESCAPED_UNICODE);


/* check if username is not in the user tabel for this 
   username is the id_number of the student 
 */
function userExists($con, $username)
{
$query = "SELECT username FROM users WHERE username = ?";
if ($stmt = $con->prepare($query)) {
    $stmt->bind_param("s", $username);
    $stmt->execute();
    $stmt->store_result();
    $stmt->fetch();
    if ($stmt->num_rows == 1) {
        $stmt->close();
        return true;
    }
    $stmt->close();
 }

return false;
}

function insertIntoUser($con, $username, $id_school, $u_password)
{
$account_type = 2;
$insertQuery  = "INSERT INTO users (id_school, username, u_password, 
account_type) VALUES (?,?,?,?)";
$stmt = $con->prepare($insertQuery);
if (
    $stmt &&
    $stmt->bind_param("isii", $id_school, $username, $u_password, 
 $account_type) &&
    $stmt->execute()
) {
     global $id;
     $id = $stmt->insert_id;
     $stmt->close();
    return true;
}
$stmt->close();
return false;
}

 function insertIntoStudent($con, $id, $id_school, $student_name, $licence, 
  $id_number, $phone)
 {
  $insertQuery  = "INSERT INTO student(id, id_school, student_name, licence, 
id_number, phone) VALUES (?,?,?,?,?,?)";
$stmt = $con->prepare($insertQuery);
if (
    $stmt && $stmt->bind_param("iissii", $id, $id_school, $student_name, 
  $licence, $id_number, $phone) &&
    $stmt->execute()
) {
    return true;
    $stmt->close();
}
$stmt->close();
return false;
}

the php script

邮递员的截图

这是java代码`
私人无效 addNewStudent() {

    StringRequest request = new StringRequest(Request.Method.POST, 
     ADD_NEW_STUDENT_URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            pBar.setVisibility(View.GONE);
            btnAddStudent.setVisibility(View.VISIBLE);
            isSelected = false;
            radioGroup.clearCheck();

            Log.d("MyTag", response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }){
        @Override
        protected Map<String, String> getParams() {
            Map<String,String> stringMap = new HashMap<>();
            stringMap.put("student_name",studentName);
            stringMap.put("id_school",idSchool);
            stringMap.put("u_password",password);
            stringMap.put("id_number",idNumber);
            stringMap.put("licence",type);
            stringMap.put("phone",phoneNumber);
            stringMap.put("username","hp");

            return stringMap;
        }
    };

    AppController.getInstance().addToRequestQueue(request,ADD_STUDENT_TAG);

}`

【问题讨论】:

  • PHP 代码应该在此处,而不是在链接页面上。输出你的POST,看看你实际拥有什么。 isset 也可以取多个值,你不应该有多个这样的调用。
  • @user3783243 像这样 (isset($_POST['student_name'],$_POST['id_school'],$_POST['id_school'],$_POST['u_password'],$_POST[ 'id_number'],$_POST['licence'],$_POST['phone'],$_POST['username'])))
  • 既然您在邮递员内部也有同样的问题,那么问题就出在您的 PHP 代码中。你能用 var_dump($_POST) 看看里面是什么吗?
  • @catcon array(0) { } {"success":0,"message":"Required field(s) is missing"} 并且当将 $_POST 更改为 $_GET 它在邮递员中工作但在android中没有
  • 您正在将值添加到查询参数中,但在 POST 请求中,您必须将它们添加到正文中。

标签: java php android api android-volley


【解决方案1】:

响应总是:{"OKCode":0,"message":"必填字段缺失"}

if (
    isset($_POST['student_name']) &&
    isset($_POST['id_school']) &&
    isset($_POST['u_password']) &&
    isset($_POST['id_number']) &&
    isset($_POST['licence']) &&
    isset($_POST['phone'])&&
    isset($_POST['username'])
) {
    # (..)
}else {
    $response["OKCode"] = 0;
    $response["message"] = "Required field(s) is missing"; # <----
}

因此您的一个或多个 $_POST 字段不存在。试试print_r($_POST) 看看 POST 中的内容..

【讨论】:

  • 这里我得到了 Array ( ) {"OKCode":0,"message":"必填字段丢失"}
  • 所以这意味着似乎没有发布任何内容。看着你的邮递员截图,这很奇怪。 print_r($_GET) 中有什么内容?请注意,您还可以使用$_REQUEST 让您的应用同时使用 GET 和 POST
  • 当我将 $_POST 更改为 $_GET 或 $_REQUEST 时,它可以在使用 Android 异步 Http 客户端库的 android 应用程序中工作,但为什么在使用 $_POST 时它不起作用
猜你喜欢
  • 2021-09-22
  • 1970-01-01
  • 1970-01-01
  • 2020-10-02
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
  • 2017-12-28
  • 1970-01-01
相关资源
最近更新 更多