Hi, I am programming an app and facing a problem now. I want to Update my Database over the GUI from my App via PHP. My Problem is that the app always sends the value of the checkbox even if it is unchecked. I will post my Script below.

update.php

<?php

if($_SERVER['REQUEST_METHOD']=='POST'){

    $id = $_GET['id'];
    $name= $_POST['Name'];
    $surname = $_POST['Surname'];
    $blood_typeA = _POST['Blood_TypeA'];
    $blood_typeB = _POST['Blood_TypeB'];
    $blood_type0 = _POST['Blood_Type0'];
    $blood_typeAB = _POST['Blood_TypeAB'];

    require_once('dbConnect.php');

    $sql = "UPDATE T_Student SET Name = '$name', Surname ='$surname', Blood_Type = '$blood_typeA,$blood_typeB,$blood_type0,$blood_typeAB' WHERE ID = $id";


    if(mysqli_query($con,$sql)or die('Error in query: $sql' . mysqli_error($con))){
        echo 'Student updatet';
    }else{
        echo 'Error try again';
    }


    mysqli_close($con);
}
?>

My java code

private void updateStudent(){

        final String Name = etName.getText().toString().trim();
        final String Surname = etSurname.getText().toString().trim();
        final String BloodTypeA = checkBoxBloodA.getText().toString().trim();
        final String BloodTypeB = checkBoxBloodB.getText().toString().trim();
        final String BloodType0 = checkBoxBlood0.getText().toString().trim();
        final String BloodTypeAB = checkBoxBloodAB.getText().toString().trim();



        class UpdateStudent extends AsyncTask<Void,Void,String>{
            ProgressDialog loading;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(getActivity(),"Updating...","Wait...",false,false);

            }
            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                Toast.makeText(getActivity(),s,Toast.LENGTH_LONG).show();
            }

            @Override
            protected String doInBackground(Void... params) {
                HashMap<String,String> hashMap = new HashMap<>();
                hashMap.put("Protokoll",Protkoll);
                hashMap.put("Name",Name);
                hashMap.put("Surname",Surname);
                hashMap.put("Blood_TypeA",BloodTypeA);
                hashMap.put("Blood_TypeB",BloodTypeB);
                hashMap.put("Blood_Type0",BloodType0);
                hashMap.put("Blood_TypeAB",BloodTypeAB);



                Connection con = new Connection();

                String request = con.sendPostRequest("MY ADRESS.php?id=" + ID_Student, hashMap);

                return request;
            }
        }

        UpdateStudent update = new UpdateStudent();
        update.execute();
    }

    @Override
    public void onClick(View v) {
        if(v == buttonUpdate){
            updateStudent();

        }

    }

You don't test to see if the check boxes are checked ( isSelected() ), you just get their text (which you knew anyway)

Could you show it on my code please?

Your code is your responsibility, but here's a trivial example you can adapt:

String s;
if (myCheckBox.isSelected()) 
   s= "option is selected":
else
  s = "option is not selected";

Ok thx I tried it, but it didn't worked with isSelected(). It worked withisChecked() but thx anyway :)

It's only doing what you told it on lines 3-9.
What do want it to do when the box isn't selected?

By the way, you're missing the $ symbol in the $_POST array:

$blood_typeA = _POST['Blood_TypeA'];
$blood_typeB = _POST['Blood_TypeB'];
$blood_type0 = _POST['Blood_Type0'];
$blood_typeAB = _POST['Blood_TypeAB'];

Should be:

$blood_typeA = $_POST['Blood_TypeA'];

And so on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.