在PHP中删除MySQL行的问题

时间:2013-06-26 05:43:53

标签: php mysql

我是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的行就好了。

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

7 个答案:

答案 0 :(得分:1)

检查这个

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

将此行更改为

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

答案 1 :(得分: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注入的影响!

答案 2 :(得分:0)

试试这个:

$todelete = $row["id"];

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

答案 3 :(得分:0)

将值存储在变量中

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

如果'id'是一个整数,则不需要保留引号。你可以写这样的

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

答案 4 :(得分:0)

试试这个。

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

或者您也可以使用

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

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

答案 5 :(得分: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'");


  }

尝试对DEBUG的每个步骤执行$ result,$ row,$ row ['id']等的var_dump()。

答案 6 :(得分:0)

您需要做的就是这个 -

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

或者更确切地说,只需使用此

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

另外,我希望你检查一下你是否在行['id']

中得到了正确的结果