必填字段缺少结果 [英] Required Field(s) missing result

查看:414
本文介绍了必填字段缺少结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想简短地回答这个问题.我的php代码有什么问题,它会一直输出0或缺少必填字段.这是代码

I'll just keep this question short. What is wrong with my php code, it keeps outputting 0 or Required Field(s) is missing. Here's the code

<?php

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['id']) && isset($_POST['status_id'])) {

    $id = $_POST['id'];
    $status_id = $_POST['status_id'];

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

    // connecting to db
    $db = new DB_CONNECT();

    // mysql update row with matched pid
    $result = mysql_query("UPDATE pims_liip_pallet_purchase_order SET status = '$status_id' WHERE id = $id");


    // check if row inserted or not
    if ($result) {
        // successfully updated
        $response["success"] = 1;
        $response["message"] = "Product successfully updated.";

        // echoing JSON response
        echo json_encode($response);
    } else {

    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>

这是我的应用程序中的帖子数据

Here is the post data in my app

protected String doInBackground(String... args) {

            // TODO Auto-generated method stub
             // Check for success tag
           int success;
           String status_id = statusID.getText().toString();

           try {
               // Building Parameters
               List<NameValuePair> params = new ArrayList<NameValuePair>();
               params.add(new BasicNameValuePair("status_id", status_id));

               Log.d("request!", "starting");

               //Posting user data to script 
               JSONObject json = jsonParser.makeHttpRequest(
                    UPDATE_COMMENTS_URL, "POST", params);

               // full json response
               Log.d("Post Update", json.toString());

               // json success element
               success = json.getInt(TAG_SUCCESS);
               if (success == 1) {
                Log.d("Updated!", json.toString());    
                finish();
                return json.getString(TAG_MESSAGE);
               }else{
                Log.d("Update Failure!", json.getString(TAG_MESSAGE));
                return json.getString(TAG_MESSAGE);

               }
           } catch (JSONException e) {
               e.printStackTrace();
           }

           return null;

        }

任何答案都会非常荣幸:D谢谢!

Any answers will be very much honored :D Thanks!

推荐答案

您的错误说明了一切.由于您到达} else { ... }位,因此意味着isset($_POST['id']) && isset($_POST['status_id'])为假.

Your error says it all. Since you get to the } else { ... } bit, it means isset($_POST['id']) && isset($_POST['status_id']) is false.

换句话说,您的表单是:

In other words, your form is either:

  • 不使用POST,而是使用GET.在这种情况下,将method="post"添加到您的<form>标记中. (实际上,POST是默认行为,因此,在这种情况下,您可能必须从表单标签中删除或更改method="GET")
  • 和/或您的表单不包含带有name="id"和/或name="status_id"
  • 的输入字段
  • not using POST, but GET. In that case add method="post" to your <form> tag. (actually, POST is default behaviour, so if this is the case, you probably have to remove or change method="GET" from the form tag)
  • and/or your form does not contain input fields with name="id" and/or name="status_id"

更新后的问题将添加Android代码.因此,此更新:

我怀疑jsonParser.makeHttpRequest是否实际发布了一个表单编码的json字符串.则更有可能只是将json数据发布到Web服务器. PHP的$ _POST不会自动填充此数据,因为它仅处理表单编码的数据.

I doubt that jsonParser.makeHttpRequest actually posts a form encoded json string. It more then likely will just POST json data to the webserver. PHP's $_POST will not automatically be filled with this data, since it only handles form encoded data.

您可能需要从stdIn读取此数据.

You probably need to read this data from stdIn.

尝试:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $rawPostData = file_get_contents("php://input");
    $postData = (array)json_decode($rawPostData);
}

然后使用$ postData,否则将使用$ _POST

And then use $postData where you otherwise would use $_POST

这篇关于必填字段缺少结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆