当我从表中删除记录时,表中有8列,如果输入的列数超过8,则必须显示没有删除任何内容。不过我给的每个号码都得到了相同的答案“已成功删除”。这是我的代码:
$query = "DELETE FROM `order` WHERE orderId = '$_POST[id]'" ;
$result = mysql_query($query);
if(! $result )
{
echo ("$_POST[id] not deleted !<a href='deleteorder.php'>Back</a>");
}
else
{
echo ("$_POST[id] deleted succesfully! <a href='deleteorder.php'>Back</a>");
}
答案 0 :(得分:4)
$result
将是有效的结果,即使您的查询不会影响任何行。使用mysql_affected_rows()
检查是否删除了任何内容。
$result = mysql_query($query);
if( mysql_affected_rows() == 0 )
{
echo ("$_POST[id] not deleted !<a href='deleteorder.php'>Back</a>");
}
else
{
echo ("$_POST[id] deleted succesfully! <a href='deleteorder.php'>Back</a>");
}
旁注:mysql_*
函数已弃用。不要使用它们来编写新代码,尤其是在学习时。请改用mysqli_*
或PDO。
答案 1 :(得分:2)
您可以使用mysql_affected_rows
$query = "DELETE FROM `order` WHERE orderId = {$_POST[id]}" ;
mysql_query($query);
if(0 == mysql_affected_rows()){
echo ("$_POST[id] not deleted !<a href='deleteorder.php'>Back</a>");
}else{
echo ("$_POST[id] deleted succesfully! <a href='deleteorder.php'>Back</a>");
}
答案 2 :(得分:0)