:::
8-5 初步完成更新功能
- 一樣請AI來處理一下
請根據以下SQL語法來產生PHP8名為update($id)的函數: 其值來源為POST,表單欄位名稱和資料欄位名稱一致 PDO物件已建立,global引入使用即可 存入時用 ? 佔位符的方式處理。 接著根據 id 值建立 uploads/id/ 文章檔案目錄來存放圖檔 投過name為image_files[]的上傳欄位,完成多個檔案上傳至文章檔案目錄的功能, 請將原圖縮小到指定寬度,並命為 main_新檔名 並同時也產生一張更小的縮圖,並命為 thumb_新檔名 縮圖已有resizeImage(string $sourcePath, string $destPath, int $targetWidth)函數可以處理, 寬度分別為1024及320 最後,以圖檔路徑為索引,縮圖路徑為值,在原有的json內容中,加入新圖片的資訊。 並更新該文章的 media 欄位值 CREATE TABLE `school_news` ( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '編號', `news_title` VARCHAR(255) NOT NULL COMMENT '新聞標題', `news_content` TEXT NOT NULL COMMENT '新聞內容', `related_link` VARCHAR(255) COMMENT '相關連結', `publisher` VARCHAR(100) NOT NULL COMMENT '發布者', `school_name` VARCHAR(100) NOT NULL COMMENT '學校名稱', `report_date` DATE NOT NULL COMMENT '報導日期', `upload_type` ENUM('image', 'youtube') NOT NULL COMMENT '上傳種類', `youtube_url` VARCHAR(255) COMMENT 'Youtube影片網址', `media` JSON COMMENT '多檔案JSON資訊', `views` INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '點閱數', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; - AI的回覆如下,我們可以將
update()函數放至admin.php的最下方:<?php function update(int $id): bool { global $pdo; try { // 更新文章基本信息 $sql = "UPDATE school_news SET news_title = ?, news_content = ?, related_link = ?, publisher = ?, school_name = ?, report_date = ?, upload_type = ?, youtube_url = ? WHERE id = ?"; $stmt = $pdo->prepare($sql); $stmt->execute([ $_POST['news_title'], $_POST['news_content'], $_POST['related_link'] ?? null, $_POST['publisher'], $_POST['school_name'], $_POST['report_date'], $_POST['upload_type'], $_POST['youtube_url'] ?? null, $id ]); // 處理圖片上傳 if ($_POST['upload_type'] === 'image' && !empty($_FILES['image_files']['name'][0])) { $uploadDir = "uploads/{$id}/"; if (!is_dir($uploadDir)) { mkdir($uploadDir, 0777, true); } // 獲取現有的 media JSON $stmt = $pdo->prepare("SELECT media FROM school_news WHERE id = ?"); $stmt->execute([$id]); $currentMedia = json_decode($stmt->fetchColumn(), true) ?? []; foreach ($_FILES['image_files']['name'] as $key => $name) { if ($_FILES['image_files']['error'][$key] === UPLOAD_ERR_OK) { $tmpName = $_FILES['image_files']['tmp_name'][$key]; $extension = pathinfo($name, PATHINFO_EXTENSION); $newName = uniqid() . '.' . $extension; $mainImage = $uploadDir . 'main_' . $newName; $thumbImage = $uploadDir . 'thumb_' . $newName; // 移動上傳的文件 move_uploaded_file($tmpName, $mainImage); // 調整主圖大小 resizeImage($mainImage, $mainImage, 1024); // 創建縮略圖 resizeImage($mainImage, $thumbImage, 320); // 更新 media JSON $currentMedia[$mainImage] = $thumbImage; } } // 更新數據庫中的 media 字段 $updateMediaSql = "UPDATE school_news SET media = ? WHERE id = ?"; $updateMediaStmt = $pdo->prepare($updateMediaSql); $updateMediaStmt->execute([json_encode($currentMedia), $id]); } return true; } catch (PDOException $e) { error_log("Database Error: " . $e->getMessage()); return false; } catch (Exception $e) { error_log("Error: " . $e->getMessage()); return false; } } // 使用示例 try { $result = update(1); // 假設更新 ID 為 1 的新聞 if ($result) { echo "新聞更新成功"; } else { echo "新聞更新失敗"; } } catch (Exception $e) { echo "錯誤: " . $e->getMessage(); } - 修改
admin.php,加入更新的流程,一樣更新完轉向:switch ($op) { case 'update': update($id); header("location:index.php?id={$id}"); exit; ...略... } - 如此,可以試試,是否可以更新並添加圖片了。
8-4 製作編輯功能