无法将数据存储到数据库中

时间:2012-06-13 19:54:09

标签: java php android mysql

我正在开发一个应用程序,它通过PHP API将用户输入存储到数据库表中,但是我收到了从PHP代码到LogCat的错误消息。任何建议都将不胜感激。

D/Create Response(284): {"message":"Required field(s) is missing","success":0}

PHP API

<?php
// array for JSON response
$response = array();

// check for the fields
if (isset($_POST['title']) && isset($_POST['request_date']) && isset($_POST['reqEndDate']) && isset($_POST['reason']) && isset($_POST['requestor']) && isset($_POST['status']) && isset($_POST['submitDate']) && isset($_POST['explanation']) && isset($_POST['hours']) && isset($_POST['id'])) {

    $title = $_POST["request_title"];
    $date = $_POST["request_date"];
    $eDate = $_POST["reqEndDate"];
    $reason = $_POST["reason"];
    $requestor = $_POST["requestor"];
    $status = $_POST["status"]; 
    $dateSubmitted = $_POST["submitDate"];
    $explanation = $_POST["explanation"];
    $numhours = $_POST["hours"];
    $id = $_POST['id'];


    // mysql inserting a new row
    $result = mysql_query("INSERT INTO requests(request_title, request_date, reqEndDate, reason, requestor, status, submitDate, explanation, hours, empid) 
                            VALUES('$title', '$date', '$eDate', '$reason', '$requestor', '$status', '$dateSubmitted', '$explanation', '$numhours', '$id')");


?>

JAVA CLASS

    // url to the PHP API to create new request
    private static String url_create_request = "http://mywebsite.com/create_request.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_request);

        // Edit Text
        inputTitle = (EditText) findViewById(R.id.inputTitle);
        inputSdate = (EditText) findViewById(R.id.inputSdate);
        inputEdate = (EditText) findViewById(R.id.inputEdate);
        inputHours = (EditText) findViewById(R.id.inputHours);
        inputReason = (EditText) findViewById(R.id.inputReason);
        inputExp = (EditText) findViewById(R.id.inputExp);

        // Create button
        Button btnCreateRequest = (Button) findViewById(R.id.btnCreateRequest);

        // button click event
        btnCreateRequest.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // creating new product in background thread
                new CreateNewRequest().execute();
            }
        });
    }


    class CreateNewRequest extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(NewRequestActivity.this);
            pDialog.setMessage("Creating Request..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        /**
         * Creating Request Required Fields
         * */
        protected String doInBackground(String... args) {
            String title = inputTitle.getText().toString();
            String date = inputSdate.getText().toString();
            String eDate = inputEdate.getText().toString();
            String hours = inputHours.getText().toString();
            String reason = inputReason.getText().toString();
            String explanation = inputExp.getText().toString();

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("request_title", title));
            params.add(new BasicNameValuePair("request_date", date));
            params.add(new BasicNameValuePair("reqEndDate", eDate));
            params.add(new BasicNameValuePair("hours", hours));
            params.add(new BasicNameValuePair("reason", reason));
            params.add(new BasicNameValuePair("explanation", explanation));

            // getting JSON Object
            // Note that create request url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_create_request,
                    "POST", params);

            // check log cat fro response
            Log.d("Create Response", json.toString());

            // check for success tag
            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // successfully created request
                    Intent i = new Intent(getApplicationContext(), AllRequestsActivity.class);
                    startActivity(i);

                    // closing this screen
                    finish();
                } else {
                    // failed to create request
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(String file_url) {
            // dismiss the dialog once done
            pDialog.dismiss();
        }

    }
}

1 个答案:

答案 0 :(得分:2)

只计算您的数据库列:似乎有12个。你只是插入到11中,并且在消除过程之后,看起来你正在遗漏“活动”,除非它有一个默认值或者可以为null,否则会抛出“必填字段” “尝试插入数据库时​​遇到的错误。