将图片上传到服务器并保存 url 到服务器

upload image to server and saving url to server

我正在构建一个图片上传器,我想通过它上传我的图片到服务器并用新图片替换数据库中的旧图片 URL。上传部分工作正常,但是我无法在数据库中获取图像URL。有人可以看看我的代码并告诉我哪里做错了吗?

<?php
$target_dir = "media/images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOK = 1;
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
 if(isset($_POST["uploadImage"])) {
     $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
      if ($check != false) {
         echo "File is an image - " .$check["mime"]. ".";
       $uploadOK = 1;
      } else {
         echo "File not an image";
         $uploadOK = 0;
     }
 }

 // check if file exists 
 if (file_exists($target_file)) {
   echo "sorry File exists";
      $uploadOK = 0;
 }

  // check fle size
  if ($_FILES["fileToUpload"]["size"] > 5000000) {
     echo "Sorry your file is too large.";
    $uploadOK = 0;
 }

 // Allow certain file formats
 if ($imageFileType != "jpg" && $imageFileType != "png" &&     $imageFileType != "jpeg" && $imageFileType != "gif") {
 echo "sorry, only JPG, JPEG, PNG and GIF files allowed.";
 $uploadOK == 0;
}

// check if $uploadOK is set 0 by an error
 if ($uploadOK == 0) {
     echo "Sorry your file was not uploaded.";
     // if everything is ok try to upload file
  } else {
     if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],  $target_file)) {
     echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has      been uploaded.";
     include 'connect.php';

    $stmt = $conn->prepare("INSERT INTO image(name,imageURL, image_cat_id, AlbumID) VALUES (home1, ?, 8, 0)");

    $stmt->bind_param('s', $target_file);
    $stmt->execute();
    $stmt->close();
 } else {
     echo "Sorry, there was an error uploading your file.";
 }

 }
 ?>

上传成功结果:

File is an image - image/png.The file arrowdown_51.png has been uploaded. Fatal error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\WebDevelopment\Mosta Dynamic\CMS\upload.php on line 47

函数 prepare() returns 可能是一个布尔值...我想,您在 SQL-Query 中犯了一个错误。尝试以下方法:

INSERT INTO image(name,imageURL, image_cat_id, AlbumID)
VALUES ('home1', ?, '8', '0')

编辑:如果上述查询也没有保存记录:还要检查您的列名(和 table)是否正确。

请原谅我的英语不好,我不是母语人士...:)