0
我正在處理一個應用程序,該應用程序通過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();
}
}
}
您的數據庫定義與您的插入語句不符。你有一個'empid',但不是另一個,'userid'和其他可能是相同的。 –
此外,它將幫助顯示數據庫表的真實定義,包括類型和所有內容。具體而言,是否有任何字段是「允許的空值」以及「request_id」是否是自動增量等等。 –
謝謝Paul! – tvdog