在SQL数据库中存储android复选框状态

在SQL数据库中存储android复选框状态

问题描述:

I am creating an app that will allow users to register with their names, ages, etc. I am storing all of this information in a User class, which looks like this.

public class User {

String name, username, password, phoneNumber;
int age;

//constructor
public User(String _name, String _username, String _password, String _phoneNumber, int _age){
    name = _name;
    username = _username;
    password = _password;
    phoneNumber = _phoneNumber;
    age = _age;
}
}

I am sending that data to a PHP script to store in an SQL database. That code is as follows:

protected Void doInBackground(Void... params) {
            ArrayList<NameValuePair> dataToSend = new ArrayList<>();
            dataToSend.add(new BasicNameValuePair("name", user.name));
            dataToSend.add(new BasicNameValuePair("age", user.age + ""));
            dataToSend.add(new BasicNameValuePair("username", user.username));
            dataToSend.add(new BasicNameValuePair("password", user.password));
            dataToSend.add(new BasicNameValuePair("phoneNumber", user.phoneNumber));

        HttpParams httpRequestParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);

        HttpClient client = new DefaultHttpClient(httpRequestParams);
        HttpPost post = new HttpPost(SERVER_ADDRESS + "Register.php");

        try{
            post.setEntity(new UrlEncodedFormEntity(dataToSend));
            client.execute(post);
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

Now I want to create about 10 checkboxes whose state (checked vs unchecked) I can store. I know that I could create a bunch of boolean values as part of the User class, but that seems cumbersome and bad practice. My question is, therefore, how best to store the CheckBox data in the SQL database?

For reference, the PHP script is as follows:

<?php
$con = mysqli_connect("localhost", "root", "", "is_clients");

$name = $_POST["name"];
$age = $_POST["age"];
$username = $_POST["username"];
$password = $_POST["password"];
$phoneNumber = $_POST["phoneNumber"];

$statement = mysqli_prepare($con, "INSERT INTO user (name, age, username, password, phoneNumber) VALUES (?, ?, ?, ?, ?) "); 
mysqli_stmt_bind_param($statement, "sisss", $name, $age, $username, $password, $phoneNumber);
mysqli_stmt_execute($statement);

mysqli_stmt_close($statement);

mysqli_close($con);

?>

You could pass an array to PHP, each element in the array can be a check box value. In PHP you can do that by using a variable with brackets like checkBoxesArr[] so you can use the same variable, and every occurrence would be another element in the array.

So, in Java (checkBoxXXXValue can be a binary or boolean):

dataToSend.add(new BasicNameValuePair("checkBoxArr[]", checkBoxOneValue));
dataToSend.add(new BasicNameValuePair("checkBoxArr[]", checkBoxTwoValue));
dataToSend.add(new BasicNameValuePair("checkBoxArr[]", checkBoxThreeValue));

PHP:

$checkBoxArr = $_POST['checkBoxArr'];
print_r($checkBoxArr);

Is that what you wanted?