I'm making a shopping list, I've already have a function to delete a single item, as of right now, I'm having a problem to delete multiple items using checkbox. My item table is consist of itemName, itemId, and listId
I'm using ajax jquery
Delete Multiple JS function
function deleteMultiple(){
$("#deleteMultiple").click(function(){
var itemsDelete = new Array();
$("input:checkbox:checked").each(function() {
itemsDelete.push( $(this).val() );
});
$.ajax({
type: "POST",
url: "deleteMultiple.php",
data: { itemsDelete : itemsDelete },
cache: false,
success: function(){
}
});
return false;
});
}
}
PHP function to spit out all the items in a specific list
$sql = ("SELECT listId FROM list WHERE listName = '$listName' LIMIT 1");
$result = mysqli_query($conn,$sql) or die(mysql_error());
while($row = mysqli_fetch_assoc($result)) {
$_SESSION['listIdIn'] = $row['listId'];
}
$sql = "(SELECT itemName FROM item WHERE listId =". $_SESSION['listIdIn'] .")";
$result = mysqli_query($conn,$sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<li><input name = 'checkbox[]' type='checkbox' value ='" . $row["itemName"]. "'>" . $row["itemName"]. "</input><input type='button' class='deleteItem' Value='Delete'/></li>";
}
} else {
return null;
}
and the PHP function to delete multiple item
$sql="SELECT * FROM item";
$result=mysqli_query($conn,$sql);
$count=mysqli_num_rows($result);
for($i=0;$i<$count;$i++){
$itemsDelete = $_REQUEST['itemsDelete'];
$sql = "DELETE FROM 'item' WHERE itemName = '$itemsDelete'";
$result = mysqli_query($conn,$sql);
}
Thank you
UPDATE:So I changed my deleteMultiple.php, here's how the code looks now
$itemsDelete = $_REQUEST['itemsDelete'];
var_dump ($itemsDelete);
for ($i = 0; $i < count($itemsDelete); $i++){
$itemsDeleteName = $itemsDelete[$i];
$sql = "DELETE FROM 'item' WHERE 'itemName' IN (//I read from some article I have to use implode, I tried but it's not working) AND 'listId' = ".$_SESSION['listIdIn'];
mysqli_query($conn,$sql);
}
The var_dump gives out this result
array(3) { [0]=> string(5) "item3" [1]=> string(5) "item4" [2]=> string(9) "iteeeemmm" }
So far, by clicking the delete button, I can pass the values into multipleDelete.php, I can pass the value into php array, the problem right now, I cannot use the value in the php array and delete MySQL database