如何从表单上载多个图像和单个文本

时间:2019-04-30 10:08:37

标签: php mysql

我有一个必须上传多张图像的表单,但是我只将一个输入表单值插入数据库中。

if(isset($_POST['submit'])) {
    for($i = 0; $i < count($_FILES['file_upload']['name']); $i++){
      $filetmp  = $_FILES['file_upload']['tmp_name'][$i];
      $filename = $_FILES['file_upload']['name'][$i];
      $filetype = $_FILES['file_upload']['type'][$i];
      $filepath = "../images/".$filename;
      move_uploaded_file($filetmp,$filepath);

    $title        = $_POST['title'];
    $location     = $_POST['location'];
    $price        = $_POST['price'];
    $type         = $_POST['type'];
    $str = '0123456789qazxswedcvfrbgtnhymjuklio';
    $str = str_shuffle($str);
    $str = substr($str, 0, 10); 

    $sql  = "INSERT INTO pictures (";
    $sql .= "photograph_id, image, title, location, price, type";
    $sql .= ") VALUES ('";
    $sql .= $str ."', '";   
    $sql .= $filename ."', '";  
    $sql .= $title ."', '";
    $sql .= $location ."', '";  
    $sql .= $price ."', '"; 
    $sql .= $type ."')";
    $result = $database->query($sql);
        if($result){
        $session->message('<div class="btn bg-success">Picture uploaded sucessfully.</div>');
        redirect_to('picture_upload.php');
        }
}
  }
?>

请注意:每当我选择多个图像并将其插入数据库时​​,我的查询都会插入一个图像和输入详细信息,而不会插入其余图像...我对循环有些困惑。

<form action="picture_upload.php" enctype="multipart/form-data" method="post" class="form-horizontal">
<div class="form-group">
<label for="file_upload" type="hidden" name="file_upload" class="col-sm-3 control-label" >Upload Image:</label>
<div class="col-sm-8">
    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" />
    <input type="file" id="file_upload" name="file_upload[]" multiple="multiple" required class="form-control"/>
    </div>
    </div>
    <div class="form-group">
        <label for="title" class="col-sm-2 control-label" >Title:</label>
        <div class="col-sm-8">
        <input type="text" required id="title" onchange="trim(this)" class="form-control" name="title" />
           </div>
       </div>
        <div class="form-group">
        <label for="location" class="col-sm-2 control-label" >Location:</label>
        <div class="col-sm-8">
        <input type="text" required id="location" onchange="trim(this)" class="form-control" name="location" />
           </div>
       </div>
       <div class="form-group">
        <label for="price" class="col-sm-2 control-label" >Price:</label>
        <div class="col-sm-8">
        <input type="text" required id="price" onchange="trim(this)" class="form-control" name="price" />
           </div>
       </div>
       <div class="form-group">
        <select name="type" class="form-control col-sm-3 custom-select">
        <option value=""></option>
        <option value="Rent">For Rent</option>
        <option value="Sale">For Sale</option>
        </select>
       </div>  
    <div class="form-group">
       <div class="col-sm-8 col-sm-push-2">
           <button type="submit" name="submit" class="btn btn-success">Upload</button>
           </div>
       </div>
</form>

2 个答案:

答案 0 :(得分:0)

由于

cms.example.com/admin

首次插入后,如果插入成功,则重定向页面。重定向后,脚本将终止执行。

答案 1 :(得分:-1)

B001ᛦ pointed out in the comments一样,我的原始代码已存在带有重定向的循环。这意味着循环仅运行一次。

下面的代码修订版删除了循环内的重定向,将其替换为指示成功的字符串的简单回显。

if(isset($_POST['submit'])) {
    for($i = 0; $i < count($_FILES['file_upload']['name']); $i++){
      $filetmp  = $_FILES['file_upload']['tmp_name'][$i];
      $filename = $_FILES['file_upload']['name'][$i];
      $filetype = $_FILES['file_upload']['type'][$i];
      $filepath = "../images/".$filename;
      move_uploaded_file($filetmp,$filepath);

    $title        = $_POST['title'];
    $location     = $_POST['location'];
    $price        = $_POST['price'];
    $type         = $_POST['type'];
    $str = '0123456789qazxswedcvfrbgtnhymjuklio';
    $str = str_shuffle($str);
    $str = substr($str, 0, 10); 

    $sql  = "INSERT INTO pictures (";
    $sql .= "photograph_id, image, title, location, price, type";
    $sql .= ") VALUES ('";
    $sql .= $str ."', '";   
    $sql .= $filename ."', '";  
    $sql .= $title ."', '";
    $sql .= $location ."', '";  
    $sql .= $price ."', '"; 
    $sql .= $type ."')";
    $result = $database->query($sql);
        if($result){
        echo "success";
        }
}
  }
?>