I been trying to create a code that reduces an item's quantity by user input. But whenever I tried to enter a number above 10, it doubles its value instead of a normal subtraction. What could be the issue? Here is the php code:
$current_id = $_POST['current_id'];
$quantity = abs(intval($_POST['quantity']));
$query = "SELECT quantity FROM products WHERE pro_id = $current_id";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_array($result);
$current_quantity = $row['quantity'];
$new_quantity = $current_quantity - $quantity;
$query = "UPDATE products SET quantity = $new_quantity WHERE pro_id = $current_id";
$result = mysqli_query($con, $query);
echo $new_quantity;
?>
And here is the ajax script:
$('.quantity').on('input', function() {
let current_id = $(this).attr('id');
let quantity = $('#'+current_id).val();
if ($.trim(quantity).length == 0) {
return;
}
if(quantity.match(/^\d+$/)) {
$.ajax({
type: 'POST',
url: 'php/update_Quantity.php',
async:false,
data: {
current_id: current_id,
quantity: quantity
},
success: function(data) {
$('#quantity_'+current_id).text(data);
}
});
} else {
$('#quantity_'+current_id).text("Invalid quantity");
}
});
I tried changing the data type of the quantity column but it did not do anything.
source https://stackoverflow.com/questions/76157992/quantity-is-not-being-reduced-to-normal
No comments:
Post a Comment