【问题标题】:database design - Should I have many column about uploaded image column or I should put it in new table with many rows?数据库设计 - 我应该有很多关于上传图像列的列,还是应该把它放在有很多行的新表中?
【发布时间】:2017-04-05 22:40:02
【问题描述】:

您好,我在插入和编辑上传多个文件时遇到问题。我有 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>

【问题讨论】:

  • 每行一张图片,在一个新表中。
  • 你应该显示你的数据库表和示例数据,而不是你的 PHP 和 HTML 代码
  • 如果有一天您需要添加更多图像怎么办?您必须更改数据库中的列数。应该以“我不需要改变它”的心态来制作一个程序。创建一个新表并每行添加一个图像。您可以创建一列对它们进行编号,并且需要将它们链接到 oav_event 表。
  • 不要不要跨列展开一系列事物。这是一个很常见的问题;答案总是一样的。

标签: php mysql sql database database-design


【解决方案1】:

我认为我们都强烈建议将图像移动到新表以实现可扩展性,但是,我会让您知道您的代码有什么问题。

您将名称存储在$arr_newname 中,为此您使用array_push($arr_newname, $newname); array_push 将自动添加数字索引,这意味着如果您只上传第三张图片,当您将名称推送到$arr_newname 时,它将成为该数组的第一个元素,key = 0。

你应该保持$_FILES["images"]["name"]$arr_newname的索引之间的关系,所以你的array_push应该被这个替换:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-16
    • 2016-03-28
    • 2012-11-06
    • 2012-03-08
    相关资源
    最近更新 更多