在数据库中上载和插入图像链接时出现问题

vmdwslir  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(364)

这个问题在这里已经有答案了

使用php的“notice:未定义变量”、“notice:未定义索引”和“notice:未定义偏移量”(28个答案)
两年前关门了。
我在尝试上载图像和状态更新时遇到问题。如果用户不想在他们的状态旁边上传一个图像,它会很好地插入到表(newsfeed)中,但是如果他们想添加一个图像,它会出现一个错误:undefined index:img in。。。。有什么办法可以解决这个问题吗?
这是我的html,他们可以上传一个图像和一个状态更新。

<form action="include/updatestatus.php" method="post">
 <div class="form-group">
  <label for="status">Status</label>
  <input type="text" class="form-control" name="status" id="status" placeholder="<?php echo $status;?>">
</div>
<!-- Single button -->
<p ><span class="glyphicon glyphicon-film"></span> Upload an Image?:
  <input type="file" name="img" class="btn btn-default">
</p>
<div class="text-right">
        <button type="submit" name="statusupdate" class="btn btn-default btn-md">
          <span class="glyphicon glyphicon-thumbs-up"></span> Update Status
        </button>
      </div>
</form>

这里是上传和插入php代码(include/updatestatus.php)

<?php
if (isset($_POST['statusupdate'])) {
    $date = date("Y.m.d");
//If Post Image is blank --> insert with nothing.
if (empty($_POST['img'])) {
    // prepare sql and bind parameters
    $stmt = $DB_con->prepare("INSERT INTO newsfeed (userid, status,uploaddate)
    VALUES (:userid, :status, :date)");
    $stmt->bindParam(':userid', $_SESSION['user']['id']);
    $stmt->bindParam(':status', $_POST['status']);
     $stmt->bindParam(':date', $date);
    $stmt->execute();

    echo "New records created successfully";
    }
else {

 $name=$_FILES['img']['name'];
 $type=$_FILES['img']['type'];
 $size=($_FILES['img']['size'])/1024;

 $ext=end(explode('.',$name));
 if (($ext == "gif")
 || ($ext == "jpeg")
 || ($ext == "jpg")
 || ($ext =="png")
 || ($ext =="PNG")
 && ($size > 50))
 {
 //Changes Name to Number :)
 $newname=uniqid();
 $imagename=$newname.".".$ext;
 $directory="images/upload/";
 $fulldirectory=$directory.$imagename;
 if(move_uploaded_file($_FILES['img']['tmp_name'],$fulldirectory))
 {
   $stmt = $DB_con->prepare("INSERT INTO newsfeed (userid, status,uploaddate,image)
   VALUES (:userid, :status, :date, :image)");
       $stmt->bindParam(':userid', $_SESSION['user']['id']);
       $stmt->bindParam(':status', $_POST['status']);
      $stmt->bindParam(':date', $date);
      $stmt->bindParam(':image', $imagename);

 //If upload is a success --> Redirect to profile.php?action=upload.success
       if($stmt->execute()){
   header ('location: ../profile.php?action=upload.success');

       }
     else
       header ('location: ../profile.php?action=upload.failed');
     }
 else
 {
    header('location: ../profile.php?action=file.size');
 }
 }
 else{
     header('location: ../profile.php?action=wrong.extension');
     }
   } //end of first else
} //end of $_Post['statusupdate']
?>
7kjnsjlb

7kjnsjlb1#

您的表单标签必须包括 enctype="multipart/form-data" 上传文件。
你还要检查 $_POST['img'] 它永远不会存在,因为它是 $_FILES 包含图像数据的数组

sirbozc5

sirbozc52#

尝试将enctype=“multipart/form data”添加到标记中。
要检查文件是否已上载,请使用以下命令:

if(!file_exists($_FILES['img']['tmp_name']) || !is_uploaded_file($_FILES['img']['tmp_name'])) {
    echo 'No upload';
}

相关问题