PHP-上传新的个人资料图片。无法设置新的img用户变量,并且无法将img插入数据库

时间:2018-08-01 13:05:14

标签: php html mysql phpmyadmin

我不明白为什么这不起作用,我有一个插入查询,并且一个要更改Session变量,两者都很好。实际的图像会很好地进入文件夹,只是不会插入数据库中,也不会存储为会话变量并显示在页面上。任何帮助将不胜感激!

这是php处理代码:

 <?php

 session_start();
 include_once 'dbh-inc.php';

 if (isset($_POST['upload'])) {
 //gets all info from file to upload using the form//
 $file = $_FILES['file'];
 //extracting all info from file//
 $FileName = $_FILES['file'] ['name']; 
 $FileTmpName = $_FILES['file'] ['tmp_name'];
 $FileSize = $_FILES['file'] ['size'];
 $FileError = $_FILES['file'] ['error'];
 $FileType = $_FILES['file'] ['type'];
 //separating the file name from the file extension, single out extension//
 $FileExt = explode('.', $FileName); 
  //end function gets the last piece of data from an array//
    $FileActualExt = strtolower(end($FileExt));
    //allowed file types//
    $allowed = array('jpg', 'jpeg', 'png');
    //checking whether the fileactualext ie. extension types are defined as 
  allowed//
     if (in_array($FileActualExt, $allowed)) {
        if ($FileError === 0) {
           if ($FileSize < 1000000) {

            $FileNameNew = uniqid('', true).".".$FileActualExt;

            $FileDestination = 'uploads/'.$FileNameNew;
           //inserting into database//

            $sql = "INSERT INTO users (user_image) VALUES ('$FileName') WHERE 
      `user_id`='".$_SESSION["u_id"]."' ;";
            mysqli_query($conn, $sql);

            //updating session image variable//
            $result = mysqli_query($conn, "SELECT * FROM `users` WHERE 
     `user_id`='".$_SESSION["u_id"]."'");
            $row = mysqli_fetch_array($result);             
            $_SESSION['u_image'] = $row['user_image'];

                         //inserting into folder//
            move_uploaded_file($FileTmpName, $FileDestination);

            header("Location: index.php?upload=success");
            exit();

          } else {
            echo "your file is too big!";
          }
        }


    } else {
        echo "error uploading file";
  }

} else {
    echo "you cannot upload files of this type!";
}

这是编辑个人资料页面的html代码:

<?php include_once 'Header.php'; 

$uid = ucfirst($_SESSION['u_uid']);

//THIS CODE ONLY EDITS THE VISIBLE PORTION OF A USERS PROFILE//
?>

             <table width="398" border="0" align="center" cellpadding="0">
    <form class="edit-profile" action="includes/edit-profile-inc.php" 
     method="POST">
   <tr>
     <td width="82" valign="top"><div align="left"> <?php echo $uid ?> </div> 
    </td>
    <td height="26" colspan="2"><input type="text" name="uid" 
  placeholder="New Username"></td>
    <td><div align="right"><a href="index.php">logout</a></div></td>
  </tr>
  <tr>
    <td width="129" rowspan="5"><img src="<?php echo $picture ?>" width="129" 
   height="129" alt="no image found"/></td>
   <td width="82" valign="top"><div align="left">FirstName:</div></td>
      <td width="165" valign="top"><input type="text" name="first" 
    placeholder="New First name"></td>
 </tr>
    <tr>
       <td valign="top"><div align="left">LastName:</div></td>
        <td valign="top"><input type="text" name="last" placeholder="New Last 
    Name"></td>
   </tr>
 <tr>
   <tr>
  <td valign="top"><div align="left">City:</div></td>
  <td valign="top"><input type="text" name="city" placeholder="New City"> 
</td>
  </tr>
<tr>
  <tr>
   <td valign="top"><div align="left">Region: </div></td>
   <td valign="top"><input type="text" name="region" placeholder="New 
   Region"></td>
 </tr>
 <tr>
   <td valign="top"><div align="left">Country:</div></td>
   <td valign="top"><input type="text" name="country" placeholder="New 
Country"></td>
 </tr>
 <tr> 
<td valign="top"><div align="left">About me: </div></td>
<td valign="top">
  <input type="text" name="about" placeholder="About me">
 </thgd>
 </tr>
 <tr>
   <td><button type="submit" name="edit">Update profile</button></td>
 </tr>
</form>
 <form action="uploads-inc.php" method="POST" enctype="multipart/form-data"> 
<tr>
  <td>
  <input type="file" name="file" >
  <button type="submit" name="upload">Update profile picture</button>
 </td>
  </tr>
   </form>

 <p align="center"><a href="index.php"></a></p>


   <?php
    include_once 'Footer.php';
   ?>

发送帮助!

0 个答案:

没有答案