0

In my application, while registering, user can choose the role of Student or Shop owner by using the radio button(RadioGroup)

After they choose it and fill up the register form, all data including the role should be store into mysql.

Unfortunately, only name, email, username and password are stored successfully but role cannot be stored.

Here is my java file:

package com.androidhive.dashboard;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import androidhive.dashboard.R;

public class Register extends Activity {

JSONParser jsonParser = new JSONParser();
EditText inputName;
EditText inputUsername;
EditText inputEmail;
EditText inputPassword;
RadioGroup rgroup;
Button button3;
int success = 0;
String role = "";

private static String url_register_user = "http://192.168.1.5/database/add_user.php";
private static final String TAG_SUCCESS = "success";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register_layout);

    inputName = (EditText) findViewById(R.id.nameTextBox);
    inputUsername = (EditText) findViewById(R.id.usernameTextBox);
    inputEmail = (EditText) findViewById(R.id.emailTextBox);
    inputPassword = (EditText) findViewById(R.id.passwordTextBox);

    Button button3 = (Button) findViewById(R.id.regSubmitButton);

    final RadioGroup rgroup = (RadioGroup)findViewById(R.id.roleRgroup);
    final RadioButton student = (RadioButton)findViewById(R.id.studButton);
    final RadioButton shopowner = (RadioButton)findViewById(R.id.shopownerButton);

    button3.setOnClickListener(new View.OnClickListener() {         
        public void onClick(View view) {

            String name = inputName.getText().toString();
            String username = inputUsername.getText().toString();
            String email = inputEmail.getText().toString();
            String password = inputPassword.getText().toString();

            if (rgroup.getCheckedRadioButtonId() == student.getId())
            {
                 role = "Student";
            }

            else if(rgroup.getCheckedRadioButtonId() == shopowner.getId())
            {
                 role = "ShopOwner";
            }

            if (name.contentEquals("")||username.contentEquals("")||email.contentEquals("")||password.contentEquals(""))
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(Register.this);

                builder.setMessage(R.string.nullAlert)
                   .setTitle(R.string.alertTitle);

                builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {

                   }
               });

                AlertDialog dialog = builder.show();
            }
            else
            {
                new RegisterNewUser().execute();
            }           
        }
    });
}

   class RegisterNewUser extends AsyncTask<String, String, String>{
   protected String doInBackground(String... args) {
       String name = inputName.getText().toString();
       String username = inputUsername.getText().toString();
       String email = inputEmail.getText().toString();
       String password = inputPassword.getText().toString();

       List<NameValuePair> params = new ArrayList<NameValuePair>();
       params.add(new BasicNameValuePair("name", name));
       params.add(new BasicNameValuePair("username", username));
       params.add(new BasicNameValuePair("email", email));
       params.add(new BasicNameValuePair("password", password));
       params.add(new BasicNameValuePair("role", role));

       JSONObject json = jsonParser.makeHttpRequest(url_register_user,
                           "GET", params);

       Log.d("Send Notification", json.toString());

       try
           {
                   int success = json.getInt(TAG_SUCCESS);

                   if (success == 1)
                   {

                       if   (role.contentEquals("Student"))
                        {
                            Intent i = new Intent(getApplicationContext(), StudMain.class);
                            startActivity(i);
                            finish();
                        }
                        else if ((role.contentEquals("Shop Owner")))
                        {
                            Intent i = new Intent(getApplicationContext(), ShopMain.class);
                            startActivity(i);
                            finish();
                        }
                        else
                        {
                            AlertDialog.Builder builder = new AlertDialog.Builder(Register.this);

                            builder.setMessage(R.string.roleNullAlert)
                               .setTitle(R.string.alertTitle);

                            builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                               public void onClick(DialogInterface dialog, int id) {

                               }
                           });

                            AlertDialog dialog = builder.show();
                        }

                   }

                   else
                   {

                   }
           }

            catch (Exception e)
            {
                  e.printStackTrace();
            }
            return null;
           }
   }
}

and the php file:

<?php

// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// check for required fields
if (isset($_GET['name']) && isset($_GET['username']) && isset($_GET['email']) && isset($_GET['password']) && isset($_GET['role'])) {

$name = $_GET['name'];
$username = $_GET['username'];
$email = $_GET['email'];
$password = $_GET['password'];
$role = $_GET['role'];

// mysql inserting a new row
$result = mysql_query("INSERT INTO register(name, username, email, password) VALUES('$name', '$username', '$email', '$password', '$role')");

// check if row inserted or not
if ($result) {
    // successfully inserted into database
    $response["success"] = 1;
    $response["message"] = "You are successfully registered to MEMS.";

    // 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);
}
?>

besides these two files, is it any possible factor that will affect the function of RadioGroup?

Thanks for answering.. =)

4

1 に答える 1

1

Your mysql insert query is missing "role" and that's why you cant save it.

$result = mysql_query("INSERT INTO register(name, username, email, password) VALUES('$name', '$username', '$email', '$password', '$role')");

Change it to

$result = mysql_query("INSERT INTO register(name, username, email, password,role) VALUES('$name', '$username', '$email', '$password', '$role')");
于 2012-12-01T10:38:26.980 に答える