The problem is the following, in my application I try to change a database record from android, which would be a user's password, the problem comes when pressing the button that should do the action does nothing , neither shows error message, nor changes anything in the database. I take the password of an editText and the mail of an intent from a previous screen and I pass both by post to the php.
Next I will put the codes used for this:
PHP.
<?php
//Datos BD
$user = "id6152109_admin";
$pass = "admin";
$bd = "id6152109_kiriki";
$server = "localhost";
if(isset($_POST['password'])){
$E = $_POST["correo"];
$P = $_POST["password"];
//Evitar inyección de código
$correo = htmlspecialchars($E);
$password = htmlspecialchars($P);
//Encriptamos password
$encrip = password_hash($password, PASSWORD_DEFAULT);
//Conexión BD
$mysqli = mysqli_connect($server,$user,$pass,$bd);
//Consulta SQL (SELECT). Comprobamos si el email ya existe
$sqlSelect = "SELECT * FROM Usuario WHERE Correo = '".$correo."'" ;
$result = mysqli_query($mysqli, $sqlSelect);
//Comprobar resultado
if (mysqli_num_rows($result) == 1) {
$sqlUpdate = "UPDATE Usuario SET Password = '".$encrip."' WHERE Correo = '".$correo."'";
$result2 = mysqli_query($mysqli,$sqlUpdate);
if(mysqli_connect_errno()==0){
//Consulta correcta
$response["success"] = 1;
$response["message"] = "Update realizado con exito";
}else{
$response["success"] = 0;
$response["message"] = "Fallo en el update";
//Mostramos respuesta JSON
echo json_encode($response);
}
}else if(mysqli_num_rows($result) == 0){
//Consulta incorrecta
$response["success"] = 0;
$response["message"] = "No existe un usuario con ese correo";
//Mostramos respuesta JSON
echo json_encode($response);
}else{
//Consulta incorrecta
$response["success"] = 0;
$response["message"] = "Hay mas de un usuario con ese correo.";
//Mostramos respuesta JSON
echo json_encode($response);
}
}else{
//Consulta incorrecta
$response["success"] = 0;
$response["message"] = "Error.Campo vacio";
//Mostramos respuesta JSON
echo json_encode($response);
}
?>
Class of the update to use the gson.
package com.example.alex.proyecto;
public class UpdatePass {
/**
* success : 0
* message : Error.Campo vacio
*/
private int success;
private String message;
public int getSuccess() {
return success;
}
public void setSuccess(int success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Class where the database is updated, the action is performed if the editText are not empty.
package com.example.alex.proyecto;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import org.json.JSONObject;
import cz.msebera.android.httpclient.Header;
public class CambioPassActivity extends AppCompatActivity {
EditText pass,pass2;
Button bCambiar;
String correoN;
String url = "ftp://[email protected]/public_html/cambiarPass.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pantalla_cambiopass);
pass = (EditText) findViewById(R.id.textoPass);
pass2 = (EditText) findViewById(R.id.textoPass2);
bCambiar = (Button) findViewById(R.id.botonCambioPass);
correoN = getIntent().getStringExtra("correo");
bCambiar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), correoN, Toast.LENGTH_LONG).show();
if(pass.getText().toString().trim().equalsIgnoreCase("")||pass2.getText().toString().trim().equalsIgnoreCase("")){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View formElementsView = inflater.inflate(R.layout.pantalla_jugadores,
null, false);
AlertDialog.Builder builder = new AlertDialog.Builder(CambioPassActivity.this);
builder.setTitle("Deben estar todos los campos rellenos.");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
builder.setView(formElementsView);
final AlertDialog dialog = builder.create();
dialog.show();
}else{
if(pass.getText().toString().trim().equalsIgnoreCase(pass2.getText().toString().trim())==true) {
AsyncHttpClient cliente = new AsyncHttpClient();
RequestParams rp = new RequestParams();
rp.put("correo", correoN);
rp.put("password", pass.getText().toString().trim());
cliente.post(url, rp, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
if (statusCode == 200) {
String respuesta = response.toString();
Gson gson = new Gson();
UpdatePass update = gson.fromJson(respuesta, UpdatePass.class);
String mensaje = update.getMessage();
Toast.makeText(getApplicationContext(), mensaje, Toast.LENGTH_LONG).show();
int res = update.getSuccess();
//Si es correcta
if (res == 1) {
Toast.makeText(getApplicationContext(), "Contraseña cambiada", Toast.LENGTH_LONG).show();
//Una vez iniciada la sesión, nos vamos a la pantalla principal
Intent Principal = new Intent(getApplicationContext(), MainActivity.class);
finish();
startActivity(Principal);
}
}
}
});
}
}
}
});
}
}
Finally I will put a photo of the table in question with which I am working.
Testing I have verified that the application goes well until arriving at the client.post, from there it does not do anything for what I suppose that it will be error in the onSuccess, but I still do not know which one.