将Checkbox值发送到MySql数据库

时间:2015-09-01 16:09:05

标签: android mysql checkbox

大家好我尝试将我的Checkboxe的值发送到我的MySQl数据库。

问题是,当我想发送关于Edittext或Radiobutton的值工作正常,我没有问题,但功能Checkbox对我来说是新的和ia newbe in this.Below我的代码也是php文件,我希望你能帮助我来解决我的问题。

公共类ProfilEinstellungen extends Activity实现了View.OnClickListener {

public static final String DEFAULT = "N/A";

private EditText Benutzername,Passwort;
private EditText eVorname,eNachname;
private RadioGroup geschlechtGroup;
private RadioButton RB1;
private CheckBox m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12;
private EditText Tag,Monat,Jahr;
private Button bSpeichern;


// Progress Dialog
private ProgressDialog pDialog;

// JSON parser class
JSONParser jsonParser = new JSONParser();

private static final String POST_COMMENT_URL = "http://192.168.56.1:80/webservice/daten.php";


private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";

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

    // TextViews
    Benutzername = (EditText) findViewById(R.id.ED_Benutzername_Einstellungen);
    Passwort = (EditText) findViewById(R.id.ED_Passwort_Einstellungen);
    //EiditText
    eVorname = (EditText) findViewById(R.id.ED_Vorname_Einstellungen);
    eNachname = (EditText) findViewById(R.id.ED_Nachname_Einstellungen);

    //Radiogroup
    geschlechtGroup = (RadioGroup) findViewById(R.id.RadioGroup_Einstellungen);

   //EditText
    Tag = (EditText) findViewById(R.id.Spinner_GEBtag_einstellungen);
    Monat = (EditText) findViewById(R.id.Spinner_GEBmonat_einstellungen);
    Jahr = (EditText) findViewById(R.id.Spinner_GEBjahr_einstellungen);




    //Benutzername und Passwort
    SharedPreferences sharedPreferences = getSharedPreferences("MyData", Context.MODE_PRIVATE );
    String name = sharedPreferences.getString("name", "");
    String passwort = sharedPreferences.getString("passwort","");
    Benutzername.setText(name);
    Passwort.setText(passwort);

    //button
    bSpeichern = (Button) findViewById(R.id.BUTTON_speichen_einstellungen);
    bSpeichern.setOnClickListener(this);


}

@Override
public void onClick(View v) {
    new saveData().execute();


}


class saveData extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(ProfilEinstellungen.this);
        pDialog.setMessage("Speichert...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... args) {

        // Hier wird der Name vom Login übernommen und gleich auch an den Server geschickt
        SharedPreferences sharedPreferences = getSharedPreferences("MyData", Context.MODE_PRIVATE);
        String post_name = sharedPreferences.getString("name", "");
        // Hier werden die Eingegeben Daten in strings umgewandelt
        int success;
        String post_vorname = eVorname.getText().toString();
        String post_nachname = eNachname.getText().toString();

        int selectRadioButtonGeschlecht = geschlechtGroup.getCheckedRadioButtonId();
        RB1 = (RadioButton) findViewById(selectRadioButtonGeschlecht);
        String post_geschlecht = RB1.getText().toString();

        String post_tag = Tag.getText().toString();
        String post_monat = Monat.getText().toString();
        String post_jahr = Jahr.getText().toString();
        //Checkbox
        m1 = (CheckBox) findViewById(R.id.Musik1);
        m1.setOnClickListener(ProfilEinstellungen.this);
        m2 = (CheckBox) findViewById(R.id.Musik2);
        m2.setOnClickListener(ProfilEinstellungen.this);
        m3 = (CheckBox) findViewById(R.id.Musik3);
        m3.setOnClickListener(ProfilEinstellungen.this);
        m4 = (CheckBox) findViewById(R.id.Musik4);
        m4.setOnClickListener(ProfilEinstellungen.this);
        m5 = (CheckBox) findViewById(R.id.Musik5);
        m5.setOnClickListener(ProfilEinstellungen.this);
        m6 = (CheckBox) findViewById(R.id.Musik6);
        m6.setOnClickListener(ProfilEinstellungen.this);
        m7 = (CheckBox) findViewById(R.id.Musik7);
        m7.setOnClickListener(ProfilEinstellungen.this);
        m8 = (CheckBox) findViewById(R.id.Musik8);
        m8.setOnClickListener(ProfilEinstellungen.this);
        m9 = (CheckBox) findViewById(R.id.Musik9);
        m9.setOnClickListener(ProfilEinstellungen.this);
        m10 = (CheckBox) findViewById(R.id.Musik10);
        m10.setOnClickListener(ProfilEinstellungen.this);
        m11 = (CheckBox) findViewById(R.id.Musik11);
        m11.setOnClickListener(ProfilEinstellungen.this);
        m12 = (CheckBox) findViewById(R.id.Musik12);
        m12.setOnClickListener(ProfilEinstellungen.this);

        if (m1.isChecked()||
                m2.isChecked()||
                m3.isChecked()||
                m4.isChecked()||
                m5.isChecked()||
                m6.isChecked()||
                m7.isChecked()||
                m8.isChecked()||
                m9.isChecked()||
                m10.isChecked()||
                m11.isChecked()||
                m12.isChecked()
                ){
            String post_90 = m1.getText().toString();
            String post_charts = m2.getText().toString();
            String post_house = m3.getText().toString();
            String post_hiphop = m4.getText().toString();
            String post_rnb = m5.getText().toString();
            String post_pop = m6.getText().toString();
            String post_rock = m7.getText().toString();
            String post_metal = m8.getText().toString();
            String post_indie = m9.getText().toString();
            String post_balkan = m10.getText().toString();
            String post_latin = m11.getText().toString();
            String post_sonstiges = m12.getText().toString();

            try {
                // Building Parameters
                List<NameValuePair> params1 = new ArrayList<NameValuePair>();

                // Musik
                params1.add(new BasicNameValuePair("90", post_90));
                params1.add(new BasicNameValuePair("charts", post_charts));
                params1.add(new BasicNameValuePair("house", post_house));
                params1.add(new BasicNameValuePair("hiphop", post_hiphop));
                params1.add(new BasicNameValuePair("rnb", post_rnb));
                params1.add(new BasicNameValuePair("pop", post_pop));
                params1.add(new BasicNameValuePair("rock", post_rock));
                params1.add(new BasicNameValuePair("metal", post_metal));
                params1.add(new BasicNameValuePair("indie", post_indie));
                params1.add(new BasicNameValuePair("balkan", post_balkan));
                params1.add(new BasicNameValuePair("latin", post_latin));
                params1.add(new BasicNameValuePair("sonstiges", post_sonstiges));


                Log.d("request!", "starting");

                //Posting user data to script
                JSONObject json = jsonParser.makeHttpRequest(
                        POST_COMMENT_URL, "POST", params1);

                // full json response
                Log.d("Versuch send Data", json.toString());

                // json success element
                success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    Log.d("gespeichert...", json.toString());
                    Intent i = new Intent(ProfilEinstellungen.this,ProfilEinstellungen.class);
                    finish();
                    startActivity(i);
                    return json.getString(TAG_MESSAGE);
                }else{
                    Log.d("Da hat was nicht geklappt", json.getString(TAG_MESSAGE));
                    return json.getString(TAG_MESSAGE);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }



        // hier wird das Bundl erstellt was an den Server geschickt werden soll
        try {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();

            //Persönliche Daten
            params.add(new BasicNameValuePair("benutzername", post_name));
            params.add(new BasicNameValuePair("firstname", post_vorname));
            params.add(new BasicNameValuePair("lastname", post_nachname));

            // Geschlecht
            params.add(new BasicNameValuePair("geschlecht", post_geschlecht));

            //Geburstag
            params.add(new BasicNameValuePair("tag", post_tag));
            params.add(new BasicNameValuePair("monat", post_monat));
            params.add(new BasicNameValuePair("jahr", post_jahr));


            Log.d("request!", "starting");

            //Posting user data to script
            JSONObject json = jsonParser.makeHttpRequest(
                    POST_COMMENT_URL, "POST", params);

            // full json response
            Log.d("Versuch send Data", json.toString());

            // json success element
            success = json.getInt(TAG_SUCCESS);
            if (success == 1) {
                Log.d("gespeichert...", json.toString());
                Intent i = new Intent(ProfilEinstellungen.this,ProfilEinstellungen.class);
                finish();
                startActivity(i);
                return json.getString(TAG_MESSAGE);
            }else{
                Log.d("Da hat was nicht geklappt", json.getString(TAG_MESSAGE));
                return json.getString(TAG_MESSAGE);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;

    }

    protected void onPostExecute(String file_url) {
        // dismiss the dialog once product deleted
        pDialog.dismiss();
        if (file_url != null){
            Toast.makeText(ProfilEinstellungen.this, file_url, Toast.LENGTH_LONG).show();
        }

    }

}

}

Php文件

<?php

//load and connect to MySQL database stuff
require("config.inc.php");


if (!empty($_POST)) {
    //initial query
    $query = "INSERT INTO fulldata ( benutzername, firstname, lastname, geschlecht, tag, monat, jahr, 90, charts, house, hiphop, rnb, pop, rock, metal, indie, balkan, latin, sonstiges)VALUES( :benutzer,:firstname, :lastname, :geschlecht, :tag, :monat, :jahr, :90, :charts, :house, :hiphop, :rnb, :pop, :rock, :metal, :indie, :balkan, :latin, :sonstiges) ";

    //Update query
    $query_params = array(
        ':benutzer' => $_POST['benutzername'],
        ':firstname' => $_POST['firstname'],
        ':lastname' => $_POST['lastname'],
         ':geschlecht' => $_POST['geschlecht'],
        ':tag' => $_POST['tag'],
        ':monat' => $_POST['monat'],
        ':jahr' => $_POST['jahr'],

        ':90' => $_POST['90'],
        ':charts' => $_POST['charts'],
        ':house' => $_POST['house'],
        ':hiphop' => $_POST['hiphop'],
        ':rnb' => $_POST['rnb'],
        ':pop' => $_POST['pop'],
        ':rock' => $_POST['rock'],
        ':metal' => $_POST['metal'],
        ':indie' => $_POST['indie'],
        ':balkan' => $_POST['balkan'],
        ':latin' => $_POST['latin'],
        ':sonstiges' => $_POST['sonstiges'],

    );

    //execute query
    try {
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);
    }
    catch (PDOException $ex) {
        // For testing, you could use a die and message. 
        //die("Failed to run query: " . $ex->getMessage());

        //or just use this use this one:
        $response["success"] = 0;
        $response["message"] = "Database Error. Couldn't add post!";
        die(json_encode($response));
    }

    $response["success"] = 1;
    $response["message"] = "Wurde gespeichert";
    echo json_encode($response);

} else {
?>
        <h1>Add Data</h1> 
        <form action="datenvervollstaendigen.php" method="post">    
            Username:<br /> 
            <input type="text" name="Username" placeholder="Username" /> 
            <br /><br /> 

            firstname:<br /> 
            <input type="text" name="firstname" placeholder="firstname" /> 
            <br /><br /> 

            lastname:<br /> 
            <input type="text" name="lastname" placeholder="lastname" /> 
            <br /><br />

            tag:<br /> 
            <input type="text" name="tag" placeholder="tag" /> 
            <br /><br />

            monat:<br /> 
            <input type="text" name="monat" placeholder="monat" /> 
            <br /><br />

            jahr:<br /> 
            <input type="text" name="jahr" placeholder="jahr" /> 
            <br /><br />

            90:<br /> 
            <input type="text" name="90" placeholder="jahr" /> 
            <br /><br />

            charts:<br /> 
            <input type="text" name="charts" placeholder="charts" /> 
            <br /><br />

            house:<br /> 
            <input type="text" name="house" placeholder="house" /> 
            <br /><br />

            hiphop:<br /> 
            <input type="text" name="hiphop" placeholder="hiphop" /> 
            <br /><br />


            rnb:<br /> 
            <input type="text" name="rnb" placeholder="rnb" /> 
            <br /><br />


            pop:<br /> 
            <input type="text" name="pop" placeholder="pop" /> 
            <br /><br />


            rock:<br /> 
            <input type="text" name="rock" placeholder="rock" /> 
            <br /><br />


            indie:<br /> 
            <input type="text" name="indie" placeholder="indie" /> 
            <br /><br />


            balkan:<br /> 
            <input type="text" name="balkan" placeholder="balkan" /> 
            <br /><br />


            latin:<br /> 
            <input type="text" name="latin" placeholder="latin" /> 
            <br /><br />


            sonstiges:<br /> 
            <input type="text" name="sonstiges" placeholder="sonstiges" /> 
            <br /><br />


            <input type="submit" value="Add Data" /> 
        </form> 
    <?php
}

?> 

如果你需要更多,请告诉我。

1 个答案:

答案 0 :(得分:0)

我只用HTML&amp; PHP所以也许这些信息并没有真正帮助,但我会尽我所能。

对于HTML,假设您有一个表单,请为BOTH值指定一个类型=&#34;复选框&#34;,一个id和一个名称。您只需使用:

if(isset($_POST['NameOfCheckBoxHere']))
  {
    $checkbox = 'checked';
  }else $checkbox = 'unchecked';

再一次,抱歉,我无法在JSON中提供帮助。