0

我对 PHP 和 mySQL 很陌生,所以我真的很困惑为什么这不起作用:

$row = mysql_fetch_array($result);

$response["id"] = $row["id"];
mysql_query(" DELETE FROM balloons WHERE ID='$response['id']'");
$response["name"] = $row["name"];
$response["message"] = $row["message"];
$response["city"] = $row["city"];
$response["time"] = $row["date"];

$response["success"] = 1;

我基本上想立即删除该行。我已经尝试了很多不同的东西,我认为我的语法一定是错误的。我试着说

$todelete = $response["id"];
mysql_query(" DELETE FROM balloons WHERE ID='$todelete'");

但不好。奇怪的是,如果我说

 mysql_query(" DELETE FROM balloons WHERE ID=4");

它删除了 ID 为 4 的行就好了。

我敢肯定这是一个非常愚蠢的错误,有人可以教育我吗?

4

7 回答 7

1

检查这个

mysql_query(" DELETE FROM balloons WHERE ID='$todelete'");

将此行更改为

mysql_query(" DELETE FROM balloons WHERE ID='".$todelete."'");
于 2013-06-26T05:46:16.593 回答
1

这看起来像一个字符串格式问题。以下是一些选项:

mysql_query("DELETE FROM balloons WHERE ID = '{$row['id']}'");

mysql_query("DELETE FROM balloons WHERE ID = '" . $row['id'] . "'");

// Or try this

$todelete = $row["id"];

mysql_query("DELETE FROM balloons WHERE ID = '$todelete'");

mysql_query("DELETE FROM balloons WHERE ID = '" . $todelete . "'");

确保您的代码不易受到 SQL 注入的影响!

于 2013-06-26T05:47:06.797 回答
0

尝试这个。

mysql_query("DELETE FROM default_ps_products WHERE ID='".$todelete."'");

或者你也可以使用它

mysql_query("DELETE FROM default_ps_products WHERE ID=$todelete");

它会正常工作。希望你找到你的解决方案。

于 2013-06-26T06:05:47.850 回答
0

您是否获得了 $row['id'] 和 $result 的正确值?

你试过转储 $result 和 $row['id'] 吗?

尝试

 var_dump($result);

如果它为您提供了具有所需值的正确数组。你可以做:

 $row = mysql_fetch_array($result);

 $todelete = $row["id"];
 mysql_query(" DELETE FROM balloons WHERE ID='$todelete'");

或者您可能需要这样做:

 while($row = mysql_fetch_array()){
           $todelete = $row["id"];
           mysql_query(" DELETE FROM balloons WHERE ID='$todelete'");


  }

尝试在调试的每个步骤上执行 $result、$row、$row['id'] 等的 var_dump()。

于 2013-06-26T06:13:38.220 回答
0

首先将值存储在变量中

 $id=$response['id'];
 mysql_query("DELETE FROM balloons WHERE ID='$id'");

如果 'id' 是整数,则不需要用引号括起来。你可以这样写

 mysql_query("DELETE FROM balloons WHERE ID=$id");
于 2013-06-26T05:50:39.873 回答
0

试试这个:

$todelete = $row["id"];

$query = "DELETE FROM balloons WHERE ID='$todelete'";

于 2013-06-26T05:45:54.787 回答
0

你需要做的就是这个——

mysql_query(" DELETE FROM balloons WHERE ID=' " .$response['id']. " ' ");

或者更确切地说只是使用这个

mysql_query(" DELETE FROM balloons WHERE ID=' " .$row['id']. " ' ");

另外,我希望您检查是否将正确的结果放入 row['id']

于 2013-06-26T06:18:33.177 回答