Php 安卓:Can';不要将数据发送到MYSQL。导致崩溃
我正在尝试构建一个android应用程序,它通过用户输入填充一个数据库表,但该应用程序不会填充所有列,有些列会故意为空。无论何时我试图提交它都会崩溃我做错了什么 我尝试过一个接一个地消除java类和php中的变量,但没有成功 类别代码Php 安卓:Can';不要将数据发送到MYSQL。导致崩溃,php,android,mysql,Php,Android,Mysql,我正在尝试构建一个android应用程序,它通过用户输入填充一个数据库表,但该应用程序不会填充所有列,有些列会故意为空。无论何时我试图提交它都会崩溃我做错了什么 我尝试过一个接一个地消除java类和php中的变量,但没有成功 类别代码 // Progress Dialog private ProgressDialog pDialog; JSONParser jsonParser = new JSONParser(); EditText start; EditText end; EditText
// Progress Dialog
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText start;
EditText end;
EditText reasonfield;
Spinner spinner;
// url to create new product
private static String url_create_contact = "http://10.0.3.2/sunshie-ems/create_contact.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.leave);
// Edit Text
start = (EditText) findViewById(R.id.start);
end = (EditText) findViewById(R.id.end);
reasonfield = (EditText) findViewById(R.id.reasonfield);
Spinner mySpinner=(Spinner) findViewById(R.id.spinner);
// Create button
Button submit = (Button) findViewById(R.id.submit);
// button click event
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// creating new product in background thread
new CreateNewProduct().execute();
}
});
}
/**
* Background Async Task to Create new product
* */
class CreateNewProduct extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Leave.this);
pDialog.setMessage("Creating Leave..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
String name = start.getText().toString();
String number = end.getText().toString();
String about = reasonfield.getText().toString();
String leave = spinner.getSelectedItem().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("number", number));
params.add(new BasicNameValuePair("about", about));
params.add(new BasicNameValuePair("leave", leave));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_contact,
"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 product
Toast.makeText(getApplicationContext(), "Successful", Toast.LENGTH_LONG).show();
//Intent i = new Intent(getApplicationContext(), MainActivity.class);
//startActivity(i);
// closing this screen
finish();
} else {
Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_LONG).show();
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
}
PHP“创建联系人”
您正在将微调器设置为
mySpinner
,并从spinner
获取值,这会导致NullPointerException
,因此请更改
Spinner mySpinner=(Spinner) findViewById(R.id.spinner)
到
同时从查询中删除额外报价,即更改
$result = mysql_query("INSERT INTO leavelist(leavetype, date, start, end, reason) VALUES(''$leavetype', '$date', '$start', '$end', '$reason')");
到
由于某些原因,PHP代码不会出现D:将您的json数据上传到Hered,您是指我的json代码吗?我确信这不会导致错误,因为该应用程序还有一个登录功能,它可以很好地登录you@insert_name_here你说不工作是什么意思,更新
'$leavetype'
=>'$leavetype'
中的logcat exceptionExtra quote,我想为我在尝试显示PHP代码时出现的额外quote道歉,因为它不久前没有显示。在检查我的代码后,它没有额外的报价
Spinner mySpinner=(Spinner) findViewById(R.id.spinner)
spinner=(Spinner) findViewById(R.id.spinner)
$result = mysql_query("INSERT INTO leavelist(leavetype, date, start, end, reason) VALUES(''$leavetype', '$date', '$start', '$end', '$reason')");
$result = mysql_query("INSERT INTO leavelist(leavetype, date, start, end, reason) VALUES('$leavetype', '$date', '$start', '$end', '$reason')");