简体   繁体   English

在PHP MYSQL中将多个文件插入/上传到数据库表中的特定行

[英]Inserting/ Uploading Multiple Files to a specific row in a database table in PHP MYSQL

Problem: Only the last file upload inserted to the row (listing-amenities-safety-photos-id), which is supposed to be the entire photos selected during the upload. 问题:只有上次上传的文件插入到行(listing-amenities-safety-photos-id),该行应该是上载期间选择的全部照片。

HTML: HTML:

    <form action="#" method="post" enctype="multipart/form-data">
        <input name="upload[]" type="file" multiple="multiple" />
        <input type="submit" name="submit" value="UPLOAD" />
    </form>

PHP: PHP:

    <?php
if(isset($_POST['submit'])) {
    for ($i=0; $i<count($_FILES['upload']['name']); $i++) {
        $path = "../media/images/Listings/Set/";
        $ext = explode('.', basename( $_FILES['upload']['name'][$i]));
        $path = $path . md5(uniqid()) . "." . $ext[count($ext)-1]; 
        move_uploaded_file($_FILES['upload']['tmp_name'][$i], $path);           

    }           

    $quotequery = "INSERT INTO `listing-details` (`listing-amenities-safety-photos-id`) VALUES ('$path')";
    if (mysqli_query($conn, $quotequery) == TRUE) {

        echo "File uploaded.";
    } else {
        echo "Upload failed.". mysqli_error($conn);
    }

}

?>

Photo to show only one entry is inserted: Image as proof that only one entry from $path is inserted 显示仅插入了一个条目的照片: 用作证明仅插入了$ path中的一个条目的图像

Note: All photos were uploaded successfully to the designated path. 注意:所有照片均已成功上传到指定路径。 However, I can't get mysql to insert all the files in my row. 但是,我无法让mysql在行中插入所有文件。 I'm thinking like an array might solve my problem, but I'm not really an array expert, or even PHP! 我在想像数组可以解决我的问题,但是我并不是数组专家,甚至不是PHP! So please, any answer that is helpful but "marked as duplicate" or "down vote" will be appreciated. 因此,请回答任何有用但“标记为重复”或“否决”的答案。 Thanks 谢谢

Move your database query inside your of loop. 在循环中移动数据库查询。 $path gets overwritten on each pass, so as it stands, it will only insert the last one. $path在每次通过时都将被覆盖,因此按它的立场,它只会插入最后一个。

<?php
if(isset($_POST['submit'])) {
    for ($i=0; $i<count($_FILES['upload']['name']); $i++) {
        $path = "../media/images/Listings/Set/";
        $ext = explode('.', basename( $_FILES['upload']['name'][$i]));
        $path = $path . md5(uniqid()) . "." . $ext[count($ext)-1];
        move_uploaded_file($_FILES['upload']['tmp_name'][$i], $path);

        $quotequery = "INSERT INTO `listing-details` (`listing-amenities-safety-photos-id`) VALUES ('$path')";
        if (mysqli_query($conn, $quotequery) == TRUE) {

            echo "File uploaded.";
        } else {
            echo "Upload failed.". mysqli_error($conn);
        }
    }
}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM