如何将包含多个图像的表单数据上传到数据库MYSQL

时间:2017-03-22 14:39:34

标签: php html mysql forms

我有一个表单,我想插入一些值并将多个图像上传到一个文件夹并将所有数据存储到数据库mysql中,但是,目前,我的代码上传并保存图像,但不是其余的表单数据。

让我们看一个屏幕:

enter image description here

因此,您可以看到除图像字段

外所有表单字段都是空的

这是我的HTML表单代码:

       <head>
<script src="ckeditor/ckeditor.js"></script>

</head>

<form action="uploadall.php" enctype="multipart/form-data" method="post">

<div class="form-group">
      <label for="name">Nome:</label>
      <input type="text" name="name" class="form-control" id="name" placeholder="Inserisci Nome" />
    </div>
    <div class="form-group">
      <label for="author">Autore:</label>
      <input type="text" name="author" class="form-control" id="author" placeholder="Inserisci Paese/Nazione" />
    </div>

  <div class="form-group">
      <label for="description">description:</label>
      <textarea id="editordescription" name="description" cols="45" rows="15" class="form-control" id="description" placeholder="Inserisci Messaggio">
        </textarea>
        <script>
            CKEDITOR.replace( 'editordescription' );
        </script>
    </div>
    <div class="form-group">
      <label for="misure">Misure:</label>
      <input type="text" name="misure" class="form-control" id="misure" placeholder="Inserisci Paese/Nazione" />
    </div>
     <div class="form-group">
      <label for="date">Data:</label>
      <input type="website" name="date" class="form-control" id="date" placeholder="Inserisci data" />
    </div>
    <input type="hidden" name="status" value="Disattivo" size="20">



    <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>

这是我的PHP代码:

<?php
if(isset($_POST['submit'])){






    if(count($_FILES['upload']['name']) > 0){
        //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

                    // get form data, making sure it is valid
 $name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
 $author = mysql_real_escape_string(htmlspecialchars($_POST['author']));
  $description = mysql_real_escape_string(htmlspecialchars($_POST['description']));
    $misure = mysql_real_escape_string(htmlspecialchars($_POST['misure']));

 $date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
  $status = mysql_real_escape_string(htmlspecialchars($_POST['status']));

                    $servername = "xxxxx";
$username = "xxxx";
$password = "xxxxx";
$dbname = "xxxxx";

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


$sql = "INSERT INTO exposition (name, author, description, misure, nameimage, pathimage, date, status)
VALUES ('$name', '$author','$description', '$misure', '$shortname', '$filePath', '$date', '$status');";


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

$conn->close();




                }
              }
        }
    }

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

?>

提前致谢

0 个答案:

没有答案