使用android从MySql数据库中删除用户

时间:2015-11-25 02:10:14

标签: java php android mysql

我正在尝试根据id从mysql表中删除一行。应该是相当直接的,这可能非常简单,我做错了。服务器使用xampp在本地托管。

这是java异步任务代码:

private class DeleteUserTask extends AsyncTask<ApiConnector,Long,Boolean> {

    @Override
    protected Boolean doInBackground(ApiConnector... params) {

        // it is executed on Background thread

        return params[0].DeleteUser(userId);
    }

    @Override
    protected void onPostExecute(Boolean deleted) {
        if (deleted){
            AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(ViewUsers.this);
            dlgAlert.setMessage("User Deleted");
            dlgAlert.setTitle("Success");
            dlgAlert.setPositiveButton("OK", null);
            dlgAlert.setCancelable(true);
            dlgAlert.create().show();
        }else{
            AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(ViewUsers.this);
            dlgAlert.setMessage("User Not Deleted");
            dlgAlert.setTitle("Failed");
            dlgAlert.setPositiveButton("OK", null);
            dlgAlert.setCancelable(true);
            dlgAlert.create().show();
        }
    }

}

创建并执行asyncTask:

final DeleteUserTask asyncDeleteUser = new DeleteUserTask();
asyncDeleteUser.execute(new ApiConnector());

ApiConnector方法:

public Boolean DeleteUser(int userId){
    // URL for getting all customers

    String url = "http://192.168.20.107/webservice/deleteUser.php?userId="+userId;

    // Get HttpResponse Object from url.
    // Get HttpEntity from Http Response Object

    HttpEntity httpEntity = null;

    try
    {
        DefaultHttpClient httpClient = new DefaultHttpClient();  // Default HttpClient
        HttpGet httpGet = new HttpGet(url);

        HttpResponse httpResponse = httpClient.execute(httpGet);

        httpEntity = httpResponse.getEntity();

        return true;
    } catch (ClientProtocolException e) {
        // Signals error in http protocol
        e.printStackTrace();
        //Log Errors Here
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

和服务器上托管的deleteUser.php文件:

<?php

$connection = mysqli_connect("localhost","root","","webservice") or die("Error " . mysqli_error($connection));

$userId =$_REQUEST['userId'];

$sql = "DELETE FROM users WHERE id = '$userId'";
mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));


mysqli_close($connection);
?>

我可以确认java代码中的userId变量是否包含正确的int值。

当运行应用程序并尝试删除用户时,它会转到“成功,用户已删除”alertdialog,但用户仍在数据库中。

3 个答案:

答案 0 :(得分:1)

请尝试改变 $ userId = $ _ REQUEST [&#39; userId&#39;];

要 $ userId = $ _ GET [&#39; userId&#39;];

检查php是否在userId变量中获得正确的值。

答案 1 :(得分:1)

只要其中的 JAVA 代码运行(连接到网址),您的try块将始终返回true。 php和mysqli 代码中的错误不会以任何方式影响它,因为java和android无法区分具有工作php和成功查询的站点与有错误的站点之间的区别。 (再看几行代码来查看网站是否有错误可以解决问题)

将您的网址(使用userId)复制并粘贴到计算机的网络浏览器中(http://192.168.20.107/webservice/deleteUser.php?userId=1)并阅读其显示的错误消息(如果有)。

也可以使用

$userId = $_GET['userId'];

而不是

$userId = $_REQUEST['userId'];

答案 2 :(得分:-1)

更改此行代码:

$sql = "DELETE FROM users WHERE id = '$userId'";

为:

$sql = "DELETE FROM users WHERE id = $userId";