Actualiser api.php

This commit is contained in:
2026-06-18 16:51:36 +02:00
parent a6249a37c7
commit c3418f0fbc
+3 -7
View File
@@ -17,8 +17,7 @@ try {
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]); ]);
// NOUVEAU : Création automatique des tables si elles n'existent pas // Création automatique des tables si elles n'existent pas
// Cela évite les erreurs SQL lors de la toute première exécution de l'application
$pdo->exec("CREATE TABLE IF NOT EXISTS users ( $pdo->exec("CREATE TABLE IF NOT EXISTS users (
id INT PRIMARY KEY, id INT PRIMARY KEY,
username VARCHAR(50) NOT NULL, username VARCHAR(50) NOT NULL,
@@ -56,7 +55,6 @@ try {
exit; exit;
} }
// CORRECTION : Protection contre les valeurs nulles
function makeStableId($title, $year) { function makeStableId($title, $year) {
$key = strtolower(trim($title ?? '')) . '|' . trim($year ?? ''); $key = strtolower(trim($title ?? '')) . '|' . trim($year ?? '');
return (abs(crc32($key)) % 2000000000) + 100000000; return (abs(crc32($key)) % 2000000000) + 100000000;
@@ -118,9 +116,7 @@ switch ($action) {
case 'save_film': case 'save_film':
checkAuth($pdo); checkAuth($pdo);
$type = $data['type'] ?? 'critique'; $type = $data['type'] ?? 'critique';
$table = ($type === 'videotheque') ? 'videotheque' : 'critiques';
// CORRECTION : Protection avec `?? ''` sur les champs title et year
$id = !empty($data['id']) ? $data['id'] : makeStableId($data['title'] ?? '', $data['year'] ?? '0000'); $id = !empty($data['id']) ? $data['id'] : makeStableId($data['title'] ?? '', $data['year'] ?? '0000');
if ($type === 'critique') { if ($type === 'critique') {
@@ -167,7 +163,6 @@ switch ($action) {
case 'bulk_delete': case 'bulk_delete':
checkAuth($pdo); checkAuth($pdo);
$ids = $data['ids'] ?? []; $ids = $data['ids'] ?? [];
// CORRECTION : Protection sur type pour éviter un warning PHP
$type = $data['type'] ?? 'critique'; $type = $data['type'] ?? 'critique';
$table = ($type === 'videotheque') ? 'videotheque' : 'critiques'; $table = ($type === 'videotheque') ? 'videotheque' : 'critiques';
@@ -208,4 +203,5 @@ switch ($action) {
http_response_code(400); http_response_code(400);
echo json_encode(["error" => "Aucun fichier reçu."]); echo json_encode(["error" => "Aucun fichier reçu."]);
} }
break; break;
}