doumen1883 2015-04-13 18:44
浏览 49

将图像上传到服务器并将图像路径保存到数据库

I have the code to upload an image to the server, but the PHP code for saving the image path to the database has a problem.It does not update the database. It shows the following errors and con not udate the database:

 ( ! ) Notice: Undefined index: fileToUpload in C:\wamp\www\hosty\Uploads.php on line 4
 ( ! ) Notice: Undefined index: datereg in C:\wamp\www\hosty\Uploads.php on line 8
 ( ! ) Notice: Undefined index: Description in C:\wamp\www\hosty\Uploads.php on line 9
 ( ! ) Notice: Undefined index: title in C:\wamp\www\hosty\Uploads.php on line 10
 ( ! ) Notice: Undefined index: fileToUpload in C:\wamp\www\hosty\Uploads.php on line 26


<?php require_once('Connections/Conn_host.php'); ?>
<?php
 error_reporting(0);
  $target_dir = "uploads/";
   $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
 $uploadOk = 1;
$datereg=$_POST['datereg'];
$Description=$_POST['Description'];
$title=$_POST['title'];
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
} else {
    echo "File is not an image.";
    $uploadOk = 0;
   }}
if (file_exists($target_file)) {

  echo "Sorry, file already exists.";
       $uploadOk = 0;
}
  // Check file size
  if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
     $uploadOk = 0;
  }
  $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
   echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
} 

 if ($uploadOk == 0) {
   echo "Sorry, your file was not uploaded.";

} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],  $target_file)) {
       mysql_select_db($database_Conn_host, $Conn_host);
    $query_rsUpload = "insert into infor(title,Description,userId,datereg,name,path) values('$title','$Description',,'','$datereg','$name','uploads/$name')";
 $rsUpload = mysql_query($query_rsUpload, $Conn_host) or die(mysql_error());


       echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
}
}
   ?> 
 <!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    <p>
      <label>Title
        <input name="title" type="text" id="title" />
      </label>
    </p>
<p>
    <label for="Description">Description</label>
     <textarea name="Description" id="Description" cols="45" rows="5">  </textarea>
  </p>
   <p>
     <input name="userId" type="hidden" id="hiddenField" />
     <input name="datereg" type="hidden" id="hiddenField2" />
  </p>
<p>Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
      <input type="submit" value="Upload Image" name="submit">
  </p>
</form>

 </body>
 </html>
  • 写回答

2条回答 默认 最新

  • dongyizhui0616 2015-04-13 18:48
    关注

    There's a error in your query.

    $query_rsUpload = "insert into infor(title,Description,userId,datereg,name,path) values('$title','$Description',,'','$datereg','$name','uploads/$name')";
    

    After $Description, there are two commas ,,.

    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?