0

I dont understand why this isnt working, i have an insert querie, and one to change the Session variable, which both look fine. the actual image goes into the folder fine, it just does not get inserted into the database, nor stored as a session variable and displayed on the page. any help would be very appreciated!

here is the php processing code:

 <?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!";
}

and here is the html code for the editing profile page:

<?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';
   ?>

Send help!

akemedis
  • 414
  • 2
  • 6
  • 17
  • Try $_SESSION['u_image'] = $row[0]['user_image']; This should not be final solution, as the code is not very good, but try it and see if you get any result then. – Zony86 Aug 01 '18 at 13:13
  • didn't work unfortunately :( – akemedis Aug 01 '18 at 13:17
  • Ok, I see now your first INSERT query is wrong also. You can not use INSERT and WHERE together, you have to change that to UPDATE. So "UPDATE users SET user_image = '$FileName' WHERE ......." etc – Zony86 Aug 01 '18 at 13:22
  • dude you're the greatest! thanks so much you bloody legend! totally worked – akemedis Aug 01 '18 at 13:50

0 Answers0