插入用户PHP MySQL

时间:2018-04-02 20:16:17

标签: php html mysql mysqli phpmyadmin

我正在创建一个社交网络,我遇到了问题。用户可以选择将图像上传为帖子。当他们进入他们的home.php页面时,他们会看到图像以及其他帖子。但是图像进入我的文件夹但它没有保存在数据库中。有人可以帮助我吗?

imageupload.php:

$username = isset($_SESSION['username']) ? $_SESSION['username'] : "";

$info = date('Y-m-d_H-i-s');

if(!empty($username))
{
    if (isset($_FILES['fileToUpload'])) {

      $errors= array();
      $file_name = $_FILES['fileToUpload']['name'];
      $file_size = $_FILES['fileToUpload']['size'];
      $width = 1500;
      $height = 1500;
      $file_tmp = $_FILES['fileToUpload']['tmp_name'];
      $file_type = $_FILES['fileToUpload']['type'];
      $tmp = explode('.',$_FILES['fileToUpload']['name']);
      $file_ext=strtolower (end ($tmp));

      $extensions= array("jpeg","jpg","png");

      if(in_array($file_ext,$extensions)=== false){
         $errors[]="extension not allowed, please choose a JPEG or PNG file.";
      }

       if ($file_size > 8097152) {
        $errors[] = 'File size must be 2 MB';
      }

      if ($width > 1500 || $height > 1500) {
            echo"File is to large";
      }

      if(empty($errors)==true)
      {
         move_uploaded_file($file_tmp,"images/".date('Y-m-d_H-i-s').$file_name);

         $store = "INSERT INTO posts (username, body) VALUES body='".mysqli_real_escape_string( $conn, date('Y-m-d_H-i-s').$file_name )."', date_time='" . $info . "' WHERE username='".mysqli_real_escape_string( $conn,  $username )."'";
         echo"Saved to DB";
        if(mysqli_query($conn, $store))
        {
        }
        else
        {
            echo "Upload failed!";
        }

      }else{
         print_r($errors);
         echo"Couldn't upload picture";
      }

    }}
   else
    {
    echo "Invalid Username";
    }
    ?>

    <?php
  $getimg = mysqli_query($conn,"SELECT userPic, date_time FROM users WHERE
  username='".mysqli_real_escape_string( $conn, $username )."'");
   $rows=mysqli_fetch_array($getimg);
   $img = $rows['userPic'];
   ?>

profile.php:

<form id="form3" action="imageupload.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload"><br />
<br><input id="sub2" type="submit" value="Upload image" name="change"><br />
</form> 

0 个答案:

没有答案