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

时间:2017-06-07 06:58:44

标签: php mysql

我能够将多个图像上传到上传文件夹及其数据库路径,但我的代码是在数据库中为每个具有新ID的图像创建新行(我不想要)。我想将所有图像路径存储到数据库中具有相同ID并用逗号分隔的单个列。

这是我的PHP代码:

    <?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>';
        }
    }

}

    ?>

数据库截图如下

enter image description here

1 个答案:

答案 0 :(得分:1)

只需改进代码,并将插入查询放在foreach循环之外

<?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);
}

我认为这会对你有帮助。