php - 将图片上传到服务器并将网址保存到服务器

php - 将图片上传到服务器并将网址保存到服务器,第1张

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

<?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.";
 }

 }
 ?>

成功上传的结果:

  

文件是图片 - image / png。文件arrowdown_51.png已上传。   致命错误:在第47行的C:\ xampp \ htdocs \ WebDevelopment \ Mosta Dynamic \ CMS \ upload.php中调用boolean上的成员函数bind_param()

最佳答案:

1 个答案:

答案 0 :(得分:1)

可能的是,函数prepare()返回一个布尔值...我想,你在SQL-Query中犯了一个错误。尝试使用以下内容:

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

编辑:如果上述查询也没有保留记录:如果列(和表)的名称正确,请检查。

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

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复