Jump to content

Add as Session


herghost

Recommended Posts

Hi all,

 

When the below code runs, how can I store the result (image) as a session?

 

<?php
session_start();

include('include/database.php');


// Make sure the user actually 
// selected and uploaded a file
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 

      // Temporary file name stored on the server
      $tmpName  = $_FILES['image']['tmp_name'];  
       $userid = $_SESSION['SESS_USERID'];
      // Read the file 
      $fp      = fopen($tmpName, 'r');
      $data = fread($fp, filesize($tmpName));
      $data = addslashes($data);
      fclose($fp);
      

      // Create the query and insert
      // into our database.
  
  $sql = mysql_query("SELECT * FROM bandpic WHERE userid = '$userid'");
if(mysql_num_rows($sql) == 0)
{
      $query = "INSERT INTO bandpic
   (userid, image)
   values
   ('$userid','$data')";
   
}
else
$query = "UPDATE bandpic SET 
userid = '$userid',
image = '$data'";

}
   
     $result = mysql_query($query);
 session_regenerate_id();
		$bandpic = mysql_fetch_assoc($result);
    $_SESSION["BANDPIC"] = $bandpic['image'];
session_write_close();

   //Check whether the query was successful or not
   if($result) {
      header("location: member_home.php");
      exit();
   }else {
      die(mysql_error());
      
   }
   


// Close our MySQL Link
mysql_close($con);
?>  

Link to comment
https://forums.phpfreaks.com/topic/153580-add-as-session/
Share on other sites

You have to use a SELECT query to pull the image  out of the database. Either that, or use the variable $data as opposed $bandpic['image']. $bandpic is null because the query you assigned it to is not retrieving information, it's inserting information. (Hopefully that's clear.)

Link to comment
https://forums.phpfreaks.com/topic/153580-add-as-session/#findComment-807014
Share on other sites

<?php
session_start();

include('include/database.php');


// Make sure the user actually 
// selected and uploaded a file
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 

      // Temporary file name stored on the server
      $tmpName  = $_FILES['image']['tmp_name'];  
       $userid = $_SESSION['SESS_USERID'];
      // Read the file 
      $fp      = fopen($tmpName, 'r');
      $data = fread($fp, filesize($tmpName));
      fclose($fp);

      // Create the query and insert
      // into our database.

     $sql = mysql_query("SELECT * FROM bandpic WHERE userid = '$userid'");
if(mysql_num_rows($sql) == 0)
{
      $query = "INSERT INTO bandpic
   (userid, image)
   values
   ('$userid','$data')";
   
}
else
$query = "UPDATE bandpic SET 
userid = '$userid',
image = '$data'";

}

   //Check whether the query was successful or not
   if($result) {
      session_regenerate_id();
      $_SESSION["BANDPIC"] = $data;
      session_write_close();

      header("location: member_home.php");
      exit();
   }else {
      die(mysql_error());
      
   }
   


// Close our MySQL Link
mysql_close($con);
?> 

 

Try this and see if it works. Also, as xtopolis implied, there's no conceivable reason to pass your image through the addslashes() function.

Link to comment
https://forums.phpfreaks.com/topic/153580-add-as-session/#findComment-807046
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.