dragon321723 2014-12-16 09:21
浏览 22
已采纳

在mysql中插入多个图像,也插入多行

I searched Google all over the place for an answer but I can't seem to find the right one so I'll try it here.

I want to store the users' firstname, lastname and the path of the images in the mysqli database and the image in the folder uploads. (the user can upload multiple images)

This works and is no problem.

The issue is:

When for example they type their firstname and lastname and select two images and press the upload button it uploads everything but it also creates two rows with the exact same values.

I want to store everyting in one row, see my code below:

<?php

$con = mysqli_connect('localhost','root','','img_db');
mysqli_select_db($con,'img_db');

$rn = mt_rand();

if(isset($_POST['submit'])&& isset($_FILES['image'])&& !empty($_POST['firstname'])&& !empty($_POST['lastname']))
{
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];

    for($i=0; $i<count($_FILES['image']['name']); $i++)
    {
        $tmp_name = $_FILES['image']['tmp_name'][$i];
        $max_size = 100000;
        $path = "uploads/";  
        $name = $_FILES['image']['name'][$i];
        $size = $_FILES['image']['size'][$i];
        $type = $_FILES['image']['type'][$i];

        $ext = strtolower(substr($name, strpos($name, '.') +1));
        $name = str_replace('_','',trim($name));
        $name = str_replace('-','',trim($name));
        $name = str_replace(' ','',trim($name)); 
        $name = str_replace('__','',trim($name));

        if(($ext=='jpg' || $ext=='jpeg' || $ext=='png' || $ext=='gif')&&($type=='image/jpeg' || $type=='image/png' || $type=='image/gif')&&$size<=$max_size)
        {  

            if(move_uploaded_file($_FILES['image']['tmp_name'][$i], $path.$rn.$name)) 
            { 
                mysqli_query($con,"INSERT into `img` (firstname,lastname,url) VALUES('$firstname','$lastname','$rn$name')");
            }
        }// END EXT

    }// END FOR LOOP

}// END IF ISSET POST SUBMIT

?>
  • 写回答

1条回答 默认 最新

  • douyu1990 2014-12-16 09:30
    关注

    Suggestion 1

    One solution is to store uploaded file urls in an array (declared before the for loop) and utilize the array outside of the for loop, like so:

    if (isset(/* ... */)) {
      // Outside your for loop
      $files = array();
    
      for (/* .. */) {
    
        // Doing things
    
        if (/* $ext stuff */) {
          if (move_uploaded_file(/* ... */))
          {
            $files[] = $rn.$name;
          }
        } // End EXT
      } // End FOR
    
      // Utilize $files array to determine how you want to store all of these URLs in one field.
      // Let's say you created a variable called $store with the desired representation.
      mysqli_query($con,"INSERT into `img` (firstname,lastname,url) VALUES('$firstname','$lastname','$store')");
    
    } // End ISSET
    

    Suggestion 2

    You could use a two-table structure (one table is users, one table is images) and create a foreign key relation from each image table row to a user table row via the desired user's primary key.

    See: http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度