Posting a variable to the database has been solved. Still need help posting to the correct row.
What i am trying to do is post a variable to the mysql database. The big problem is getting it to be posted to the correct users row. The user that is logged in.
Below is snippet my quiz activity for maths. At the end of the quiz a dialog box appears. the variable right_answers is being submitted to the database.
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
}
}
});
}
From user function class which is stored in a package library is the following block.
It is used to to get the login status.
public boolean isUserLoggedIn(Context context){
DatabaseHandler db = new DatabaseHandler(context);
int count = db.getRowCount();
if(count > 0){
// user logged in
return true;
}
return false;
}
Finally here is the php responsible for inserting the right_answers in the appropriate column.
include 'xxxxx/connect.php';
$right_answers = $_POST['right_answers'];
$sql=mysql_query("INSERT INTO members (maths)VALUES('" . $right_answers . "')");
Help would be appreciated!
Thanks