I create a player's inventory system in PHP and jQuery. Everything works fine, but when the player removes two items from the inventory at once, the last item is transferred to the backpack, duplicated as many times as the items were removed. I hope I have made myself clear enough. I am sending part of the code below.
Javascript:
var bri = $(this).data('id');
const unequipBtn = document.getElementById("unequipBtn_"+bri);
$(unequipBtn).on('click', function() {
$(unequipBtn).attr("disabled", "disabled");
var bri = $(this).data('bri');
var itemid = $(this).data('itemid');
var itemcat = $(this).data('itemcat');
var playerid = $(this).data('pid');
if(bri!="" && itemcat!="" && itemcat!="" && playerid!=""){
$.ajax({
url: "back/actions/unequipItem.action.php",
type: "POST",
data: {
bri: bri,
itemid: itemid,
itemcat: itemcat,
playerid: playerid
},
cache: false,
success: function(dataResult){
console.log('GIT');
}
});
//window.location.reload(true);
//$('#main').load('includes/overview.include.php');
location.replace(location.href.split('#')[0]);
}
else{
alert('Error');
}
});
PHP:
$itemID = $_POST['itemid'];
$backpackRowID = $_POST['bri'];
$itemCat = $_POST['itemcat'];
$playerID = $_POST['playerid'];
if($itemCat == 'swords'){
//usun przedmiot z ekwipunku gracza
$query1 = "
UPDATE inventory
SET invSwordID = '0'
WHERE invPlayerID = '$playerID';
";
if (mysqli_query($db, $query1)) {
echo "Pomyślnie zdjęto przedmiot z ekwipunku.<br/>";
//dodaj przedmiot do plecaka gracza
$query2 = "
INSERT INTO backpack (backpackItemID, backpackPlayerID)
VALUES ('$itemID', '$playerID')
";
if(mysqli_query($db, $query2)){
echo "Pomyślnie dodano przedmiot do plecaka.";
}
else{
echo "Nie udało się dodać przedmiotu do placaka.<br/>";
print_r($query2);
}
}
else {
echo "Nie udało się zdjąć przedmiotu z ekwipunku.";
}
}
Thank you for the replies.