2016-09-01 17 views
1

私はそのフォームでいくつかの入力を持つフォームを持っています。空白に置き換えないようにするために、画像をデータベースに保存しておきたい。入力が空の場合、条件分岐以外の場合はエラーecho "Sorry, there was an error uploading your file.";からエラーが発生します。画像をアップロードすると、すべて正常に動作します。ここに私のコードです入力画像が空の場合エラーが発生する

<?php 

include "../../../config/config.php"; 
session_start(); 

if (isset($_GET['car'])) { 
    $car = $_GET['car']; 
} else { 
    die("Not found"); 
} 


if (isset($_POST['submit-edit'])) { 



    $title = mysqli_real_escape_string($con, $_POST['title-edit']); 
    $description = mysqli_real_escape_string($con, $_POST['description-edit']); 
    $category = mysqli_real_escape_string($con, $_POST['category-edit']); 



    /* ----------------------- MAIN IMAGE -------------------------- */ 
    $target_dir = "../../../img/found/thumbs-category/"; 
    $target_file2 = "" . basename($_FILES["img-edit"]["name"]); 
    $target_file = $target_dir . basename($_FILES["img-edit"]["name"]); 

    $uploadOk = 1; 
    $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION); 

    // Check file size 
    if ($_FILES["img-edit"]["size"] > 100000) { 
     $_SESSION['image-size'] = 1; 

     header("Location: /dashboard/views/edit.php?car=$car"); 
     exit(); 
    } 
//  
    //Allow certain file formats 
    if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" && $imageFileType != NULL) { 
     $_SESSION['image-format'] = 1; 

     header("Location: /dashboard/views/edit.php?car=$car"); 
     exit(); 
    } 

    // Check if $uploadOk is set to 0 by an error 
    if ($uploadOk == 0) { 

    } else { 
     if (move_uploaded_file($_FILES["img-edit"]["tmp_name"], $target_file)) { 


     } else { 
      echo "Sorry, there was an error uploading your file."; 
      exit(); 
     } 


     $query = "UPDATE cars 
      SET title='" . $title . "', text='" . $description . "', image='" . $target_file2 . "', fk_cars_first='" . $category . "' WHERE car=" . $car; 


     $result = mysqli_query($con, $query); 
//    var_dump($query); 
//    exit(); 
     if ($result) { 


      header("Location: /dashboard/views/edit.php?car=$car"); 
     } else { 
//   
      echo "error"; 
      exit(); 
      header("Location: /dashboard/views/edit.php?car=$car"); 
     } 
    } 
} 
?> 

もっと私はフォームを使用し、タイプ=「ファイル」と入力が空の私がある場合は、データベースを編集したい場合は、正確に私のデータベースに私が

 
title | description | image | fk_category 
test more text  car.jpg tuning

を持っています右のコード..消去しないように、実際のパスを維持したいthis..Iような何かを送信ボタンを設定し、取得エラーSorry, there was an error uploading your file.と、データベース内のパス空の場合、データベースが

 
title  | description | image | fk_category 
testedited | ed!t  |  | tuning_tex_edited

のように見えます空白です。

答えて

0

単純にこのコードを使用します。

if ($_FILES['img-edit']){ 
    foreach($_FILES['img-edit']['name'] as $a=>$v){ 

    //Check if empty 
    if(!empty($_FILES['img-edit']['name'][$a])){ 

     //Check size of image 
     if($_FILES['img-edit']['size'][$a]>0){ 

     //Now code here 

     } 
    } 
    } 
} 
1

if (move_uploaded_file($_FILES["img-edit"]["tmp_name"], $target_file)) { //if you have a valid file it will replace the image name 
$query = "UPDATE cars 
     SET title='" . $title . "', text='" . $description . "', image='" . $target_file2 . "', fk_cars_first='" . $category . "' WHERE car=" . $car; 

    } else { //if you dont have any image it will leave with the older one 
$query = "UPDATE cars 
     SET title='" . $title . "', text='" . $description . "', fk_cars_first='" . $category . "' WHERE car=" . $car; 
     echo "Sorry, there was an error uploading your file."; 
     exit(); 
    } 
このコードを試してみてください
関連する問題