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

Java 使用android从MySql数据库中删除用户,java,php,android,mysql,Java,Php,Android,Mysql,我正在尝试根据id从mysql表中删除一行。这应该是相当直接的,可能是我做错了一些非常简单的事情。服务器使用xampp在本地托管 以下是java异步任务代码: private class DeleteUserTask extends AsyncTask<ApiConnector,Long,Boolean> { @Override protected Boolean doInBackground(ApiConnector... params) { /

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

}
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);
?>

更改这行代码:

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

请换衣服试试 $userId=$_请求['userId']

到 $userId=$_GET['userId']


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

只要try块中的JAVA代码运行(连接到url),try块将始终返回true。php和mysqli代码中的错误不会对其产生任何影响,因为java和android无法区分具有正常工作的php和成功查询的站点与具有错误的站点。(再多写几行代码,看看网站是否返回错误,就能解决这个问题)

将您的url(带有用户ID)复制并粘贴到计算机的web浏览器()中,并读取它显示的错误消息(如果有)

也使用

$userId = $_GET['userId'];
而不是

$userId = $_REQUEST['userId'];

两件事:首先,你确定你真的需要
“$userId”
上的那些单引号吗?其次,您应该考虑一些输入验证,以避免SQL注入。@我已经将查询更改为没有单引号,但这没有任何区别。还添加了
$userId=mysqli\u real\u escape\u字符串($connection,$userId)
将任何特殊字符视为文字字符。这就是你的意思吗?
$userId = $_GET['userId'];
$userId = $_REQUEST['userId'];