Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在一个记录PHP MySql中上载多个图像_Php_Mysql_File Upload - Fatal编程技术网

在一个记录PHP MySql中上载多个图像

在一个记录PHP MySql中上载多个图像,php,mysql,file-upload,Php,Mysql,File Upload,我有一个工作多个图像上传(最多5个图像),我插入到多个记录他们。我现在的目标是将它们插入一个记录中。我将用代码和屏幕来解释它: form.html <form action="imagesupload.php" enctype="multipart/form-data" method="post"> Codice Riferimento: <input type="text" name="codrif" value="<?php echo $codrif; ?&g

我有一个工作多个图像上传(最多5个图像),我插入到多个记录他们。我现在的目标是将它们插入一个记录中。我将用代码和屏幕来解释它:

form.html

    <form action="imagesupload.php" enctype="multipart/form-data" method="post">
Codice Riferimento: <input type="text" name="codrif" value="<?php echo $codrif; ?>"/><br>


    <div>
        <label for='upload'>Add Attachments:</label>
        <input id='upload' name="upload[]" type="file" multiple="multiple" />
    </div>

    <p><input type="submit" name="submit" value="Submit"></p>

</form>


Codice Riferimento:阅读关于规范化和一对多关系的内容是可能的,但是在关系数据库中你不会这样做。研究规范化。你也可以使用json类型将图像数据存储在键值对中。@James69:这不是一个好的理由。这可能是个不好的理由。正如其他人所说:您应该尝试规范化您的表。如果您不知道这意味着什么,请阅读数据库规范化。成千上万的人贡献了这一知识:这是最好的方法。
    <?php
if(isset($_POST['submit'])){
        $codrif = $_POST['codrif'];


    if(count($_FILES['upload']['name']) > 0 && count($_FILES['upload']['name']) < 6){
        //Loop through each file
        for($i=0; $i<count($_FILES['upload']['name']); $i++) {
          //Get the temp file path
            $tmpFilePath = $_FILES['upload']['tmp_name'][$i];

            //Make sure we have a filepath
            if($tmpFilePath != ""){

                //save the filename
                $shortname = $_FILES['upload']['name'][$i];

                //save the url and the file
                $filePath = "uploads/" . date('d-m-Y-H-i-s').'-'.$_FILES['upload']['name'][$i];

                //Upload the file into the temp dir
                if(move_uploaded_file($tmpFilePath, $filePath)) {

                    $files[] = $shortname;
                    //insert into db 
                    //use $shortname for the filename
                    //use $filePath for the relative url to the file

$servername = "xxxxxxxxxxxxxxx";
$username = "xxxxxxxxxxxxxxx";
$password = "xxxxxxxxxxxxxxx";
$dbname = "xxxxxxxxxxxxxxxx";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 


$sql = "INSERT INTO uploads (codrif, name, imagesname, description)
VALUES ('$codrif', '$name', '$shortname', '$filePath');";


if ($conn->multi_query($sql) === TRUE) {
    echo "New records created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();




                }
              }
        }
    }else{
        echo "Error!";    


    }

    //show success message
    echo "<h1>Uploaded:</h1>";    
    if(is_array($files)){
        echo "<ul>";
        foreach($files as $file){
            echo "<li>$file</li>";

        }
        echo "</ul>";
    }
}
?>