douzhuanqian8244 2015-07-07 15:39 采纳率: 100%
浏览 84
已采纳

在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);

?>
  • 写回答

1条回答 默认 最新

  • dongyuan9892 2015-07-07 15:59
    关注

    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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP