简体   繁体   English

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

[英]Storing android checkbox state in SQL database

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. 我正在创建一个应用程序,该应用程序将允许用户使用其姓名,年龄等进行注册。我将所有这些信息存储在如下所示的User类中。

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. 我将数据发送到PHP脚本以存储在SQL数据库中。 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. 现在,我想创建大约10个可以存储其状态(选中与未选中)的复选框。 I know that I could create a bunch of boolean values as part of the User class, but that seems cumbersome and bad practice. 我知道我可以在User类中创建一堆布尔值,但这似乎很麻烦且不明智。 My question is, therefore, how best to store the CheckBox data in the SQL database? 因此,我的问题是,如何最好地将CheckBox数据存储在SQL数据库中?

For reference, the PHP script is as follows: 供参考,PHP脚本如下:

<?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. 您可以将数组传递给PHP,该数组中的每个元素都可以是一个复选框值。 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. 在PHP中,您可以通过使用带有方括号的变量(例如checkBoxesArr [])来执行此操作,以便可以使用相同的变量,并且每次出现都将是数组中的另一个元素。

So, in Java (checkBoxXXXValue can be a binary or boolean): 因此,在Java中(checkBoxXXXValue可以为二进制或布尔值):

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

PHP: PHP:

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

Is that what you wanted? 那是你想要的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM