dongtang5057 2017-06-07 06:58
浏览 231
已采纳

在MySQL数据库表的一列中存储多个图像路径

I'm able to upload multiple images to the upload folder and its path to the database but my code is creating new row in database for each image with new id (which i dont want). I want to store all the images path to a single column in database with same id and separated with comma.

here is my php code:

    <?php
    include('connection.php');
    foreach($_FILES['files']['name'] as $i => $name) {

    $name = $_FILES['files']['name'][$i];
    $size = $_FILES['files']['size'][$i];
    $type = $_FILES['files']['type'][$i];
    $tmp = $_FILES['files']['tmp_name'][$i];

    $explode = explode('.', $name);


    $ext = end($explode);

    $path = 'uploads/';
    $path = $path . basename( $explode[0] . time() .'.'. $ext);

     $sql="INSERT into pictures (FILE_NAME, FILE_SIZE, FILE_TYPE ) 
     VALUES('$name','$size','$type'); ";

    mysqli_query($conn, $sql);
    $errors = array();

    if(empty($_FILES['files']['tmp_name'][$i])) {
        $errors[] = 'Please choose at least 1 file to be uploaded.';
    }else {

        $allowed = array('jpg','jpeg','gif','bmp','png');

        $max_size = 2000000; // 2MB

        if(in_array($ext, $allowed) === false) {
            $errors[] = 'The file <b>'.$name.'</b> extension is not allowed.';
        }

        if($size > $max_size) {
            $errors[] = 'The file <b>'.$name.'</b> size is too hight.';
        }

    }

    if(empty($errors)) {

        if(!file_exists('uploads')) {
            mkdir('uploads', 0777);
        }

        if(move_uploaded_file($tmp, $path)) {
            echo '<p>The file <b>'.$name.'</b> successful upload</p>';
        }else {
            echo 'Something went wrong while uploading 
     <b>'.$name.'</b>';
        }

    }else {
        foreach($errors as $error) {
            echo '<p>'.$error.'<p>';
        }
    }

}

    ?>

database screenshot as below

enter image description here

  • 写回答

1条回答 默认 最新

  • dongyou8701 2017-06-07 07:19
    关注

    Just improve your code and also place insert query outside the foreach loop

    <?php
    include('connection.php');
    $files = [];
    foreach($_FILES['files']['name'] as $i => $name) {
    
        $name = $_FILES['files']['name'][$i];
        $size = $_FILES['files']['size'][$i];
        $type = $_FILES['files']['type'][$i];
        $tmp = $_FILES['files']['tmp_name'][$i];
    
        $explode = explode('.', $name);
    
    
        $ext = end($explode);
    
        $updatdName = $explode[0] . time() .'.'. $ext;
        $path = 'uploads/';
        $path = $path . basename( $updatdName );
    
        if(empty($_FILES['files']['tmp_name'][$i])) {
            $errors[] = 'Please choose at least 1 file to be uploaded.';
        }else {
    
            $allowed = array('jpg','jpeg','gif','bmp','png');
    
            $max_size = 2000000; // 2MB
    
            if(in_array($ext, $allowed) === false) {
                $errors[] = 'The file <b>'.$name.'</b> extension is not allowed.';
            }
    
            if($size > $max_size) {
                $errors[] = 'The file <b>'.$name.'</b> size is too hight.';
            }
    
        }
    
        if(empty($errors)) {
    
            // if there is no error then set values
            $files['file_name'][] = $updatdName;
            $files['size'][] = $size;
            $files['type'][] = $type;
            $errors = array();
            if(!file_exists('uploads')) {
                mkdir('uploads', 0777);
            }
    
            if(move_uploaded_file($tmp, $path)) {
                echo '<p>The file <b>'.$name.'</b> successful upload</p>';
            }else {
                echo 'Something went wrong while uploading 
         <b>'.$name.'</b>';
            }
    
        }else {
            foreach($errors as $error) {
                echo '<p>'.$error.'<p>';
            }
        }
    
    }
    
    if(!empty($files)) {
    
        $files['file_name'][] = $updatdName;
        $files['size'][] = $size;
        $files['type'][] = $type;
        $names = implode(',', $files['file_name']);
        $sizes = implode(',', $files['size']);
        $types = implode(',', $files['type']);
        $sql="INSERT into pictures (FILE_NAME, FILE_SIZE, FILE_TYPE ) 
             VALUES('$names','$sizes','$types'); ";
        mysqli_query($conn, $sql);
    }
    

    I think this will help you.

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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?