我正在尝试从 android 应用程序向 mysql 数据库发送一些数据,数据是字符串形式(它是用户完成的测验的结果),我不确定问题是什么。
这是我的 HTTP Post 的 android 代码:
// http post
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://ast.ncl-coll.ac.uk/~cmaughan/firesafety/app_results.php");
try {
//Link to quiz string results
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("StaffID","StaffID"));
nameValuePairs.add(new BasicNameValuePair("Forename","Forename"));
nameValuePairs.add(new BasicNameValuePair("Surname", "Surname"));
nameValuePairs.add(new BasicNameValuePair("Score", "Score"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
Toast.makeText(getApplicationContext(),
这是我的php:
<?php
include('./inc/connection.inc.php');
connect();
//retrieve the data
$StaffID = $_POST['StaffID'];
$Forename = $_POST['Forename'];
$Surname = $_POST['Surname'];
$Score = $_POST['Score'];
//Insert into the database
$sql = "INSERT INTO tblQuiz (staffid, forename, surname, score) VALUES ('$StaffID', '$Forename', '$Surname', '$Score')";
$result = mysql_query($sql) or die("Could not run query");
// Close connection to the database
mysql_close();
// Echo success
echo "Upload Successful";
?>
任何帮助将不胜感激,我认为数组是错误的,因为我正在尝试使用 HTTP Post 方法发送字符串
注意: *测验的结果将进入 DDMS 中的 mysqllite 数据库,我只想将这些结果上传到 php/mysql 数据库。*