嗨,我正在尝试使用where子句从MYSQL数据库中的表中删除一条记录。 到目前为止,这是我所能得到的,但是没有用,我不确定如何去做。有没有办法使这项工作?我已经包括了我的删除方法和php文件代码。
我的网址-
deleteCompletedGoal=("http://10.0.2.2/deleteCompletedGoalAddress.php?user_goal_id="+completed_goalID);
我的代码-
private void deleteNonActiveGoal(){
try {
URL url = new URL(deleteCompletedGoal);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setRequestProperty("X-HTTP-Method-Override", "DELETE");
http.setDoInput(true);
http.setDoOutput(true);
OutputStream ops = http.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(ops, "UTF-8"));
String data = URLEncoder.encode("user_goal_id", "UTF-8") + "=" + URLEncoder.encode(completed_goalID, "UTF-8") + "&&";
writer.write(data);
writer.flush();
writer.close();
ops.close();
InputStream ips = http.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(ips, "ISO-8859-1"));
String line;
while ((line = reader.readLine()) != null) {
result += line;
}
reader.close();
ips.close();
http.disconnect();
}
catch (MalformedURLException e) {
result = e.getMessage();
} catch (IOException e) {
result = e.getMessage();
}
}
PHP文件:
<?php
require "connection.php";
$completed_goalID=$_POST["user_goal_id"];
$mysql_qry = "DELETE from user_goals WHERE user_goal_id ='$completed_goalID'";
if($conn->query($mysql_qry) === TRUE) {
echo "delete successful";
}
else{
echo "delete failed";
}
$conn->close();
?>
答案 0 :(得分:0)
由于要在查询字符串中发送变量,因此将使用GET而不是POST。更改:
$completed_goalID=$_POST["user_goal_id"];
到
$completed_goalID=$_GET["user_goal_id"];
警告
Little Bobby说 your script is at risk for SQL Injection Attacks. 了解有关prepared的MySQLi语句。甚至escaping the string都不安全!
答案 1 :(得分:0)
使用$ _GET作为来自URL的catch变量,例如:
$completed_goalID=$_GET["user_goal_id"];
更改查询以防止sql攻击(Reference),例如:
<?php
require "connection.php";
$completed_goalID=$_POST["user_goal_id"];
$mysql_qry = $conn->prepare("DELETE from user_goals WHERE user_goal_id=?");
$mysql_qry->bind_param('i',$completed_goalID);
if($mysql_qry->execute() === TRUE){
echo "delete successful";
}
else{
echo "delete failed";
}
$mysql_qry->close();
$conn->close();
?>