已解决将变量发布到数据库的问题。仍然需要帮助发布到正确的行。
我想要做的是将变量发布到 mysql 数据库。最大的问题是将其发布到正确的用户行。登录的用户。
下面是我的数学测验活动的片段。测验结束时会出现一个对话框。变量 right_answers 正在提交到数据库。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
userFunctions = new UserFunctions();
if(userFunctions.isUserLoggedIn(getApplicationContext())){
uestions.length){
if(qno==6){
//If all questions have been answered answer Alert Dialog appears, the user can choose to:
//Submit score
//Go to main menu without submitting score
//Retry
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mathsActivity.this);
// Submit score button
alertDialog.setPositiveButton("Submit Score", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed YES button. Write Logic Here
//Toast.makeText(getApplicationContext(), "Score Submitted",Toast.LENGTH_SHORT).show();
//startActivity(new Intent(getApplication(), MathsMenuActivity.class));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xxxxxxxxxxxx.com/script.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("right_answers", Integer.toString(right_answers)));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
});
}
来自存储在包库中的用户函数类是以下块。
用于获取登录状态。
public boolean isUserLoggedIn(Context context){
DatabaseHandler db = new DatabaseHandler(context);
int count = db.getRowCount();
if(count > 0){
// user logged in
return true;
}
return false;
}
最后,这里是负责在相应列中插入 right_answers 的 php。
include 'xxxxx/connect.php';
$right_answers = $_POST['right_answers'];
$sql=mysql_query("INSERT INTO members (maths)VALUES('" . $right_answers . "')");
帮助将不胜感激!
谢谢