0

I want to delete a row from my database using MySQL. I need to pass the parameter I want to delete from my Android app to the php script. I am using AsyncHttpClient but nothings happens, I get an OK message in my log.

I have tried changing the value username="$name" in my php script to an existing value in the table and it works correctly, the row is deleted. I think my error is in passing the value from Android to php.

deleteuser.php script:

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

$name = $_GET["username"];

$sql = 'DELETE FROM usuarios
        WHERE username="$name"';

mysql_select_db('etsiitcast_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not delete data: ' . mysql_error());
}
echo "Deleted data successfully\n";
mysql_close($conn);
?>

Android code:

 final AsyncHttpClient client = new AsyncHttpClient();

        Button delete = (Button) rootView.findViewById(R.id.delete);
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                RequestParams params = new RequestParams();
                params.put("username", user);

                client.post("http://" + IP_Server + "/etsiitcast_db/deleteuser.php", params, new AsyncHttpResponseHandler() {

                    @Override
                    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                        Log.d("OK", "Usuario eliminado");
                        getActivity().finish();
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                        Log.d("ERROR", "Se ha producido un error al eliminar al usuario");
                    }
                });
            }
        });

Where user is a String which value is the username:

Bundle extras = getActivity().getIntent().getExtras();

if (extras != null){
   user = extras.getString("user");
} else {
   user = "error";
}

UPDATE:

In my Android code I have changed this line:

client.get("http://" + IP_Server + "/etsiitcast_db/deleteuser.php", params, new AsyncHttpResponseHandler() { [...] }

New php script:

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysqli_error());
}

$name = $_GET["username"];
$name = $mysqli->real_escape_string($name);

$sql = 'DELETE FROM usuarios
        WHERE username="$name"';

mysqli_select_db('etsiitcast_db');
$retval = mysqli_query( $sql, $conn );
if(! $retval )
{
  die('Could not delete data: ' . mysqli_error());
}
echo "Deleted data successfully\n";
mysqli_close($conn);
?>
  • Note that you are using deprecated MySQL commands - use mysqli_* instead of mysql_*. Also your PHP script is exposed to SQL injection (don't pass $name as it is to the query! make sure you filter it first with mysqli_real_escape_string). – uri2x Sep 04 '15 at 14:54

1 Answers1

0

On your Android application, you're using client.post which generates a POST command to the webserver. However, on your PHP script, you're looking for the $_GET value.

You should either change your Android application to do client.get OR your PHP script to access $_POST['username'].

uri2x
  • 3,162
  • 1
  • 11
  • 25
  • I have tried both ways but it isn't working. I have added `$name = $mysqli->real_escape_string($name);` and changed `mysql_*` to `mysql_*`. – Irene Herrera Sep 04 '15 at 15:53
  • Can you update your question to reflect the changes you've made? – uri2x Sep 04 '15 at 15:54
  • `mysqli_query` has an opposite parameters order. Use `mysqli_query($conn, $sql);` instead of your current call, and check your php errors log to see if there are any others. – uri2x Sep 04 '15 at 16:07