不删除记录

时间:2013-10-24 04:23:35

标签: php mysql sql-delete

我目前正在进行实时搜索,效果很好,当有人看到实时搜索的结果时,有三个“编辑,删除和查看”链接

当我尝试通过点击删除链接从数据库中删除其中一个产品信息时,问题是它根本没有显示任何错误甚至根本没有显示...

我显示网址末尾没有product_id编号,请看完整网址...

http://localhost/shopone/admin/live/search-save.php?deleteid=

我在代码中错过了什么?

此部分用于从搜索文本字段中获取结果。

<?php
if(isset($_POST['kw']) && $_POST['kw'] != '')
{
  $kws = $_POST['kw'];
  $kws = mysql_real_escape_string($kws); 
  $query = "select * from product where product_name like '%".$kws."%' limit 10" ;
  $res = mysql_query($query);
  $count = mysql_num_rows($res);
  $i = 0;
  if($count > 0)
  {
    while($row = mysql_fetch_array($res))
    {
    $product_id = $row["product_id"];

    //Contanter     
    echo "  <div class='container'>";

    //First columns Product Images
    echo "<div class='sidebar1'>";
    echo "<div>";?><a href='$row[$product_name]'><img src="<?php echo $row["screenshot"];?>" width="100" height="100" /></a> 
    <?php echo "</div>" ; 
    echo "</div>";

    //Second columns Product Name
    echo "<div class='content'>";
    echo $row['product_name'];"</div>";
    echo "</div>";

    //3rd columns Product Category
    echo "<div class='sidebar2'>";
    echo $row['product_category'];
    echo "</div>";

    //Fourth columns Product Retail Price
    echo "<div class='sidebar3'>";
    echo $row['product_retail_price'];
    echo "</div>";

    //Fifth columns Product Price
    echo "<div class='sidebar4'>";
    echo $row['product_price'];
    echo "</div>";
    //Sixth columns Product Price
    echo "<div class='sidebar4'>";
    echo "<a href='../data/edit_product.php?pid=' . $product_id . ''>Edit</a> &nbsp;&nbsp;
    <a href='search-save.php?deleteid=' . $product_id . ''>Delete</a>&nbsp;&nbsp;
    <a href='../data/edit-view.php?pid=' . $product_id . ''>View</a>";

    echo "</div>";

    //End of Container
    echo "</div>";

      $i++;
      if($i == 5) break;
    }
    echo "</ul>";
    if($count > 5)
    {
      echo "<div id='view_more'><a href='#'>View more results</a></div>";
    }
  }
  else
  {
    echo "<div id='no_result'>No result found !</div>";
  }
}
?>

,此部分用于按删除时删除....

<?php
  //Delete Item Question to Admin, and Delete Product if they choose
  if (isset($_GET['deleteid'])) {
     echo 'Do you really want to delete product with ID of ' . $_GET['deleteid'] . '? <a href="search-save.php?yesdelete=' . $_GET['deleteid'] . '">Yes</a> | <a href="search-save.php">No</a>';
    exit();
  }
 if (isset($_GET['yesdelete'])) {
//remove item from system and delete its picture
//delete from database
    $id_to_delete = $_GET['yesdelete'];

    $sql = mysql_query("DELETE FROM product WHERE product_id='$id_to_delete' LIMIT 1") or die (mysql_error());
/////////////////////////////////////////////////////////////
// unlink the image from server
// Remove The Pic -------------------------------------------
/////////////////////////////////////////////////////////////

    $pictodelete = ("../product_images/$id_to_delete.jpg");

    if (file_exists($pictodelete)) {
                unlink($pictodelete);
    }
}
?>

1 个答案:

答案 0 :(得分:1)

你的这一行

<a href='$row[$product_name]'>

是错误的使用php标签,而问题不是由于这个,它是由于下一行

echo "<a href='../data/edit_product.php?pid=' . $product_id . ''>Edit</a> &nbsp;&nbsp;
<a href='search-save.php?deleteid=' . $product_id . ''>Delete</a>&nbsp;&nbsp;
<a href='../data/edit-view.php?pid=' . $product_id . ''>View</a>";

当你检查你的HTML时,你会得到这个

<a href="../data/edit_product.php?pid=" .="" 2="" ''="">Edit</a> &nbsp;&nbsp;
<a href="search-save.php?deleteid=" .="" 2="" ''="">Delete</a>&nbsp;&nbsp;
<a href="../data/edit-view.php?pid=" .="" 2="" ''="">View</a>

假设产品ID为2。 所以它不会起作用。

将php代码更改为

 echo '<a href="../data/edit_product.php?pid='. $product_id . '">Edit</a> &nbsp;&nbsp;
<a href="search-save.php?deleteid=' . $product_id . '">Delete</a>&nbsp;&nbsp;
<a href="../data/edit-view.php?pid=' . $product_id . '">View</a>';