[英]In laravel project with many to many relation, should I put the “value of attribute” in which table of database?
[英]database design - Should I have many column about uploaded image column or I should put it in new table with many rows?
您好,我在插入和編輯上傳多個文件時遇到問題。 我有3張圖片上傳文件。 如果我在第二個或第三個上載文件中插入,則總是以第一個為准。 (但是如果我全部上傳3張圖片,它將正常工作)
現在,我的數據庫表與攝影師的活動有關。 列的外觀如下:event_id,model_name,max_photographer_qty,reserve_photographer_qty ........並且此表中的列名為“ location_img1”,“ location_img2”,“ location_img3”。
問題是我應該將其移到新表中並且在新表中只有id和一個圖像列嗎? 我的意思是,如果上傳3張圖片,那么它將在新表格中插入3行。 (我的舊表是插入1行3列)
這是我的代碼
$count=count($_FILES["images"]["name"]);
$arr_newname = array();
for($i=1; $i <= $count; $i++)
{
if ((($_FILES["images"]["type"][$i-1] == "image/gif")
|| ($_FILES["images"]["type"][$i-1] == "image/jpeg")
|| ($_FILES["images"]["type"][$i-1] == "image/png"))
&& ($_FILES["images"]["size"][$i-1] < 9000000)) //9 MB
{
if ($_FILES["images"]["error"][$i-1] > 0)
{
echo "File Error : " . $_FILES["images"]["error"][$i] . "<br />";
}
else
{
// echo "Upload File Name: " . $_FILES["images"]["name"][$i] . "<br />";
// echo "File Type: " . $_FILES["images"]["type"][$i] . "<br />";
//echo "File Size: " . ($_FILES["images"]["size"][$i] / 1024) . " Kb<br />";
if (file_exists("images/location/".$_FILES["images"]["name"][$i-1] ))
{
echo "<b>".$_FILES["images"]["name"][$i-1] . " already exists. </b>";
}
else
{
$newname = "id".$id."_".date('Y-m-d')."_".$i.".jpg"; //ใช้$id จากparameter methodเลย
array_push($arr_newname, $newname);
move_uploaded_file($_FILES["images"]["tmp_name"][$i-1] , "images/location/".$newname);
// echo "Stored in: " . "images/location/" . $_FILES["images"]["name"][$i] ."<br />";
?>
<?php
}
}
}else
{
//echo "Invalid file detail ::<br> file type ::".$_FILES["images"]["type"][$i-1] ." , file size::: ".$_FILES["images"]["size"][$i-1] ;
}
}
$data = array(
'location_pic' => $arr_newname['0'],
'location_pic2' => $arr_newname['1'],
'location_pic3' => $arr_newname['2']
);
$this->db->where('event_id', $id);
$this->db->update('oav_event', $data);
dgd
<tr>
<td><label style="color:#3a87ad; font-size:18px;">Location pic1</label></td>
<td width="30"></td>
<td><input type="file" name="images[]" size="20" /></td>
</tr>
<tr>
<td><label style="color:#3a87ad; font-size:18px;">Location pic2</label></td>
<td width="30"></td>
<td><input type="file" name="images[]" size="20" /></td>
</tr>
<tr>
<td><label style="color:#3a87ad; font-size:18px;">Location pic3</label></td>
<td width="30"></td>
<td><input type="file" name="images[]" size="20" /></td>
</tr>
我認為我們都強烈建議將圖像移到新表中以實現可伸縮性,但是,我會讓您知道代碼的問題所在。
您將名稱存儲在$arr_newname
,並且為此使用了array_push($arr_newname, $newname);
array_push將自動添加數字索引,這意味着如果您僅上傳第三張圖片,則將名稱推入$arr_newname
,它將成為該數組的第一個元素,鍵= 0。
您應該保留$_FILES["images"]["name"]
和$arr_newname
的索引之間的關系,因此您應將array_push替換為:
$arr_newname[$i-1] = $newname;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.