CRUD
This commit is contained in:
61
crud/add.php
Normal file
61
crud/add.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
require_once('connect.php');
|
||||
if (isset($_POST)) {
|
||||
if (
|
||||
isset($_POST['login']) && !empty($_POST['login'])
|
||||
&& isset($_POST['password']) && !empty($_POST['password'])
|
||||
&& isset($_POST['lastname']) && !empty($_POST['lastname'])
|
||||
&& isset($_POST['firstname']) && !empty($_POST['firstname'])
|
||||
&& isset($_POST['role']) && !empty($_POST['role'])
|
||||
&& isset($_POST['description']) && !empty($_POST['description'])
|
||||
) {
|
||||
$login = strip_tags($_POST['login']);
|
||||
$password = strip_tags($_POST['password']);
|
||||
$lastname = strip_tags($_POST['lastname']);
|
||||
$role = strip_tags($_POST['role']);
|
||||
$firstname = strip_tags($_POST['firstname']);
|
||||
$description = strip_tags($_POST['description']);
|
||||
$sql = "INSERT INTO `users` (`login`, `password`, `firstname`, `lastname`, `description`, `role`, `enabled`) VALUES (:login, :password, :firstname, :lastname, :description, :role, :enabled);";
|
||||
$query = $db->prepare($sql);
|
||||
$query->bindValue(':login', $login, PDO::PARAM_STR);
|
||||
$query->bindValue(':password', $password, PDO::PARAM_STR);
|
||||
$query->bindValue(':firstname', $firstname, PDO::PARAM_STR);
|
||||
$query->bindValue(':lastname', $lastname, PDO::PARAM_STR);
|
||||
$query->bindValue(':description', $description, PDO::PARAM_STR);
|
||||
$query->bindValue(':role', $role, PDO::PARAM_INT);
|
||||
$query->bindValue(':enabled', 1, PDO::PARAM_INT);
|
||||
$query->execute();
|
||||
$_SESSION['message'] = "Utilisateur ajouté avec succès !";
|
||||
header('Location: index.php');
|
||||
}
|
||||
}
|
||||
require_once('close.php');
|
||||
?>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Ajouter utilisateur</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<form method="post">
|
||||
<label for="login">Utilisateur</label>
|
||||
<input type="text" name="login" id="login">
|
||||
<label for="password">Mot de passe</label>
|
||||
<input type="text" name="password" , id="password" />
|
||||
<label for="firstname">Prénom</label>
|
||||
<input type="text" name="firstname" id="firstname">
|
||||
<label for="lastname">Nom de famille</label>
|
||||
<input type="text" name="lastname" id="lastname" />
|
||||
<label for="role">Role</label>
|
||||
<input type="number" name="role" id="role">
|
||||
<label for="description">Description</label>
|
||||
<input type="text" name="description" id="description" />
|
||||
<button>Enregistrer</button>
|
||||
</form>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
2
crud/close.php
Normal file
2
crud/close.php
Normal file
@@ -0,0 +1,2 @@
|
||||
<?php
|
||||
$db = null;
|
||||
14
crud/connect.php
Normal file
14
crud/connect.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
try {
|
||||
|
||||
$host = "localhost";
|
||||
$user = "root";
|
||||
$password = "2487169350";
|
||||
|
||||
// Connexion à la bdd
|
||||
$db = new PDO("mysql:host=$host;dbname=cruddb", $user, $password);
|
||||
$db->exec('SET NAMES "UTF8"');
|
||||
} catch (PDOException $e) {
|
||||
echo 'Erreur : ' . $e->getMessage();
|
||||
die();
|
||||
}
|
||||
11
crud/delete.php
Normal file
11
crud/delete.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
require_once('connect.php');
|
||||
if (isset($_GET['id']) && !empty($_GET['id'])) {
|
||||
$id = strip_tags($_GET['id']);
|
||||
$sql = "DELETE FROM `users` WHERE `id`=:id;";
|
||||
$query = $db->prepare($sql);
|
||||
$query->bindValue(':id', $id, PDO::PARAM_INT);
|
||||
$query->execute();
|
||||
header('Location: index.php');
|
||||
}
|
||||
require_once('close.php');
|
||||
46
crud/details.php
Normal file
46
crud/details.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
session_start();
|
||||
// On inclut la connexion à la base
|
||||
require_once('connect.php');
|
||||
if (isset($_GET['id']) && !empty($_GET['id'])) {
|
||||
$id = strip_tags($_GET['id']);
|
||||
// On écrit notre requête
|
||||
$sql = 'SELECT * FROM `users` WHERE `id`=:id';
|
||||
// On prépare la requête
|
||||
$query = $db->prepare($sql);
|
||||
// On attache les valeurs
|
||||
$query->bindValue(':id', $id, PDO::PARAM_STR);
|
||||
// On exécute la requête
|
||||
$query->execute();
|
||||
// On stocke le résultat dans un tableau associatif
|
||||
$user = $query->fetch();
|
||||
if (!$user) {
|
||||
header('Location: index.php');
|
||||
}
|
||||
} else {
|
||||
header('Location: index.php');
|
||||
}
|
||||
require_once('close.php');
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="fr">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Liste des produits</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Détails pour l’utilisateur <?= $user['login'] ?></h1>
|
||||
<p>ID : <?= $user['id'] ?></p>
|
||||
<p>Login : <?= $user['login'] ?></p>
|
||||
<p>FirstName : <?= $user['firstname'] ?></p>
|
||||
<p>LastName : <?= $user['lastname'] ?></p>
|
||||
<p>Role : <?= $user['role'] ?></p>
|
||||
<p><a href="edit.php?id=<?= $user['id'] ?>">Modifier</a>
|
||||
<a href="delete.php?id=<?= $user['id'] ?>">Supprimer</a>
|
||||
</p>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
73
crud/edit.php
Normal file
73
crud/edit.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
require_once('connect.php');
|
||||
if (isset($_POST)) {
|
||||
if (
|
||||
isset($_POST['id']) && !empty($_POST['id'])
|
||||
&& isset($_POST['login']) && !empty($_POST['login'])
|
||||
&& isset($_POST['description']) && !empty($_POST['description'])
|
||||
&& isset($_POST['role']) && !empty($_POST['role'])
|
||||
) {
|
||||
$id = strip_tags($_GET['id']);
|
||||
$login = strip_tags($_POST['login']);
|
||||
$description = strip_tags($_POST['description']);
|
||||
$role = strip_tags($_POST['role']);
|
||||
$sql = "UPDATE `users` SET `login`=:login, `description`=:description,
|
||||
`role`=:role WHERE `id`=:id;";
|
||||
$query = $db->prepare($sql);
|
||||
$query->bindValue(':login', $login, PDO::PARAM_STR);
|
||||
$query->bindValue(':description', $description, PDO::PARAM_STR);
|
||||
$query->bindValue(':role', $role, PDO::PARAM_INT);
|
||||
$query->bindValue(':id', $id, PDO::PARAM_INT);
|
||||
$query->execute();
|
||||
header('Location: index.php');
|
||||
}
|
||||
}
|
||||
if (isset($_GET['id']) && !empty($_GET['id'])) {
|
||||
$id = strip_tags($_GET['id']);
|
||||
$sql = "SELECT * FROM `users` WHERE `id`=:id;";
|
||||
$query = $db->prepare($sql);
|
||||
$query->bindValue(':id', $id, PDO::PARAM_INT);
|
||||
$query->execute();
|
||||
$result = $query->fetch();
|
||||
}
|
||||
require_once('close.php');
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="fr">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Liste des produits</title>
|
||||
<link rel="stylesheet"
|
||||
href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
|
||||
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
|
||||
crossorigin="anonymous">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Modifier un utilisateur</h1>
|
||||
<form method="post">
|
||||
<p>
|
||||
<label for="login">Login</label>
|
||||
<input type="text" name="login" id="login" value="<?= $result['login']
|
||||
?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="description">Description</label>
|
||||
<input type="text" name="description" id="description" value="<?=
|
||||
$result['description'] ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="role">Role</label>
|
||||
<input type="number" name="role" id="role" value="<?= $result['role']
|
||||
?>">
|
||||
</p>
|
||||
<p>
|
||||
<button>Enregistrer</button>
|
||||
</p>
|
||||
<input type="hidden" name="id" value="<?= $result['id'] ?>">
|
||||
</form>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
55
crud/index.php
Normal file
55
crud/index.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
// On inclut la connexion à la base
|
||||
require_once('connect.php');
|
||||
// On écrit notre requête
|
||||
$sql = 'SELECT * FROM `users`';
|
||||
// On prépare la requête
|
||||
$query = $db->prepare($sql);
|
||||
// On exécute la requête
|
||||
$query->execute();
|
||||
// On stocke le résultat dans un tableau associatif
|
||||
$result = $query->fetchAll(PDO::FETCH_ASSOC);
|
||||
require_once('close.php');
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="fr">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Liste des utilisateurs</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Liste des utilisateurs</h1>
|
||||
<table>
|
||||
<thead>
|
||||
<th>ID</th>
|
||||
<th>Login</th>
|
||||
<th>Nom</th>
|
||||
<th>Prenom</th>
|
||||
<th>Rôle</th>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php
|
||||
foreach ($result as $user) {
|
||||
?>
|
||||
<tr>
|
||||
<td><?= $user['id'] ?></td>
|
||||
<td><?= $user['login'] ?></td>
|
||||
<td><?= $user['firstname'] ?></td>
|
||||
<td><?= $user['lastname'] ?></td>
|
||||
<td><?= $user['role'] ?></td>
|
||||
<td><a href="details.php?id=<?= $user['id'] ?>">Voir</a> <a
|
||||
href="edit.php?id=<?= $user['id'] ?>">Modifier</a> <a href="delete.php?id=<?=
|
||||
$user['id'] ?>">Supprimer</a></td>
|
||||
</tr>
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
</tbody>
|
||||
</table>
|
||||
<a href="add.php">Ajouter</a>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user