0

我有以下代码从我的数据库中检索项目并将它们显示在表格中。该文件是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>';
}

单击删除链接时(也在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);
  }
}

当我单击删除时,它显示未定义的索引:产品。请问我的错误在哪里。谢谢

4

1 回答 1

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]

于 2013-05-15T15:05:06.483 回答