tuchawat tuchawat - 11 days ago 6
SQL Question

database design - Should I have many column about uploaded image column or I should put it in new table with many rows?

Hello I have a problem with insert and editing uploading multiple files. I have 3 images upload files.
if I insert in the second or third upload file, it will always result in first. (but if I upload all 3 images, it will work fine)

Now My database table is about photographer's event. Column look like this
event_id, model_name, max_photographer_qty, reserve_photographer_qty,.......
and I have column name "location_img1" "location_img2" "location_img3" in this table.

The question is Should I move this to new table and in new table has only id and one image column? I mean if upload 3 images then it will insert 3 rows in new table. (my old table is insert 1 row with 3 column)

here is my code

$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>

Answer

I think we all strongly recommend moving the images to a new table for scalability, however, I will let you know what is wrong with your code.

You are storing the names in $arr_newname, and for that you are using array_push($arr_newname, $newname); array_push will add numeric index automatically, meaning if you only upload the 3rd image, when you push the name to $arr_newname, it will become the first element of that array, with key = 0.

You should keep the relation between the index of $_FILES["images"]["name"] and $arr_newname, so you array_push should be replaced by this:

$arr_newname[$i-1] = $newname;
Comments