預告片海報新增功能雖然只有名稱和上傳圖片兩個欄位,但因為管理功能中還有設定顯示、排序、轉場動畫等資料要處理,因此在後端處理儲存資料時要記得別漏了需要的資料及預設值。
- 建立新增資料的表單
html
程式碼1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16<h3 class='ct'>新增預告片海報</h3>
<!--因為有上傳檔案,記得加上enctype的設定-->
<form action="./api/add_poster.php" method="post" enctype="multipart/form-data">
<table class="ts">
<tr>
<td class="ct">預告片海報</td>
<td class="ct"><input type="file" name="poster" id=""></td>
<td class="ct">預告片片名</td>
<td class="ct"><input type="text" name="name" id=""></td>
</tr>
</table>
<div class="ct">
<input type="submit" value="新增">
<input type="reset" value="重置">
</div>
</form>
- 建立
/api/add_poster.php
檔案,並撰寫表單處理程式1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23include_once "db.php";
//判斷檔案是否上傳成功
if(isset($_FILES['poster']['tmp_name'])){
//搬移上傳檔案到指定的/upload目錄下
move_uploaded_file($_FILES['poster']['tmp_name'],"../img/{$_FILES['poster']['name']}");
//在$_POST陣列中建立上傳檔案檔名
$_POST['img']=$_FILES['poster']['name'];
}
//在$_POST陣列中建立sh變數,並設定預設值為1
$_POST['sh']=1;
//依照資料表的最大id+1來做為排序的預設值
$_POST['rank']=$Poster->max('id')+1;
//在$_POST陣列中建立ani變數,使用亂數在1~3中選一個當成預設值
$_POST['ani']=rand(1,3);
//將$_POST陣列存入資料表
$Poster->save($_POST);
//返回後台的預告片海報管理頁
to("../back.php?do=poster");