简体   繁体   中英

jQuery onclick delete row from mysql table

I wrote a function to delete an item from cart; the success message flash, but the item is not getting deleted. There's no error message being flagged in the console. However I think my query is wrong.

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.'; 

Can you try this?

I think you need to append value instead.

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

Please update code as given below and try :

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

If any confusion please let me ask. Thanks...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM