Skip to content
Snippets Groups Projects
Commit 9509fe70 authored by Sofiane Lasri's avatar Sofiane Lasri
Browse files

TP7 terminé.

parent c1c649bc
Branches
No related tags found
No related merge requests found
......@@ -127,4 +127,31 @@ class ControleurObjet
'pageContent' => "Il manque des paramètres dans l'url."
]);
}
public static function supprimerObjet(): string
{
$pageTitle = "Suppression d'un(e) {$_GET["tableName"]}";
if (!empty($_GET["tableName"]) && !empty($_GET["idName"]) && !empty($_GET["idValue"])){
$response = Objet::deleteObjectById($_GET["tableName"],
[
"name" => $_GET["idName"],
"value" => $_GET["idValue"]
]);
if($response){
return view('simple-data', [
'pageTitle' => $pageTitle,
'pageContent' => "Objet supprimé avec succès."
]);
}else{
return view('simple-data', [
'pageTitle' => $pageTitle,
'pageContent' => "Erreur lors de la suppression de l'objet."
]);
}
}
return view('simple-data', [
'pageTitle' => $pageTitle,
'pageContent' => "Il manque des paramètres dans l'url."
]);
}
}
\ No newline at end of file
......@@ -30,6 +30,7 @@ if (empty($_REQUEST["action"])) {
"lireLivres" => ControleurLivre::lireLivres(),
"describeTable" => ControleurObjet::describeTable(),
"modifierObjet" => ControleurObjet::modifyObject(),
"supprimerObjet" => ControleurObjet::supprimerObjet(),
default => ControleurAuteur::lireAuteur(),
};
} catch (Exception $e) {
......
......@@ -42,6 +42,7 @@ class Objet
public static function addObject(String $tableName, array $columns) : array
{
$result["status"] = "success";
$result["message"] = "$tableName créé avec succès !";
$validation = Objet::verifyTableAndFormat($tableName, $columns);
if($validation["status"] !== "success"){
......@@ -225,4 +226,27 @@ class Objet
"message" => "Validation réussie."
];
}
public static function deleteObjectById(string $tableName, array $id): bool
{
$validation = Objet::verifyTableAndFormat($tableName);
if($validation["status"] !== "success"){
return false;
}
if(empty($id["name"]) || empty($id["value"]) || !isAlphaNumeric($id["name"])){
return false;
}
// On prépare la requête
$queryString = ("DELETE FROM $tableName WHERE " . $id["name"] . "=?");
$insertQuery = Database::pdo()->prepare($queryString);
try{
// Et on l'exécute
$insertQuery->execute([$id["value"]]);
} catch (PDOException $e) {
echo $e->getMessage();
return false;
}
return true;
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment