✏️ 编辑电影
修改「= h($movie['title']) ?>」的信息
query("SELECT * FROM persons ORDER BY name"); $all_persons = $stmt->fetchAll(); $error = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $title = trim($_POST['title'] ?? ''); $info = trim($_POST['info'] ?? ''); $watch_date = $_POST['watch_date'] ?? ''; $cinema_id = $_POST['cinema_id'] ?? ''; $viewer_selects = $_POST['viewer_select'] ?? []; $viewer_news = $_POST['viewers_new'] ?? []; $poster_crop = $_POST['poster_crop'] ?? ''; $ticket_crop = $_POST['ticket_crop'] ?? ''; $photo_crops = $_POST['photo_crop'] ?? []; // 新建电影院 $new_cinema_place = trim($_POST['new_cinema_place'] ?? ''); $new_cinema_name = trim($_POST['new_cinema_name'] ?? ''); $new_cinema_address = trim($_POST['new_cinema_address'] ?? ''); if ($cinema_id === 'new' && !empty($new_cinema_name)) { $stmt = $db->prepare("INSERT INTO cinemas (place, name, address) VALUES (?, ?, ?)"); $stmt->execute([$new_cinema_place, $new_cinema_name, $new_cinema_address]); $cinema_id = $db->lastInsertId(); } elseif ($cinema_id === 'new') { $cinema_id = null; } elseif (!empty($cinema_id) && is_numeric($cinema_id)) { $cinema_id = (int)$cinema_id; } else { $cinema_id = null; } if (empty($title)) { $error = '请输入电影名称'; } elseif (empty($watch_date)) { $error = '请选择观影日期'; } else { // ----- 处理海报裁剪/上传 ----- $poster_path = $movie['poster_path']; if (!empty($poster_crop)) { $new_path = saveBase64Image($poster_crop, POSTER_DIR, 'poster'); if ($new_path) { if ($poster_path && file_exists(__DIR__ . '/' . $poster_path)) { unlink(__DIR__ . '/' . $poster_path); } $poster_path = $new_path; } else { $error = '海报裁剪保存失败,请重试'; } } elseif (isset($_FILES['poster']) && $_FILES['poster']['error'] === UPLOAD_ERR_OK) { // 上传新海报替换 $ext = strtolower(pathinfo($_FILES['poster']['name'], PATHINFO_EXTENSION)); if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'webp'])) { $filename = uniqueFilename($_FILES['poster']['name']); $target = POSTER_DIR . $filename; if (move_uploaded_file($_FILES['poster']['tmp_name'], $target)) { if ($poster_path && file_exists(__DIR__ . '/' . $poster_path)) { unlink(__DIR__ . '/' . $poster_path); } $poster_path = 'uploads/posters/' . $filename; } } } // ----- 处理票根裁剪/上传 ----- $ticket_path = $movie['ticket_path']; if (!empty($ticket_crop)) { $new_path = saveBase64Image($ticket_crop, TICKET_DIR, 'ticket'); if ($new_path) { if ($ticket_path && file_exists(__DIR__ . '/' . $ticket_path)) { unlink(__DIR__ . '/' . $ticket_path); } $ticket_path = $new_path; } else { $error = '票根裁剪保存失败,请重试'; } } elseif (isset($_FILES['ticket']) && $_FILES['ticket']['error'] === UPLOAD_ERR_OK) { $ext = strtolower(pathinfo($_FILES['ticket']['name'], PATHINFO_EXTENSION)); if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'webp'])) { $filename = uniqueFilename($_FILES['ticket']['name']); $target = TICKET_DIR . $filename; if (move_uploaded_file($_FILES['ticket']['tmp_name'], $target)) { if ($ticket_path && file_exists(__DIR__ . '/' . $ticket_path)) { unlink(__DIR__ . '/' . $ticket_path); } $ticket_path = 'uploads/tickets/' . $filename; } } } // 如果前面处理没有错误,继续更新数据库 if (empty($error)) { // 更新电影基本信息 $stmt = $db->prepare(" UPDATE movies SET title = ?, info = ?, poster_path = ?, ticket_path = ?, cinema_id = ?, watch_date = ? WHERE id = ? "); $stmt->execute([$title, $info, $poster_path, $ticket_path, $cinema_id, $watch_date, $id]); // ----- 处理合影裁剪 ----- if (!empty($photo_crops)) { foreach ($photo_crops as $photo_id => $base64_data) { if (empty($base64_data)) continue; $stmt = $db->prepare("SELECT photo_path FROM movie_photos WHERE id = ? AND movie_id = ?"); $stmt->execute([$photo_id, $id]); $old = $stmt->fetch(); if ($old) { $new_path = saveBase64Image($base64_data, PHOTO_DIR, 'photo'); if ($new_path) { if ($old['photo_path'] && file_exists(__DIR__ . '/' . $old['photo_path'])) { unlink(__DIR__ . '/' . $old['photo_path']); } $stmt = $db->prepare("UPDATE movie_photos SET photo_path = ? WHERE id = ?"); $stmt->execute([$new_path, $photo_id]); } } } } // ----- 新增合影照片 ----- if (isset($_FILES['new_photos']) && is_array($_FILES['new_photos']['tmp_name'])) { $photo_files = $_FILES['new_photos']; for ($i = 0; $i < count($photo_files['tmp_name']); $i++) { if ($photo_files['error'][$i] === UPLOAD_ERR_OK) { $ext = strtolower(pathinfo($photo_files['name'][$i], PATHINFO_EXTENSION)); if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'webp'])) { $filename = uniqueFilename($photo_files['name'][$i]); $target = PHOTO_DIR . $filename; if (move_uploaded_file($photo_files['tmp_name'][$i], $target)) { $photo_path = 'uploads/photos/' . $filename; $stmt = $db->prepare("INSERT INTO movie_photos (movie_id, photo_path) VALUES (?, ?)"); $stmt->execute([$id, $photo_path]); } } } } } // ----- 处理观影人 ----- $stmt = $db->prepare("DELETE FROM viewers WHERE movie_id = ?"); $stmt->execute([$id]); $person_ids = []; foreach ($viewer_selects as $idx => $val) { if ($val === 'new') { $new_name = trim($viewer_news[$idx] ?? ''); if (!empty($new_name)) { $chk = $db->prepare("SELECT id FROM persons WHERE name = ?"); $chk->execute([$new_name]); $existing = $chk->fetch(); if ($existing) { $person_ids[] = $existing['id']; } else { $ins = $db->prepare("INSERT INTO persons (name) VALUES (?)"); $ins->execute([$new_name]); $person_ids[] = $db->lastInsertId(); } } } elseif (is_numeric($val) && $val > 0) { $person_ids[] = (int)$val; } } $person_ids = array_unique($person_ids); if (!empty($person_ids)) { $stmt = $db->prepare("INSERT INTO viewers (movie_id, person_id, name) VALUES (?, ?, ?)"); foreach ($person_ids as $pid) { $stmt->execute([$id, $pid, '']); } } header('Location: detail.php?id=' . $id); exit; } } } // 准备观影人数据 $current_person_ids = array_column($movie['viewers'], 'person_id'); $current_person_ids = array_filter($current_person_ids, function($v) { return $v > 0; }); if (empty($current_person_ids)) { $current_person_ids = [0]; } ?>
修改「= h($movie['title']) ?>」的信息