🎬 添加新电影
记录一部你曾看过的电影
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'] ?? []; // 票根裁剪数据 $ticket_crop = $_POST['ticket_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 = ''; if (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)) { $poster_path = 'uploads/posters/' . $filename; } } } // ----- 处理票根(裁剪优先) ----- $ticket_path = ''; if (!empty($ticket_crop)) { $new_path = saveBase64Image($ticket_crop, TICKET_DIR, 'ticket'); if ($new_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)) { $ticket_path = 'uploads/tickets/' . $filename; } } } // 如果没有错误,继续 if (empty($error)) { // 插入电影 $stmt = $db->prepare(" INSERT INTO movies (title, info, poster_path, ticket_path, cinema_id, watch_date) VALUES (?, ?, ?, ?, ?, ?) "); $stmt->execute([$title, $info, $poster_path, $ticket_path, $cinema_id, $watch_date]); $movie_id = $db->lastInsertId(); // ---- 处理合影照片(多张) ---- if (isset($_FILES['photos']) && is_array($_FILES['photos']['tmp_name'])) { $photo_files = $_FILES['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; $stmt2 = $db->prepare("INSERT INTO movie_photos (movie_id, photo_path) VALUES (?, ?)"); $stmt2->execute([$movie_id, $photo_path]); } } } } } // ---- 处理观影人 ---- $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([$movie_id, $pid, '']); } } header('Location: detail.php?id=' . $movie_id); exit; } } } ?>
记录一部你曾看过的电影