如果输入图片为空,请给我错误

时间:2016-09-01 08:46:01

标签: php mysql

我有一个表单,其中包含一些输入,我希望在数据库中进行一些编辑。我想在数据库中存在一张图片,以防止它被空格替换。如果输入为空,我从条件分支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

当我想编辑数据库时,我使用表单,如果type =“file”的输入为空,我想保持实际路径不要擦除..正确的代码做这样的事情..我设置提交按钮和数据库看起来像

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

如果为空则获取错误Sorry, there was an error uploading your file.,并且在数据库中路径为空。

2 个答案:

答案 0 :(得分: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 :(得分:0)

试试此代码

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();
    }