上传带有图片文件的所有字段上传可选的PHP MySQLI

时间:2018-07-16 23:56:49

标签: php mysqli

我进行了广泛搜索,无法以我能理解的术语找到问题的答案。我试图使我的代码上载所有文本输入字段,如果文件输入中未包含图像,则上载除图像之外的所有图像,并在存在图像时上载包括图像的所有图像。以下是我在显示图片时的工作代码。所有帮助将不胜感激。

<?php
session_start();
error_reporting(E_ALL);
include_once 'dbconnect.php';

$userID = $_SESSION['usr_id'];

if(!empty($_FILES["uploadedimage"]["tmp_name"])) {
$eTitle = mysqli_real_escape_string($con, $_POST['etitle']);
$eDate=mysqli_real_escape_string($con, $_POST['edate']);
$eDesc=mysqli_real_escape_string($con, $_POST['edesc']);
$file_tmp = $_FILES['uploadedimage']['tmp_name'];
$file_ext = strtolower(end(explode('.',$_FILES['uploadedimage']['name'])));
$date = date("d-m-Y");
$imagename = $date."-".time().".".$file_ext;
$target_path = "event_images/".$imagename;  
$move = move_uploaded_file($file_tmp, $target_path);
if($move) {
    if($_FILES['uploadedimage']===false){
        $not = "NULL";
    }ELSE{
        $not = $imagename;
    }

    $sql =mysqli_query($con, "INSERT INTO `events` (eventID,eventImage,eventTitle,eventDate,eventDescription) values (NULL,'".$not."','".$eTitle."','".$eDate."','".$eDesc."')");
    $db = mysqli_query($sql, $con);
    $msg = "Song has been uploaded successfully";
    header("Location: websiteeditor.events.php");
}
else {
    $msg = "Not uploaded because of error #".$_FILES["file"]["error"];
}

} 
else {
    $msg = "Failed to Upload<br/>Not uploaded because of error #".$_FILES["file"]["error"];
}

?>
<?=$msg;?>

1 个答案:

答案 0 :(得分:1)

以下代码应按您需要的方式工作。

<?php
session_start();
error_reporting(E_ALL);
include_once 'dbconnect.php';
$userID = $_SESSION['usr_id'];

$eTitle = mysqli_real_escape_string($con, $_POST['etitle']);
$eDate = mysqli_real_escape_string($con, $_POST['edate']);
$eDesc = mysqli_real_escape_string($con, $_POST['edesc']);
$date = date("d-m-Y"); // where is this used?

$not = null;
if (!empty($_FILES["uploadedimage"]["tmp_name"])) {
    $file_tmp = $_FILES['uploadedimage']['tmp_name'];
    $file_ext = strtolower(end(explode('.', $_FILES['uploadedimage']['name'])));
    $imagename = $date . "-" . time() . "." . $file_ext;
    $target_path = "event_images/" . $imagename;
    $move = move_uploaded_file($file_tmp, $target_path);
    if ($move) {
        $not = $imagename;
    } else {
        $msg = "Not uploaded because of error #" . $_FILES["file"]["error"];
    }
}

$sql = mysqli_query($con, "INSERT INTO `events` (eventID,eventImage,eventTitle,eventDate,eventDescription) values (NULL,'" . $not . "','" . $eTitle . "','" . $eDate . "','" . $eDesc . "')");
$db = mysqli_query($sql, $con);
$msg = "Song has been uploaded successfully";
header("Location: websiteeditor.events.php");

?>