2016-12-16 68 views
0

我写了一个函数从购物车中删除一个项目;成功消息闪烁,但该项目未被删除。控制台中没有标记错误消息。不过,我认为我的查询是错误的。jQuery onclick从mysql表中删除行

cart.php

  <form id='updateCartForm' action="update_cart.php" method="get"> 
<input name="cart_item_id" type = "hidden" id ="cart_item_id" value='<?=$product['id'];?>'> 
</form> 
    <button class="btn btn-warning" onclick='update_cart(); return false;'>&times</button> 

footer.php

function update_cart(){ 
    jQuery('#updateCartErrors').html(""); 
    var cart_item_id = jQuery('#cart_item_id').val(); 
    var error = ' '; 
var data = jQuery('#updateCartForm').serialize(); 

    jQuery.ajax({ 
    url : '/ecommerce/customer/parsers/update_cart.php ', 
    method: 'get', 
    data : data, 
    success : function(){ 
     location.reload(); 
    }, 
    error : function(){alert("Something went wrong");} 
}); 
} 

update_cart.php

<?php 
ob_start(); 
require_once $_SERVER['DOCUMENT_ROOT'].'/ecommerce/core/init.php'; 

$cart_item_id = $_GET['cart_item_id']; 
$sql = "DELETE FROM cart WHERE id = $cart_item_id" ; 


//flash success message 
$domain =($_SERVER['HTTP_HOST'] != 'localhost')?'.'.$_SERVER['HTTP_HOST']:false; 
$_SESSION['success_flash'] = $product['prod_name']. ' was deleted from your cart.'; 
+0

@ e4c5:是'$ product ['id']'由另一个查询生成以显示购物车中的产品。 – Chad

+0

您是否检查该表单是否正确呈现,但是检查了html – e4c5

+0

这些'

Sean

回答

0

下面给出的请更新代码,并尝试:

$sql = "DELETE FROM cart WHERE id =" . $cart_item_id; // update this 
$db->query($sql); //add this new line in code. 

如果有任何困惑,请让我问。 谢谢...

+0

- [*当双引号或heredoc指定字符串时,变量将在其中解析*](http ://php.net/manual/en/language.types.string.php#language.types.string.parsing)。所以你的查询等同于OP。 – Sean

+0

如果您要建议更改查询,您的'$ db->查询($ sql);'是正确的,但 – Sean

+1

,建议应该是使用预准备语句。 – e4c5

0

你能试试吗?

我认为你需要追加值。

 $sql = "DELETE FROM cart WHERE id =".$cart_item_id; 
+0

from docs - [*当一个字符串用双引号或heredoc指定时,变量在其中被解析*](http://php.net/manual/en/language.types.string.php#language.types .string.parsing)。所以你的查询等同于OP。 – Sean

+1

如果您打算建议更改查询,则建议应该是使用来自docs的预准备语句 – e4c5