从DB编辑多个图像

时间:2018-02-21 12:01:15

标签: php mysql pdo

我试图从数据库编辑图像记录。数据提取部分没问题,但我的编辑代码无效。我使用插入部分修改此代码。插入到数据库正在工作,但编辑不起作用。我是php的新手。我想为我的分类广告项目添加包含表单数据的多个图像。

在编辑页面上......

<input type="file" name="upload[]" onchange="previewFiles()" multiple />

在编辑处理页面....

<?php                       

  $file_dir  = "../images/";


  if (!file_exists($file_dir)) {
    mkdir($file_dir, 0777, true);
  }

  if (isset($_POST["Input"])) {


    function diverse_array($array) { 
      $result = []; 
      foreach($array as $key1 => $value1) 
        foreach($value1 as $key2 => $value2) 
          $result[$key2][$key1] = $value2; 

      return $result; 
    }

    $file_multi  = diverse_array($_FILES['upload']);

    for ($y = 0; $y < 4; $y++) {    
      $image[$y] = '';
    }


    for ($x = 0; $x < count($file_multi); $x++) {


      foreach ($file_multi[$x] as $key => $value) {

        $file_name   = $file_multi[$x]['name'];
        $file_size   = $file_multi[$x]['size'];
        $file_tmp    = $file_multi[$x]['tmp_name'];

        $file_target = $file_dir . $file_name;

        $errors = array();  


        $file_secure  = array('jpg', 'jpeg', 'png', 'gif', 'bmp');                  
        $file_current = strtolower(pathinfo($file_name, PATHINFO_EXTENSION)); /* (end(explode('.', $file_name) */

        if (in_array($file_current, $file_secure) === false) {
          $errors[] = "Sorry, <strong>{$file_current}</strong> extension not allowed";      
        }

      }


      if (!empty($errors)) {                            


        foreach ($errors as $keyError => $valueError) {
          echo "$keyError = $valueError <br />";
        }                           

      } else {

        if (move_uploaded_file($file_tmp, $file_target)) {

          $image[$x] = $file_target;

          echo "<strong>{$file_name}</strong> has been uploaded." . "<br />";

        } else {

          echo "Sorry, there was an something wrong in <b>move_uploaded</b>.";

        }

      }                     

    }                   

    if (!empty($errors)) {                          


      foreach ($errors as $keyError => $valueError) {
        echo "$keyError = $valueError <br />";
      }                     


    } else {


      $id   = $_POST['id'];

      $image1 = $image[0];
      $image2 = $image[1];

      require_once("koneksi.php");                  

      $sql = "UPDATE post_gallery SET images1='$image1', images2='$image2' WHERE  id='$id' ";

      $parameter = array(
                         ':image1' => $image1,
                         ':image2' => $image2);

      $query = $conn->prepare($sql);
      $query->execute($parameter);

      echo "Data succesfully inserted !!";

      $conn = null;

  header("Location:../view-post.php?id=4");
    }

  } else {

    header("Location:../view-post.php?id=4");

  }

?>

0 个答案:

没有答案
相关问题