Voici un CRUD simple en PHP utilisant MySQL (PDO).
C’est volontairement minimal pour que tu puisses l’adapter facilement.
Base de données MySQL
CREATE DATABASE crud_demo;
USE crud_demo;
CREATE TABLE clients (
id INT AUTO_INCREMENT PRIMARY KEY,
nom VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE
);
Fichier config.php (connexion à MySQL)
<?php
$host = "localhost";
$dbname = "crud_demo";
$username = "root";
$password = "";
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username,
$password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Erreur de connexion : " . $e->getMessage());
?>
create.php (ajout d’un client)
<?php
require 'config.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$nom = $_POST["nom"];
$email = $_POST["email"];
$sql = "INSERT INTO clients (nom, email) VALUES (?, ?)";
$stmt = $pdo->prepare($sql);
$stmt->execute([$nom, $email]);
header("Location: read.php");
exit();
?>
<form method="post">
Nom : <input type="text" name="nom" required><br>
Email : <input type="email" name="email" required><br>
<button type="submit">Ajouter</button>
</form>
read.php (liste des clients)
<?php
require 'config.php';
$stmt = $pdo->query("SELECT * FROM clients");
$clients = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<a href="create.php">Ajouter un client</a>
<table border="1">
<tr>
<th>ID</th><th>Nom</th><th>Email</th><th>Actions</th>
</tr>
<?php foreach ($clients as $client): ?>
<tr>
<td><?= $client['id'] ?></td>
<td><?= htmlspecialchars($client['nom']) ?></td>
<td><?= htmlspecialchars($client['email']) ?></td>
<td>
<a href="update.php?id=<?= $client['id'] ?>">Modifier</a> |
<a href="delete.php?id=<?= $client['id'] ?>" onclick="return confirm('Supprimer
?')">Supprimer</a>
</td>
</tr>
<?php endforeach; ?>
</table>
⃣ update.php (modification)
<?php
require 'config.php';
$id = $_GET['id'];
$stmt = $pdo->prepare("SELECT * FROM clients WHERE id = ?");
$stmt->execute([$id]);
$client = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$client) {
die("Client introuvable");
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$nom = $_POST["nom"];
$email = $_POST["email"];
$sql = "UPDATE clients SET nom=?, email=? WHERE id=?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$nom, $email, $id]);
header("Location: read.php");
exit();
?>
<form method="post">
Nom : <input type="text" name="nom" value="<?= htmlspecialchars($client['nom']) ?>"
required><br>
Email : <input type="email" name="email" value="<?= htmlspecialchars($client['email'])
?>" required><br>
<button type="submit">Modifier</button>
</form>
⃣ delete.php (suppression)
<?php
require 'config.php';
$id = $_GET['id'];
$sql = "DELETE FROM clients WHERE id = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$id]);
header("Location: read.php");
exit();
Points forts de ce CRUD
• Utilise PDO (plus sécurisé que mysqli avec requêtes préparées).
• HTML simple → facile à personnaliser.
• Sépare la connexion dans config.php.
• Compatible PHP 7+ et PHP 8+.