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

标签 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 值。

运行该应用程序时,我尝试删除一个用户,它会进入“成功,用户已删除”警报对话框,但该用户仍在数据库中。

最佳答案

请尝试更改
$userId =$_REQUEST['userId'];


$userId =$_GET['userId'];

如果您在 userId 变量中获得正确的值,请检查 php。

关于java - 使用android从MySql数据库中删除用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33907388/

相关文章:

PHP- 提供 ID 时自动填充数据

具有依赖注入(inject)的 PHPUnit,使用 instanceof() 测试模拟对象

java - 比较数组内容精度算法

java - Java 代码缺少一些简单的东西

java - 有什么方法可以从参数化构造函数调用默认构造函数吗?

php - SQL 表中的 JSON 得到双结果

java - 如何使用数字获取货币对象

java - 无法弄清楚为什么数组显示为空

java - 如何将引用(不可序列化)从一个 Activity 传递到另一个 Activity ?

java - 挑战 : Custom Animation of ViewPager. 更改所选元素的高度(View fold)