gpt4 book ai didi

java - Android 使用 UrlConnection 从数据库中删除行

转载 作者:行者123 更新时间:2023-11-29 18:43:08 24 4
gpt4 key购买 nike

我在尝试使用 UrlConnection POST 方法从数据库中删除行时遇到问题。我发送 id 并根据 id 执行删除查询但仍然不起作用

public String  performPostCall(String requestURL,HashMap<String, String> postDataParams) {
URL url;
String response = "";
try {
url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();

if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line=br.readLine()) != null) {
response+=line;
}
}
else {
response="";

}
} catch (Exception e) {
e.printStackTrace();
}

return response;
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for(Map.Entry<String, String> entry : params.entrySet()){
if (first)
first = false;
else
result.append("&");

result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}

return result.toString();
}

我没有收到任何错误,但没有返回结果,并且没有从表中删除任何行

    <?php
try{
$db_name="magazinbd";
$db_pass="";
$db_user="root";
$db_host="localhost";
$bdd = new PDO("mysql:host=$db_host;dbname=$db_name;charset=utf8",$db_user, $db_pass);
$val=$_POST['idc'];
$req=$bdd->prepare('DELETE FROM administrateur WHERE idadmin=:idcl');
$req->bindValue(":idcl",$val);
$req->execute();

if($req->rowCount())
{
$result="true";
}
elseif(!$req->rowCount())
{
$result="false";
}

echo $result;
}catch(Exception $e){
echo "erreur de connexion".$e->getmessage();
}

?>

最佳答案

$req->bindValue(":idcl",$val);

应该阅读

$req->bindValue(":idcl",$val,PDO::PARAM_INT);

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

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