错误使用确认框从数据库中删除项目

时间:2013-05-15 14:52:55

标签: php javascript

我有以下代码从我的数据库中检索项目并将其显示在表格中。该文件是manage-products.php。

while($row = mysql_fetch_row($result)){
       echo '</tr>';
       echo ' <td class="product"><a href="manage-products-2.php">'.$row[1].'</a></td>';
       echo'<td class="quantity">'.$row[5].'</td>';
       echo '<td class="item_price">'.$row[4].'</td>';
       echo '<td class="item_total">'.$row[6].'</td>';
       echo '<td class="item_unsold"><a href = "manage-products.php?prod = '.$row[0].'" style="color:red">Delete</a></td>';//to delete an item
       echo '</tr>';
}

单击DELETE链接时,我有以下代码(应该执行)(同样在manage-products.php中)

$prodid = $_GET['prod'];

if($prodid != ""){
  echo '<script type="text/javascript"> 
  var r = window.confirm("Are you sure you want to delete this product") ;
  if(r == true){
    $ . post ( "manage-products.php" ,  { result : r });
  }
  </script>';
  $delete = $_POST['result'];
  if($delete == true){
    $SQL1 = "DELETE FROM tbl_product WHERE id = '$prodid'";
    $result1 = mysql_query($SQL1);
  }
}

当我点击删除时,它表示未定义索引:prod。请问我的错误在哪里。感谢

1 个答案:

答案 0 :(得分:0)

重新加载前进行确认,并在重新加载时删除产品,如果发生这种情况,您的性能会更高。

在删除链接上这样:

echo '<td class="item_unsold"><a href = "manage-products.php?prod='.$row[0].'" style="color:red" onclick="return confirm('Are you sure you want to delete this product ?')">Delete</a></td>';//to delete an item

然后重新加载:

$prodid = $_GET['prod'];

if($prodid != "" && is_numeric($prodid)){
    $SQL1 = "DELETE FROM tbl_product WHERE id = '$prodid'";
    $result1 = mysql_query($SQL1);
  }
}

在删除之前不要忘记清理变量(检查数字类型);)

编辑:

manage-products.php?prod = '.$row[0]替换为manage-products.php?prod='.$row[0]