Can't Update values in MySQL table through my Android App

118
February 26, 2018, at 03:15 AM

I have a table that has two columns: UserName and Password, when the user signs up a new row is created with the Data he inputs, when he signs in the app searches the table for his username and then checks for the password, All of that is working fine, however i added the possibility for the user to change his password trough the app, for that, the app submits the Data to a PHP file which does the modifications, My PHP update file:

    <?php
    error_reporting(0);
    include("db_config.php");
    // array for JSON response
    $response = array();
    if( isset($_POST['username'] ) && isset($_POST['password']) ) {
       $username=$_POST['username'];
       $password=$_POST['password'];
       $result = mysql_query("update users1 set password='$password' where username='$username' ") or die(mysql_error());
       $row_count = mysql_affected_rows();
       if($row_count>0){
           $response["success"] = 1;
           $response["message"] = "Updated Sucessfully.";
       }
       else{
           $response["success"] = 0;
           $response["message"] = "Failed To Update.";  
       }  
       // echoing JSON response
       echo json_encode($response);
    }
    ?>

My db_config.php file:

    <?php
    $mysql_hostname = "localhost";
    $mysql_user = "root";
    $mysql_password = "";
    $mysql_database = "app";
    $db = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Opps some thing went wrong");
    mysql_select_db($mysql_database, $db) or die("Opps some thing went wrong");
    ?>

My Java file that extracts the user inputted data:

    package com.dragonite.test;
    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.Toast;
    import com.android.volley.Response;
    import com.android.volley.VolleyError;
    import org.json.JSONException;
    import org.json.JSONObject;
    import java.util.HashMap;
    import java.util.Map;
    /**
    * Created by Leondre on 2/25/2018.
    */
    public class ChangeInfo extends AppCompatActivity {
    EditText id_username;
    EditText id_password;
    String username, email, password;
    ProgressDialog PD;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_change_info);
    PD = new ProgressDialog(this);
    PD.setMessage("Wait...");
    PD.setCancelable(false);
    id_username = findViewById(R.id.id_username);
    id_password = id_email = findViewById(R.id.id_password);
    Intent i = getIntent();
    HashMap<String, String> item = (HashMap<String, String>) i.getSerializableExtra("username");
    username = "leondre68";
    password = id_password.getText().toString();
    id_username.setText(username);
}
public void Update(View v) {
    PD.show();
    password = id_password.getText().toString();
    String update_url = "http://192.168.1.103/api/update.php";
    Map<String, String> params = new HashMap<String, String>();
    params.put("username", username);
    params.put("password", password);
    CustomRequest update_request = new CustomRequest(update_url,
            params, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                int success = response.getInt("success");
                if (success == 1) {
                    PD.dismiss();
                    Toast.makeText(getApplicationContext(), "Updated Successfully", Toast.LENGTH_SHORT).show();
                    // redirect back
                    MoveToMainMenu();
                } else {
                    PD.dismiss();
                    Toast.makeText(getApplicationContext(), "Failed to update", Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });
    // Adding request to request queue
    QueueRequest.getInstance().addToReqQueue(update_request);
}
public void delete(View view) {
    //perform delete
}
private void MoveToMainMenu() {
    Intent read_intent = new Intent(ChangeInfo.this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(read_intent);
}
}

The above code is then sent to this class:

package com.dragonite.test;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
/**
* Created by Leondre on 2/25/2018.
*/
public class CustomRequest extends Request<JSONObject>{
private Listener<JSONObject> listener;
private Map<String, String> params;
public CustomRequest(String url, Map<String, String> params,
                     Listener<JSONObject> reponseListener, ErrorListener errorListener) {
    super(Method.GET, url, errorListener);
    this.listener = reponseListener;
    this.params = params;
}
public CustomRequest(int method, String url, Map<String, String> params,
                     Listener<JSONObject> reponseListener, ErrorListener errorListener) {
    super(method, url, errorListener);
    this.listener = reponseListener;
    this.params = params;
}
@Override
protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
    return params;
};
@Override
protected void deliverResponse(JSONObject response) {
    listener.onResponse(response);
}
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
    try {
        String jsonString = new String(response.data,
                HttpHeaderParser.parseCharset(response.headers));
        return Response.success(new JSONObject(jsonString),
                HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JSONException je) {
        return Response.error(new ParseError(je));
    }
}
}

And finally the following class executes the connection to the server:

package com.dragonite.test;
/**
* Created by Leondre on 2/25/2018.
*/
import android.app.Application;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
public class QueueRequest extends Application {
private RequestQueue mRequestQueue;
private static QueueRequest mInstance;
@Override
public void onCreate() {
    super.onCreate();
    mInstance = QueueRequest.this;
}
public static synchronized QueueRequest getInstance() {
    return mInstance;
}
public RequestQueue getReqQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }
    return mRequestQueue;
}
public <T> void addToReqQueue(Request<T> req, String tag) {
    getReqQueue().add(req);
}
public <T> void addToReqQueue(Request<T> req) {
    getReqQueue().add(req);
}
public void cancelPendingReq(Object tag) {
    if (mRequestQueue != null) {
        mRequestQueue.cancelAll(tag);
    }
}
}

Image of my MySQL table: https://i.imgur.com/qHG9aLZ.png

When i click on the update button the Please Wait keeps loading forever and the data on the server never gets updated, Any thoughts why this is happening? I have been trying to figure it out for almost 4 hours now! Thanks

Rent Charter Buses Company
READ ALSO
JSON array accent display

JSON array accent display

I'm trying to display a json array with contents from my database but when their is accents in the database the json array doesn't want to be displayed, their is my code:

230
How to include the header and the footer of wordpress website to a custom php file?

How to include the header and the footer of wordpress website to a custom php file?

I have a custom php file that I want to include the headerphp and the footer

174
How to make a progress bar

How to make a progress bar

How would one go about making a progress bar in html/css/javascriptI don't really want to use Flash

149