gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:52:50 25 4
gpt4 key购买 nike

我正在尝试根据 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com