如何将个人资料图片添加到用户php

时间:2019-08-07 16:17:18

标签: php mysql mysqli

我希望已登录的用户添加个人资料图片。没有显示错误,只是图片没有添加到应有的位置。 我知道我必须使用准备好的陈述。我只想先解决这个问题。

当用户未更改个人资料图片时,默认图片将完美显示。文件配置文件pic不会上传到该文件夹​​。

这是您更改图片的页面。

  <?php
  session_start();
  include_once 'dbh.php';

  <html>

  <body>
  <?php

   $sql = "SELECT * FROM user";
   $result = mysqli_query($conn, $sql);
   if (mysqli_num_rows($result) > 0) {
     while ($row = mysqli_fetch_assoc($result)) {
      $id = $row['id'];

      $sqlImg = "SELECT * FROM profileimg WHERE userid='$id'";
      $resultImg = mysqli_query($conn, $sqlImg);
      while ($rowImg = mysqli_fetch_assoc($resultImg)) {

        echo "<div>";

          if ($rowImg['status'] == 0) {
            echo "<img src='uploads/profile".$id.".jpg'>";
          }

          else {
            echo "<img src='uploads/male.jpg'>";
          }

          echo "<p>".$row['username']."</p>";
        echo "</div>";
      }
    }
  }
  else {
    echo "There are no users!";
  }


  if (isset($_SESSION['id'])) {
    echo "You are logged in!";

    echo '<form action="includes/upload.inc.php" method="post" 
    enctype="multipart/form-data">
      <input type="file" name="file">
      <button type="submit" name="submit">UPLOAD FILE</button>
    </form>';
  }
  else {
    echo "You are not logged in!";

  }
?>

这是上传的php页面

<?php

session_start();
include_once 'dbh.php';
$id = $_SESSION['id'];


if (isset($_POST['submit'])) {

$file = $_FILES['file'];
$fileName = $file['name'];
$fileType = $file['type'];
$fileTempName = $file['tmp_name'];
$fileError = $file['error'];
$fileSize = $file['size'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array("jpg", "jpeg", "png", "pdf");


if (in_array($fileActualExt, $allowed)) {

  if ($fileError === 0) {

    if ($fileSize < 500000) {
      //I now need to create a unique ID which we use to replace the name 
       of the uploaded file, before inserting it into our rootfolder
      //If I don't do this, we might end up overwriting the file if we 
       upload a file later with the same name
      //Here I use the user ID of the user to create the first part of the 
       image name
      $fileNameNew = "profile".$id.".".$fileActualExt;
      $fileDestination = 'uploads/'.$fileNameNew;
      move_uploaded_file($fileTmpName, $fileDestination);

      $sql = "UPDATE profileimg SET status=0 WHERE userid='$id';";
      $result = mysqli_query($conn, $sql);

      header("Location: index.php?uploadsuccess");
    }
    else {
      echo "Your file is too big!";
    }
  }
  else {
    echo "There was an error uploading your file, try again!";
  }
}
else {
  echo "You cannot upload files of this type!";
}
 }

2 个答案:

答案 0 :(得分:0)

首先,确保将PHP配置为允许文件上传。

在“ php.ini”文件中,搜索file_uploads指令,并将其设置为On:

答案 1 :(得分:0)

我怀疑您的以下更新查询附近存在逻辑问题:

$sql = "UPDATE profileimg SET status=0 WHERE userid='$id';";

您的逻辑将仅对那些在profileimg表中已具有相应记录的用户运行良好。但是UPDATE查询对新用户将无济于事。

因此,您必须首先检查profileimg中是否有针对特定用户的记录。如果没有记录,则运行INSERT查询;如果记录存在,则运行UPDATE查询。