0

The code below prints the correct user id of the current user but stores only 0 or 1 into the

database. I don't understand the problem with the code. Your help will be most appreciated

<?php  
 $user_id=print_r($_SESSION["id"]);

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

          $host = "";
          $db_name = "";
          $username = "";
          $password = "";

          $link=mysqli_connect($host, $username, $password, $db_name);

          //filename as image_path
          $filename=$_FILES['file']['name'];
          $filetmp=$_FILES['file']['tmp_name'];
          $image_title=$_POST['text'];
          $target="uploaded/".$filename;

          $date_time = date('Y-m-d H:i:s');
          $image_url="http://.....".$filename;

              if($filename!=""){

                  $sql="INSERT INTO images (image_path,created,image_url,image_title,user_id) VALUES('$filename','$date_time','$image_url','$image_title','$user_id')";
                  mysqli_query($link,$sql);

                   if(move_uploaded_file($filetmp,$target)){

                       echo "Image uploaded Successfully";

                   }
                   else{

                        echo "Failed to upload !!";
                   }

              }else{

                   echo "Please insert a valid image !!";
              }
      }
 ?>
Udipta Gogoi
  • 91
  • 1
  • 1
  • 11

4 Answers4

1

You are using print_r on $_SESSION["id"] so it expect that $_SESSION["id"] is an array try using: $user_id = $_SESSION["id"];

or

echo $_SESSION["id"] 

to print it

Rvdrichard
  • 335
  • 3
  • 12
0

You are trying to store assign printed value of session id to variable, but it actually assigning print function return value.

You should try following part of code:

$user_id=$_SESSION["id"];

it makes sense to store SESSION array with id key value to $user_id variable.

Keyur Potdar
  • 7,158
  • 6
  • 25
  • 40
  • it was a typing mistake in the question.I wrote exatctlly like as you said but did not work.As i said it prints the $user_id outside the if statement correctly but not inside it.What should be the cause? – Udipta Gogoi Mar 09 '18 at 12:53
0
   print_r($_SESSION["id"]);
  $user_id=$_SESSION["id"];
if(isset($_POST['submit']))
 { 
    $host = "";
    $db_name = "";
    $username = "";
    $password = "";
   $link=mysqli_connect($host, $username, $password, $db_name);
  //filename as image_path
   $filename=$_FILES['file']['name'];
  $filetmp=$_FILES['file']['tmp_name'];
  $image_title=$_POST['text'];
  $target="uploaded/".$filename;

  $date_time = date('Y-m-d H:i:s');
  $image_url="htt..".$filename;

  if($filename!=""){
   $sql="INSERT INTO images (image_path,created,image_url,image_title,user_id) VALUES ('$filename','$date_time','$image_url','$image_title','$user_id')";
   mysqli_query($link,$sql);

  if(move_uploaded_file($filetmp,$target)){
    echo "Image uploaded Successfully";
  }
  else
  {
    echo "Failed to upload !!";
   }
   }
else{
   echo "Please insert a valid image !!";
 }
  }
?>
 </body>
  </html>
Udipta Gogoi
  • 91
  • 1
  • 1
  • 11
0

You are not retrieveing the user id, but a success value returned by print_r(), which, when inserted into the database, is converted to 0 (false) or 1 (true).

You want to assign the value from $_SESSION['id'] directly, and for safety's sake to make sure it is an integer.

Also you need to escape $filename and $image_title before using them in the query, because otherwise you create an SQL injection vulnerability.

A last thing - although not part of your current problem - is that you should create the database entry only after a successful upload.

$user_id = (int)$_SESSION['id'];

if (isset($_POST['submit']))
{
    $host     = '';
    $db_name  = '';
    $username = '';
    $password = '';

    $link = mysqli_connect($host, $username, $password, $db_name);

    $filename    = $_FILES['file']['name'];
    $filetmp     = $_FILES['file']['tmp_name'];
    $image_title = $_POST['text'];
    $target      = 'uploaded/' . $filename;

    $date_time = date('Y-m-d H:i:s');
    $image_url = 'http://...' . $filename;

    if ($filename != "")
    {
        if (move_uploaded_file($filetmp, $target))
        {
            $filename    = mysqli_real_escape_string($link, $filename);
            $image_title = mysqli_real_escape_string($link, $image_title);
            $sql         = "INSERT INTO images (image_path,created,image_url,image_title,user_id) VALUES ('$filename','$date_time','$image_url','$image_title',$user_id)";
            mysqli_query($link, $sql);

            echo 'Image uploaded successfully';
        }
        else
        {
            echo 'Failed to upload!';
        }
    }
    else
    {
        echo 'Please insert a valid image!';
    }
}
nibra
  • 3,958
  • 2
  • 20
  • 34