1

So I have a ProfileActivity where I pass the user's details to EditProfileActivity.. the passed values are placed correctly on their specified edittexts. But whenever I change the values in the edittexts and clicked the save button..the values on the database are not modified..Can you please help me

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;
        }
    }







}

Here is my php code for update:

    <?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 Answer 1

1

You need to update the values on click event otherwise you will always be passing the same value that you received in 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();
Sign up to request clarification or add additional context in comments.

4 Comments

the values are still not updating :(
i mentioned the one clear bug , now the other option is debug your php codes and params , echo them to see the state of your values
Can you check my updated code sir. Values are still not updating on the database.. I posted my php code
simply echo values of $Fullname and all variables and if you get the expected values then it's mean something wrong with your query and also capture the response of mysql_query($query_insert) and see it's true or false and generate response accordingly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.