数据库值未更新(Android)

时间:2017-09-02 19:41:07

标签: java php android

所以我有一个ProfileActivity,我将用户的详细信息传递给EditProfileActivity ..传递的值正确放置在他们指定的edittexts上。但每当我更改edittexts中的值并单击保存按钮时,数据库中的值都不会被修改。可以帮助我

package com.example.androidproject;


public class EditProfileActivity extends Activity {

    Button Save, Delete;
    EditText tname,tusername, tpassword, tpassword2, tbio;
    TextView uname;
    User u = new User();
    String editfullname,editpw,editpw2,editbio,getuname;
    String fn,b,pw,pw2;
    TextView tv1,tv2,tv3;
    String getfn,getpw,getbio;

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

        Save = (Button) findViewById(R.id.buttonsignup);
        Delete = (Button) findViewById(R.id.button1);
        tname = (EditText) findViewById(R.id.txtfullname);
        tusername = (EditText) findViewById(R.id.txtun);
        tpassword = (EditText) findViewById(R.id.txtpw);
        tpassword2 = (EditText) findViewById(R.id.txtpw2);
        tbio = (EditText) findViewById(R.id.txtbio);
        uname = (TextView) findViewById(R.id.getusername);
        tv1 = (TextView) findViewById(R.id.textView2);
        tv2 = (TextView) findViewById(R.id.textView3);
        tv3 = (TextView) findViewById(R.id.textView4);

        Intent intent = getIntent();
        u.SetUsername(intent.getStringExtra(u.username()));
        editfullname = (intent.getStringExtra("Fullname"));
        editbio = (intent.getStringExtra("Bio"));
        editpw = (intent.getStringExtra("Password"));
        editpw2 = (intent.getStringExtra("Password2"));
        uname.setText(u.getUsername());

        tname.setText(editfullname);
        tpassword.setText(editpw);
        tpassword2.setText(editpw2);
        tbio.setText(editbio);

        getuname = u.username().toString();



        Save.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

            fn = tname.getText().toString();
            pw = tpassword.getText().toString();
            b = tbio.getText().toString();

            tv1.setText(fn);   //i displayed to textviews the NEW values inputted from the edittexts. 
            tv2.setText(pw);
            tv3.setText(b);

            getfn = tv1.getText().toString();    //i put these to the namevalupairs in my asynctask
            getpw = tv2.getText().toString();
            getbio = tv3.getText().toString();


               new SaveDataTask().execute();



            }
        });



            }

    class SaveDataTask extends AsyncTask<String, String, Void> {
        protected void onPreExecute() {

        }

        @Override
        protected Void doInBackground(String... params) {
            byte[] data;
            HttpPost httppost;
            StringBuffer buffer = null;
            HttpResponse response;
            HttpClient httpclient;
            InputStream inputStream;
            List<NameValuePair> nameValuePairs;

             nameValuePairs = new ArrayList<NameValuePair>(4);
             nameValuePairs.add(new BasicNameValuePair("Fullname", getfn.trim()));
             nameValuePairs.add(new BasicNameValuePair("Username", getuname));
             nameValuePairs.add(new BasicNameValuePair("Password", getpw.trim()));
             nameValuePairs.add(new BasicNameValuePair("Bio", getbio.trim()));
            try {
                httpclient = new DefaultHttpClient();
                httppost = new HttpPost("http://192.168.1.6/webservices/mycontroller/updateuser.php");


                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                response = httpclient.execute(httppost);
                inputStream = response.getEntity().getContent();

                data = new byte[256];

                buffer = new StringBuffer();

                int len = 0;

                while (-1 != (len = inputStream.read(data)) ) {
                    buffer.append(new String(data, 0, len));
                }
                //name= buffer.toString();



                inputStream.close();




                runOnUiThread(new Runnable(){
                    public void run() {
                        Toast.makeText(EditProfileActivity.this, "UPDATED", Toast.LENGTH_LONG).show();


                    }
                });



            }
            catch (Exception e) {
                Toast.makeText(EditProfileActivity.this, "error" + e.toString(), Toast.LENGTH_LONG).show();
            }
            return null;
        }
    }







}

这是我的更新PHP代码:

    <?php 
    mysql_connect("localhost","root","");
    mysql_select_db("poetrydb");

    $Fullname = $_POST['Fullname'];
    $Username = $_POST['Username'];
    $Password = $_POST['Password'];
    $Bio = $_POST['Bio'];


    $query_insert = "UPDATE account SET FullName ='$Fullname', Bio= '$Bio', Password ='$Password' WHERE Username ='$Username'";
     mysql_query($query_insert) or die(mysql_error());
    echo "UPDATED";

    ?>

1 个答案:

答案 0 :(得分:1)

您需要更新点击事件的值,否则您将始终传递您在onCreate

中收到的相同值
 Save.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
    // fetch the updated values from edittext and 
    // store them in string references 
        fn = tname.getText().toString();
        getuname = u.username().toString();
        pw = tpassword.getText().toString();
        b = tbio.getText().toString();
        new SaveDataTask().execute();