corso https://vola.udemy.com/course/php-mvc-from-scratch/learn/lecture/40931984#overview
filippo.bertilotti
2024-05-24 a500c45e3bca9a83c812b8123c599c0bb6032607
metodo delete per eliminare un record nel database (parte 140)
2 files modified
19 ■■■■ changed files
src/App/Controllers/products.php 6 ●●●●● patch | view | raw | blame | history
src/Framework/Model.php 13 ●●●● patch | view | raw | blame | history
src/App/Controllers/products.php
@@ -101,6 +101,12 @@
    public function delete(string $id) {
        $product = $this->getProduct($id);
        if($_SERVER["REQUEST_METHOD"] === "POST") {
            $this->model->delete($id);
            header("Location: /products/index");
            exit;
        }
        echo $this->viewer->render("shared/header.php", [
            "title" => "Delete Product"
        ]);
src/Framework/Model.php
@@ -122,7 +122,14 @@
            return $stmt->execute();
        }
        public function delete(string $id): bool {
            $sql = "DELETE FROM {$this->getTable()} WHERE id = :id";
            $conn = $this->database->getConnection();
            $stmt = $conn->prepare($sql);
            $stmt->bindValue(":id", $id, PDO::PARAM_INT);
            return $stmt->execute();
    }
?>
    }