【问题标题】:Delete a user from MySql database using android使用android从MySql数据库中删除用户
【发布时间】:2016-02-27 16:54:47
【问题描述】:

我正在尝试根据 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();
        }
    }

}

创建和执行异步任务:

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' 上的那些单引号吗?其次,您应该真正考虑一些输入验证以避免 SQL 注入。
  • @bearzed 我已将查询更改为不带单引号,但没有任何区别。还添加了 $userId = mysqli_real_escape_string($connection, $userId); 以将任何特殊字符视为文字字符。这是你的意思吗?

标签: java php android mysql


【解决方案1】:

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

到 $userId =$_GET['userId'];

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

【讨论】:

    【解决方案2】:

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

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

    也用

    $userId = $_GET['userId'];
    

    而不是

    $userId = $_REQUEST['userId'];
    

    【讨论】:

      【解决方案3】:

      更改这行代码:

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

      到:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-15
        • 2020-05-01
        • 2017-01-24
        • 1970-01-01
        • 2015-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多