将注册数据发送到PHP并存储在MySQL数据库中

时间:2016-08-28 18:49:55

标签: php mysql database android-studio

所以,我想将数据从我的应用程序(注册页面)发送到php并将其存储在MySQL数据库中,我的代码基于一个似乎可行的教程,但不知何故它不适用于我的应用程序。每当我输入数据并单击按钮时,我在数据库中获得一个新用户,但是有空字段,因此内容不会被传递。有没有人知道我需要改变什么?提前谢谢!

public class JetztRegistrieren extends Startseite {

Button bRegistrierungAbschliessen;
EditText etUserName, etUserMail, etUserPasswort, etUserPasswort2, etGeburtsdatum, etTelefonnummer;
RadioButton rbMaennlich, rbWeiblich;
RadioGroup rgGeschlecht;
protected String enteredname, enteredemail, enteredpasswort, enteredpasswort2, enteredGeb, enteredTelnr, userGender;

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

    bRegistrierungAbschliessen = (Button) findViewById(R.id.bRegistrierungAbschliessen);
    etUserName = (EditText) findViewById(R.id.etUserName);
    etUserMail = (EditText) findViewById(R.id.etUserMail);
    etUserPasswort = (EditText) findViewById(R.id.etUserPasswort);
    etUserPasswort2 = (EditText) findViewById(R.id.etUserPasswort2);
    etGeburtsdatum = (EditText) findViewById(R.id.etGeburtsdatum);
    etTelefonnummer = (EditText) findViewById(R.id.etTelefonnummer);
    rgGeschlecht = (RadioGroup) findViewById(R.id.rgGeschlecht);
    rbMaennlich = (RadioButton) findViewById(R.id.rbMaennlich);
    rbWeiblich = (RadioButton) findViewById(R.id.rbWeiblich);

    enteredname = etUserName.getText().toString();
    enteredemail = etUserMail.getText().toString();
    enteredpasswort = etUserPasswort.getText().toString();
    enteredpasswort2 = etUserPasswort2.getText().toString();
    enteredTelnr = etTelefonnummer.getText().toString();
    enteredGeb = etGeburtsdatum.getText().toString();
    userGender = ((RadioButton) findViewById(rgGeschlecht.getCheckedRadioButtonId())).getText().toString();


    bRegistrierungAbschliessen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            BackGround b = new BackGround();
            b.execute(enteredname, enteredemail, enteredpasswort, enteredpasswort2, enteredGeb, enteredTelnr, userGender);

            //Bedingung, dass Daten noch nicht existieren und alles ausgefüllt fehlt noch
            //-----------------------------------------------------------------

            // Toast.makeText(JetztRegistrieren.this, "Willkommen bei Stage Driver! Jetzt einloggen.", Toast.LENGTH_LONG).show();
            // Intent intent = new Intent(getApplicationContext(), Startseite.class);
            // startActivity(intent);
        }

    });
}

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

    @Override
    protected String doInBackground(String... params) {
        String enteredemail = params[0];
        String enteredpassword = params[1];
        String enteredpassword2 = params[2];
        String enteredTelnr = params[3];
        String enteredGeb = params[4];
        String userGender = params[5];
        String enteredname = params[6];
        String data = "";
        int tmp;

        try {
            URL url = new URL("http://pou-pou.de/stagedriver/android/register.php");
            String urlParams = "enteredemail=" + enteredemail + "&enteredpassword=" + enteredpassword + "&enteredpassword2=" + enteredpassword2 + "&enteredTelnr=" + enteredTelnr + "&enteredGeb=" + enteredGeb + "&userGender=" + userGender + "$enteredname" + enteredname;

            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setDoOutput(true);
            OutputStream os = httpURLConnection.getOutputStream();
            os.write(urlParams.getBytes());
            os.flush();
            os.close();
            InputStream is = httpURLConnection.getInputStream();
            while ((tmp = is.read()) != -1) {
                data += (char) tmp;
            }
            is.close();
            httpURLConnection.disconnect();

            return data;

        } catch (MalformedURLException e) {
            e.printStackTrace();
            return "Exception: " + e.getMessage();
        } catch (IOException e) {
            e.printStackTrace();
            return "Exception: " + e.getMessage();
        }
    }
    @Override
    protected void onPostExecute(String s) {
        Toast.makeText(JetztRegistrieren.this, "Inhalt gespeichert", Toast.LENGTH_LONG).show();
    }
}}

和php脚本:

<?php
include('config.php');
session_start();

$name = $_POST["enteredname"];
$pass = $_POST["enteredpassword"];
$pass2 = $_POST["enteredpassword2"];
$email = $_POST["enteredemail"];
$tel = $_POST["enteredTelnr"];
$geb = $_POST["enteredGeb"];
$gender = $_POST["userGender"];


$sql = "INSERT INTO user (user_name, user_pw, user_mail, user_tel, user_geb, user_geschl)
        VALUES ('".$name."', '".$pass."', '".$email."', '".$tel."', '".$geb."', '".$gender."');";
$query = mysql_query($sql);

if(!$query)        {
        echo mysql_error();
        die;
}
?>

0 个答案:

没有答案