Here is the code for the class:
class Delete_Category extends Category {
private $idchain = array();
public function __construct($id) {
parent::__construct();
$this->check_id($id);
$this->get_delete_ids($this->id);
$this->delete_category($this->idchain);
}
// Get all the Children IDs from the DB and store them in the array
private function get_delete_ids($id) {
$this->query = $this->db->prepare("SELECT id FROM `shop_categories` WHERE parent_id = :id");
$this->query->execute(array("id" => $id));
while($result = $this->query->fetch(PDO::FETCH_ASSOC)) {
$this->get_delete_ids($result['id']);
}
$this->idchain[]= $id;
}
// Implode the array into an id string and throw it in the query
private function delete_category($id_array) {
$id = implode(",",$id_array);
try {
$this->query = $this->db->prepare("DELETE FROM `shop_categories` WHERE id IN (:id)");
$this->query->execute(array(':id' => $id));
}
catch(PDOException $e) {
// log it{
}
}
}
The thing is that this always ends up with only the last ID being deleted. The query seems to be working however because it looks totaly fine if i echo it and replace :id with $id.
// SQL output string if echoed:
DELETE FROM `shop_categories` WHERE id IN (11,6)
// If i manually add this to the Database it works as intended so the problem has to be somewhere at the PDO statement… Can anyone help me?
You can use
FIND_IN_SETfor that: