如何使用php将图像和数据插入mysql?

我在将数据保存到数据库时遇到麻烦。我的连接详细信息和sql insert查询一切正确,并且图像也上传到了文件夹,但是我不知道为什么当我点击“上传”按钮时,数据和图像都没有保存到数据库中。有人可以帮我吗?

我的PHP代码

<?php 


include('server.php');
$userID = 1;

if(isset($_SESSION['username']))
{
   $userName =  $_SESSION['username'];
   $queryID = "SELECT id from users WHERE username = '$userName'";
   $resultID = $db->query($queryID);
   $row=$resultID->fetch_assoc();
   $userID = $row['id'];


}


if(isset($_POST['submit']))
{
     $image = $_FILES['image']['name'];
     $target = "images/".basename($image);

    $eventName = $_POST['eventName'];
    $eventDetail = $_POST['eventDetail'];
    $eventDate = $_POST['eventDate'];
    $eventTime = $_POST['eventTime'];


    $queryImage = "INSERT INTO event_detail(eventName,eventDetails,eventDate,eventTime,imagePath,userID) VALUES('$eventName','$eventDetail','$eventDate','$eventTime','$image','$userID')";

    mysqli_query($db,$queryImage);


    if(move_uploaded_file($_FILES['image']['tmp_name'],$target))
    {
        $msg = "Image uploaded successfully";
    }
    else
    {
        $msg = "There is problem";

    }

}




?>

html 
<form method="post" enctype="multipart/form-data">
<label for="eventName">Event Name:<label> 
<input type="text" id="eventName" name="eventName" ><br><br>
<label for="eventDetail">Event Detail:<label> 
<textarea id="eventDetail" name="eventDetail" ></textarea><br><br>
<label for="eventDate">Event Date:<label> 
<input type="text" id="eventDate" name="eventDate" ><br><br>
<label for="eventTime">Event Time:<label> 
<input type="text" id="eventTime" name="eventTime" ><br><br>
<input type="file" id="image" name="image"><br><br> 
<button  type="submit" id="submit" name="submit" >Submit</button>
</form>
评论
  • Buck
    Buck 回复

    改变这个

    $queryImage = "INSERT INTO event_detail(eventName,eventDetails,eventDate,eventTime,imagePath,userID) VALUES ('$eventName','$eventDetail','$eventDate','$eventTime','$image','$userID')";
    

    $queryImage = "INSERT INTO event_detail(eventName,eventDetails,eventDate,eventTime,imagePath,userID) VALUES ($eventName,$eventDetail,$eventDate,$eventTime,$image,$userID)";