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

标签 java php android mysql

我在尝试使用 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/

相关文章:

java - HttpURLConnection 到 php 但没有 mysql post?

android - 在 ImageView 上的 android 中缩放动画不流畅

android - Activity/进程生命周期 - 何时将数据保存到磁盘/从磁盘加载数据

javascript - JQuery 会干扰 bokeh.js。为什么?

PHP - 隐藏 url (GET) 参数

android - jar 中的 apklib 有什么优势?

java - 使用 swig 附加到字节数组

java - 在 Windows 下禁用 JFrame 中的默认 ALT 键操作

java - 在 JSP 中确认框调用后处理 servlet 调用时出现问题

java - 显示剩余部分而不是全部