试图在android中的mysql数据库中插入checkbox的值

时间:2018-04-09 06:50:27

标签: java php android

我是卡文。我正在创建一个检查列表...在那我试图使用复选框验证列表...之后,但点击一个按钮我的复选框值应该插入在mysql数据库...但我尝试了大多数技术,我不能得到价值。我只想获取复选框的值,并希望插入mysql数据库..任何人帮助我...我是android和java的初学者,所以解释我很简单...

public class MainActivity extends AppCompatActivity {
CheckBox box;
Button btn_test;


private static final String REGISTER_URL="http://192.168.0.106/test/checkin.php";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainactivity);


    box = (CheckBox) findViewById(R.id.checkBox);
    btn_test = (Button) findViewById(R.id.button);
    btn_test.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            registerUser();
        }
    });
}

private void registerUser() {
   // box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    //public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
      //  if(isChecked){
        //    String date = box.toString().trim().toLowerCase();
          //  register(date); } } }  );
    Boolean box1 = ((CheckBox)box).isChecked();
    if (box1) {
        String date = "yes";
    } else {
        String date = "No";
    }
    String date = box1.toString();
    register(date);
}

private void register(String date){
    String urlSuffix = "?date=" + date;
    class RegisterUser extends AsyncTask<String, Void, String> {

        ProgressDialog loading;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(MainActivity.this, "Please Wait", null, true, true);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            Toast.makeText(getApplicationContext(),"Registered", Toast.LENGTH_SHORT).show();
        }

        @Override
        protected String doInBackground(String... params) {
            String s = params[0];
            BufferedReader bufferReader=null;
            try {
                URL url=new URL(REGISTER_URL+s);
                HttpURLConnection con=(HttpURLConnection)url.openConnection();
                bufferReader=new BufferedReader(new InputStreamReader(con.getInputStream()));
                String result;
                result=bufferReader.readLine();
                return  result;

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

    }
    RegisterUser ur=new RegisterUser();
    ur.execute(urlSuffix);
}
}

我的php程序,

<?php
require "conn.php";

$date = $_GET['date'];


    $sql = "INSERT INTO check (date) VALUES('$date')";

    if($conn->query($sql)){

        echo 'successfully registered';

}
    else{

        echo 'oops! Please try again!';

    }


        $conn->close();

    }           
    ?>

0 个答案:

没有答案