0% found this document useful (0 votes)
83 views8 pages

Crud PHP Poo + Mysql

The document outlines an enhanced PHP Object-Oriented Programming (OOP) CRUD application using MySQL, featuring search functionality, pagination, and Bootstrap alerts. It consists of three main files: db.php for database connection, Product.php for CRUD operations, and index.php for the web interface. The application allows users to create, read, update, and delete products while providing a user-friendly interface with search and pagination capabilities.

Uploaded by

moisepr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views8 pages

Crud PHP Poo + Mysql

The document outlines an enhanced PHP Object-Oriented Programming (OOP) CRUD application using MySQL, featuring search functionality, pagination, and Bootstrap alerts. It consists of three main files: db.php for database connection, Product.php for CRUD operations, and index.php for the web interface. The application allows users to create, read, update, and delete products while providing a user-friendly interface with search and pagination capabilities.

Uploaded by

moisepr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Je vais te préparer la version améliorée du CRUD PHP POO + MySQL avec :

• Recherche

• Pagination

• Alertes Bootstrap

• Trois fichiers principaux :

1. [Link] → connexion PDO

2. [Link] → classe POO pour gérer les opérations CRUD

3. [Link] → interface web avec Bootstrap, recherche et pagination

[Link] — Connexion à la base

<?php

class Database {

private $host = "localhost";

private $db_name = "test_db";

private $username = "root";

private $password = "";

public $conn;

public function getConnection() {

$this->conn = null;

try {

$this->conn = new PDO(

"mysql:host=" . $this->host . ";dbname=" . $this->db_name,

$this->username,

$this->password

);

$this->conn->exec("SET NAMES utf8");

} catch(PDOException $exception) {
echo "Erreur de connexion : " . $exception->getMessage();

return $this->conn;

?>

⃣ [Link] — Classe CRUD

<?php

class Product {

private $conn;

private $table = "products";

public $id;

public $name;

public $price;

public function __construct($db) {

$this->conn = $db;

// Récupérer avec recherche + pagination

public function read($search = "", $start = 0, $limit = 5) {

$query = "SELECT * FROM " . $this->table . "

WHERE name LIKE :search

ORDER BY id DESC

LIMIT :start, :limit";

$stmt = $this->conn->prepare($query);
$stmt->bindValue(":search", "%$search%", PDO::PARAM_STR);

$stmt->bindValue(":start", (int)$start, PDO::PARAM_INT);

$stmt->bindValue(":limit", (int)$limit, PDO::PARAM_INT);

$stmt->execute();

return $stmt;

public function count($search = "") {

$query = "SELECT COUNT(*) as total FROM " . $this->table . " WHERE name LIKE
:search";

$stmt = $this->conn->prepare($query);

$stmt->bindValue(":search", "%$search%", PDO::PARAM_STR);

$stmt->execute();

$row = $stmt->fetch(PDO::FETCH_ASSOC);

return $row['total'];

public function create() {

$query = "INSERT INTO " . $this->table . " (name, price) VALUES (:name, :price)";

$stmt = $this->conn->prepare($query);

return $stmt->execute(["name" => $this->name, "price" => $this->price]);

public function update() {

$query = "UPDATE " . $this->table . " SET name=:name, price=:price WHERE id=:id";

$stmt = $this->conn->prepare($query);

return $stmt->execute(["name" => $this->name, "price" => $this->price, "id" => $this-
>id]);

}
public function delete() {

$query = "DELETE FROM " . $this->table . " WHERE id=:id";

$stmt = $this->conn->prepare($query);

return $stmt->execute(["id" => $this->id]);

?>

⃣ [Link] — Interface Web

<?php

require '[Link]';

require '[Link]';

$database = new Database();

$db = $database->getConnection();

$product = new Product($db);

$alert = "";

$search = $_GET['search'] ?? "";

$page = $_GET['page'] ?? 1;

$limit = 5;

$start = ($page - 1) * $limit;

// Ajouter

if (isset($_POST['create'])) {

$product->name = $_POST['name'];

$product->price = $_POST['price'];
if ($product->create()) {

$alert = '<div class="alert alert-success">Produit ajouté avec succès</div>';

// Modifier

if (isset($_POST['update'])) {

$product->id = $_POST['id'];

$product->name = $_POST['name'];

$product->price = $_POST['price'];

if ($product->update()) {

$alert = '<div class="alert alert-info">Produit modifié</div>';

// Supprimer

if (isset($_GET['delete'])) {

$product->id = $_GET['delete'];

if ($product->delete()) {

$alert = '<div class="alert alert-danger">Produit supprimé</div>';

$total = $product->count($search);

$stmt = $product->read($search, $start, $limit);

?>

<!DOCTYPE html>
<html lang="fr">

<head>

<meta charset="UTF-8">

<title>CRUD PHP POO</title>

<link rel="stylesheet"
href="[Link]

</head>

<body class="p-4">

<div class="container">

<h1 class="mb-4">Gestion des Produits</h1>

<?= $alert ?>

<!-- Formulaire Ajout -->

<form method="POST" class="mb-4 row g-2">

<div class="col-md-4">

<input type="text" name="name" placeholder="Nom" class="form-control" required>

</div>

<div class="col-md-3">

<input type="number" step="0.01" name="price" placeholder="Prix" class="form-


control" required>

</div>

<div class="col-md-2">

<button type="submit" name="create" class="btn btn-success w-


100">Ajouter</button>

</div>

</form>

<!-- Recherche -->


<form method="GET" class="mb-3">

<input type="text" name="search" placeholder="Rechercher..." value="<?=


htmlspecialchars($search) ?>" class="form-control">

</form>

<!-- Table -->

<table class="table table-bordered table-striped">

<tr>

<th>ID</th>

<th>Nom</th>

<th>Prix</th>

<th>Action</th>

</tr>

<?php while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { ?>

<tr>

<td><?= $row['id'] ?></td>

<td><?= htmlspecialchars($row['name']) ?></td>

<td><?= number_format($row['price'], 2) ?></td>

<td>

<a href="?delete=<?= $row['id'] ?>" class="btn btn-danger btn-sm" onclick="return


confirm('Supprimer ?')">Supprimer</a>

</td>

</tr>

<?php } ?>

</table>

<!-- Pagination -->

<nav>

<ul class="pagination">
<?php for ($i = 1; $i <= ceil($total / $limit); $i++) { ?>

<li class="page-item <?= $i == $page ? 'active' : '' ?>">

<a class="page-link" href="?page=<?= $i ?>&search=<?= urlencode($search)


?>"><?= $i ?></a>

</li>

<?php } ?>

</ul>

</nav>

</div>

</body>

</html>

You might also like