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

时间:2017-06-28 20:39:20

标签: 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();
    }

?>

1 个答案:

答案 0 :(得分:0)

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

应该阅读

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