使用php在mysql数据库中插入记录
我在android上做一个调查项目,关于登录(这部分有效)和插入数据这部分无效的是问卷调查,当调查人员登录到应用程序时,有一个调查要做,在这个项目中,我从5个问题开始,回答是或否作为原型,他必须回答这些问题,并且在单击save按钮时,必须将这些问题添加到mysql数据库中。但这不起作用(日志如下) 下面是课堂调查问卷。java:使用php在mysql数据库中插入记录,php,android,mysql,database,logcat,Php,Android,Mysql,Database,Logcat,我在android上做一个调查项目,关于登录(这部分有效)和插入数据这部分无效的是问卷调查,当调查人员登录到应用程序时,有一个调查要做,在这个项目中,我从5个问题开始,回答是或否作为原型,他必须回答这些问题,并且在单击save按钮时,必须将这些问题添加到mysql数据库中。但这不起作用(日志如下) 下面是课堂调查问卷。java: public class Questionnaire extends Activity implements LocationListener { // Progres
public class Questionnaire extends Activity implements LocationListener {
// Progress Dialog
private ProgressDialog pDialog;
//latitude et longitude
private LocationManager lm;
Location location;
double longitude = 0;
double latitude = 0;
private double altitude;
private float accuracy;
int i;
JSONParser jsonParser = new JSONParser();
Button enregistrer;
EditText zone;
RadioGroup mRadioGroup1, mRadioGroup2, mRadioGroup3, mRadioGroup4, mRadioGroup5;
RadioButton b1, b2, b3, b4, b5;
// url to create new product
private static String url_insertion = "http://10.0.2.2/android/insertion.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.questions);
Bundle params = getIntent().getExtras();
i = params.getInt("compteur");
i++;
// Edit zone text
EditText zone1 = (EditText) findViewById(R.appreciation.zone);
// edit latitude longitude
latitude = location.getLatitude();
longitude = location.getLongitude();
// edit radiogroup
mRadioGroup1 = (RadioGroup) findViewById(R.appreciation.rep1);
mRadioGroup2 = (RadioGroup) findViewById(R.appreciation.rep2);
mRadioGroup3 = (RadioGroup) findViewById(R.appreciation.rep3);
mRadioGroup4 = (RadioGroup) findViewById(R.appreciation.rep4);
mRadioGroup5 = (RadioGroup) findViewById(R.appreciation.rep5);
//edit radio button
b1 = (RadioButton) findViewById(mRadioGroup1.getCheckedRadioButtonId());
b2 = (RadioButton) findViewById(mRadioGroup2.getCheckedRadioButtonId());
b3 = (RadioButton) findViewById(mRadioGroup3.getCheckedRadioButtonId());
b4 = (RadioButton) findViewById(mRadioGroup4.getCheckedRadioButtonId());
b5 = (RadioButton) findViewById(mRadioGroup5.getCheckedRadioButtonId());
// Create button
Button enregistrer = (Button) findViewById(R.appreciation.enregistrer);
// button click event
enregistrer.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
new InsertAppreciation().execute();
}
});
}
class InsertAppreciation extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Questionnaire.this);
pDialog.setMessage("Creating Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating appreciation
* */
protected String doInBackground(String... args) {
String zone1 = zone.getText().toString();
// String latitude = String.valueOf(latitude).toString();
// String longitude = String.valueOf(longitude).toString();
String rep1 = b1.getText().toString();
String rep2 = b2.getText().toString();
String rep3 = b3.getText().toString();
String rep4 = b4.getText().toString();
String rep5 = b5.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("zone", zone1));
// params.add(new BasicNameValuePair("latitude", latitude));
// params.add(new BasicNameValuePair("longitude", longitude));
params.add(new BasicNameValuePair("rep1", rep1));
params.add(new BasicNameValuePair("rep2", rep2));
params.add(new BasicNameValuePair("rep3", rep3));
params.add(new BasicNameValuePair("rep4", rep4));
params.add(new BasicNameValuePair("rep5", rep5));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_insertion,
"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 appreciation
Toast.makeText(getBaseContext(), "Successful insertion", Toast.LENGTH_SHORT).show();
if (i < 5) {
Intent intent1 = new Intent(Questionnaire.this, Questionnaire.class);
intent1.putExtra("compteur", i);
startActivity(intent1);
} else {
Intent intent2 = new Intent(Questionnaire.this, Endsurvey.class);
startActivity(intent2);
}
} else {
// failed to create appreciation
Toast.makeText(getBaseContext(), "failed insertion", Toast.LENGTH_SHORT).show();
}
} 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文件:
<?php
//turn off error reporting
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
//Create fields for the database
//server, username, password, database
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbdb = "survey";
//connect to mySQL
$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die("connection error");
//Select the database
mysql_select_db($dbdb)or die("database selection error");
//Retrieve the login details via POST
$zone = $_POST['zone'];
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
$rep1 = $_POST['rep1'];
$rep2 = $_POST['rep2'];
$rep3 = $_POST['rep3'];
$rep4 = $_POST['rep4'];
$rep5 = $_POST['rep5'];
// mysql inserting a new row
$result = mysql_query("INSERT INTO appreciation(id, zone, latitude, longitude, rep1, rep2, rep3, rep4, rep5) VALUES('', '$zone1', '', '', '$rep1', '$rep2', '$rep3', '$rep4', '$rep5' )");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
//close the connection
mysql_close();
}
?>
首先,正如其他人在评论中所说,请查看PDO,因为您的数据库交互非常不安全
您提供的异常跟踪表明,问卷调查
活动的onCreate
方法中发生了未捕获(开始使用)异常。我预计(请通过调试进行检查)当您第二次查看意图包中的compteur
时(您将从inventory
转到inventory
活动),会抛出错误
我可以看到,当您启动intent时,您正在将一个对象放入bundle中,但是在处理它之前,您没有检查它是否为null
Unable to open stack trace file '/data/anr/traces.txt': Permission denied
您是否在清单xml文件中添加了从sd卡读取的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
第1步:不要使用mysql\u query
,而是花很短的时间来加快mysqli
或PDO的速度。您发布的这段代码充满了SQL注入漏洞,一次恶意查询就可以摧毁整个应用程序。这是PHP问题还是Java问题?你已经发布了Java堆栈跟踪和一些随机(和可怕的)PHP代码,但没有就问题的具体性质提供任何指导。谢谢你的建议,我不知道我是android新手,我想这是Java问题,但我也发布了PHP文件,谁知道呢!,我贴了日志,但我不知道到底是什么问题?感谢您的帮助只是一个建议,如果您想关闭错误报告,请这样做:error\u reporting(0)
您使用哪个版本的android?(因此,有人可以在ActivityThread.java:2496查看正在发生的事情)
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>