是否交替上传图片

alternate to upload image or not

我是 Whosebug 的新手,也是 PHP 的新手,所以我想先描述一下我的问题。对不起,如果我的语法不好,我不是本地人。

我制作了一个表单,让用户 post(到数据库)有或没有图像,这是代码

$link = mysqli_connect('localhost', 'root', '12345!@#', 'pklapps') or die("Couldnt connect to the server");
//public info dari admin
if (isset($_POST["title"]) && isset($_POST["content"]) && isset($_POST["category"])) {

    $title = $_POST['title'];
    $desc = $_POST['content'];
    $lat = $_POST['latitude'];
    $long = $_POST['longitude'];
    $category = $_POST['category'];
    $image = $_POST['userfile'];

    if (!isset($_FILES['userfile'])) {
        $query = "INSERT INTO public_info (title, content, category) VALUES ('$title','$desc','$category')"; 

        $result = mysqli_query($link, $query);

        if ($result) {
            header('Location: post.php?success');
        } else {
            header('Location: post.php?error');
        }
    } else {
        $fileName = $_FILES['userfile']['name'];
        $target = "uploads/";
        $fileTarget = $target.$fileName; 
        $tempFileName = $_FILES["userfile"]["tmp_name"];

        $result = move_uploaded_file($tempFileName,$fileTarget);
        /*
        *   If file was successfully uploaded in the destination folder
        */
        if ($result) { 
            header('Location: post.php?success'); 
            $query = "INSERT INTO public_info (title, content, category, imagePath) VALUES ('$title','$desc','$category', '$fileTarget')";
            $link->query($query) or die("Error : ".mysqli_error($link)); 
        } else { 
            header('Location: post.php?errimg'); 
        }

        mysqli_close($link);
    }
}   

并且当我附加图像或文件时表格正常,但当我不附加图像时它会转到 header('Location: post.php?errimg');。请帮我解决这个问题,谢谢。

将您的 if 条件从 !isset($_FILES['userfile']) 替换为 empty($_FILES['userfile']['name'])

    //if (!isset($_POST[]))
    $title = $_POST['title'];
    $desc = $_POST['content'];
    $lat = $_POST['latitude'];
    $long = $_POST['longitude'];
    $category = $_POST['category'];
    $image = $_POST['userfile'];

    if(empty($_FILES['userfile']['name'])) {
        $query = "INSERT INTO public_info (title, content, category) VALUES ('$title','$desc','$category')"; 

        $result = mysqli_query($link, $query);

        if ($result) {
            header('Location: post.php?success');
        }

        else {
            header('Location: post.php?error');
        }
    }
    else {
        $fileName = $_FILES['userfile']['name'];
        $target = "uploads/";
        $fileTarget = $target.$fileName; 
        $tempFileName = $_FILES["userfile"]["tmp_name"];

        $result = move_uploaded_file($tempFileName,$fileTarget);
        /*
        *   If file was successfully uploaded in the destination folder
        */
        if($result) { 
            header('Location: post.php?success'); 
            $query = "INSERT INTO public_info (title, content, category, imagePath) VALUES ('$title','$desc','$category', '$fileTarget')";
            $link->query($query) or die("Error : ".mysqli_error($link)); 
            }
        else { 
            header('Location: post.php?errimg'); 
            }
        mysqli_close($link);
    }
    // $query = "INSERT INTO public_info (title, content, category) VALUES ('$title','$desc','$category')";   
}       ?>