每行的PHP / SQL删除按钮

时间:2014-01-10 15:23:18

标签: javascript php html

好的,所以我有一个SELECT

$filme_cart = mysql_query("SELECT name, price FROM cart_test GROUP BY name");

然后我有一个看起来像这样的PHP代码:

<?php                                                                   
  while($film_cart=mysql_fetch_object($filme_cart))
  {                                                        
   echo "<tr>";                                                               
   echo "<td align='left'>";                                                                 
   echo $film_cart->name;                                                               
   echo "</td>";                                                            
   echo "<td class='cart-product-setting'>";                                                            
   echo $film_cart->price;                                                                
   echo "<a href='delete.php?delete_film=".$film_cart['id_film']."' class='remove-pro' rel='tooltip' data-title='Delete'><i class='icon-trash'></i></a></td>";                                                              
   echo "</tr>";
} 
?>

我的delete.php文件如下所示:

<?php  
 include 'config.php';
 $delete_film=$_GET['id_film']; 
 $delete_cart = mysql_query("DELETE FROM cart_test WHERE id_film='$delete_film'");
?>

因此,当我将PHP代码放入我的网页时,我的<body>消失了。

我做错了什么?

3 个答案:

答案 0 :(得分:1)

您的get参数名为delete_film NOT id_film

所以修好这一行

$delete_film=$_GET['id_film'];

这一个

$delete_film=$_GET['delete_film'];

答案 1 :(得分:1)

你应该使用mysqli或pdo
你错过了3分:

修复您的查询: 1)确保您已选择所有必填字段。

$filme_cart = mysql_query("SELECT * FROM cart_test GROUP BY name");

2)尝试使用:mysql_fetch_assoc

  <?php
    while($film_cart=mysql_fetch_assoc($filme_cart))
      {
      echo "<tr>";  
      echo "<td align='left'>";
      echo $film_cart['name'];
      echo "</td>";
      echo "<td class='cart-product-setting'>";
      echo $film_cart['price'];
      echo "<a href='delete.php?delete_film=".$film_cart['id_film']."' class='remove-pro' rel='tooltip' data-title='Delete'><i class='icon-trash'></i></a></td>";  
      echo "</tr>";
    } 
    ?>

然后: 3)$_GET['delete_film']

获取价值
<?php

include 'config.php';

$delete_film=$_GET['delete_film'];

$delete_cart = mysql_query("DELETE FROM cart_test WHERE id_film=$delete_film limit 1");

?>

答案 2 :(得分:0)

1 - 您需要从db中选择id_film     mysql_query("SELECT id_film, name, price FROM cart_test GROUP BY name");
2 - 您正在使用mysql_fetch_assoc来获取数据,因此您应该回显如下数据:
    echo $film_cart['name'];
不喜欢:
    echo $film_cart->name;
3 - 你的get参数叫做delete_film NOT id_film,所以得到的值是:
    $delete_film=$_GET['delete_film'];
4 - 在PHP 5.5中不推荐使用mysql,你应该使用pdo或mysqli
祝你好运