程式碼的第一行if($_FILES)判斷使用者是否有上傳檔案
有設定過 if($_FILES != "null"),if($_FILES != null),if($_FILES !="")
亦設定過$_FILES['newsimg'][name],$_FILES['newsimg'][tmp_name]
以下是我的upload.php
if($_FILES){
$sql = "select no from news where title_tw='$title_tw' or
title_ch='$title_ch' order by no desc;";
$result = mysqli_fetch_array(mysqli_query($con, $sql));
$newsid = $result['no'];
$num = count($_FILES['newsimg']['name']);
for($i = 0 ; $i < $num ; $i++){
$imgname = $_FILES['newsimg']['name'][$i];
$patch = "../images/news/";
$sql = "insert into news_images (newsid,imgname,imgurl) values
('$newsid','$imgname','$patch')";
if(mysqli_query($con, $sql) and
move_uploaded_file($_FILES['newsimg']['tmp_name'][$i],
$patch.$_FILES['newsimg']['name'][$i]))
echo $imgname."上傳完畢。<br />";
}
echo "圖片均以上傳成功。<br />";
}
另外這個表單接收文字訊息與上傳檔案
程式碼中我先儲存接收到的文字資料到news,在抓出該筆資料id
接著將id給要儲存圖片的news_images
感覺步驟不精簡,query了好多次
請問是否有更好的辦法取代這樣的方式呢?