views:

53

answers:

3

hi all

How can I send session[table] to database in php ?

like that.

session_start();
session_register('table');
$qty=$_POST[txtQty];
$pid=$_REQUEST[pid];
$data=@mysql_query("SELECT * from products where productid=".$pid,$con) or die(mysql_error());
$table=$_SESSION[table];
while($row=@mysql_fetch_array($data))
{   
    $table.='<tr><td>'.$row[name].'></td>
<td>'.$row[price].'</td><td>'.$qty.'</td><td>'.$row[price]*$qty.'</td>
<td>Delete</td></tr>';
    $_SESSION[table]=$table; 
}

I want to send $_SESSION[table]=$table; to database

Thank Advanced.

CCK

+1  A: 

you seems to use the $_SESSION has a cache, the $_SESSION variable is not intended to do that. It is for stocking user related variables and it should/could of kilobytes worth (even less).

If you are looking for some cache mechanism there memcached, APC, and many others.

also to be noted in my comment $_SESSION[table] will generate warning, you should add the quotes like $_SESSION['table'].

Also it's a good practice to develop with the maximum level of warning (i.e. error_reporting(E_ALL);), it would help you to find possible bugs/issues more easily.

To put back the $_SESSION['table'] in database your would have to somehow store it as an array and then loop thought that array and do subsequent INSERT queries.

Good luck

RageZ
A: 

I'm not quite sure what the intentions are, but if you want to modify the way sessions work you'll need to define your own session functions and set them with session_set_save_handler().

pssdbt
A: 

You can modify and use this class, it keeps session in DB

class MySQLSession
{
  protected $resource = null;
  protected $db = 'base';
  protected $table = 'session';
  protected $id_col = 'sess_id';
  protected $data_col = 'sess_data';
  protected $time_col = 'sess_time';

  public function __construct($context, $parameters = null){
    session_set_save_handler(array($this, 'sessionOpen'),
                             array($this, 'sessionClose'),
                             array($this, 'sessionRead'),
                             array($this, 'sessionWrite'),
                             array($this, 'sessionDestroy'),
                             array($this, 'sessionGC'));

    session_start();
  }

  public function sessionClose(){
    return true;
  }

  public function sessionDestroy($id){
    $id = mysql_real_escape_string($id, $this->resource);
    $sql = 'DELETE FROM '.$this->table.' WHERE '.$this->id_col.' = \''.$id.'\'';
    if (@mysql_query($sql, $this->resource)){
      return true;
    }
    $error = 'MySQLSessionStorage cannot destroy session id "%s"';
    $error = sprintf($error, $id);
    throw new Exception($error);
  }

  public function sessionGC($lifetime){
    $sql = 'DELETE FROM '.$this->table.' '.
           'WHERE '.$this->time_col.' + INTERVAL '.$lifetime.' SECOND < NOW()';
    if (@mysql_query($sql, $this->resource)){
      return true;
    }
    $error = 'MySQLSessionStorage cannot delete old sessions';
    throw new Exception($error);
  }

  public function sessionOpen($path, $name){
    $this->resource = mysql_connect('host', 'user', 'pass', true);
    mysql_select_db($this->db, $this->resource);
    return true;
  }

  public function sessionRead($id){
    $id = mysql_real_escape_string($id, $this->resource);

    $sql = 'SELECT '.$this->data_col.' ' .
           'FROM '.$this->table.' ' .
           'WHERE '.$this->id_col.' = \''.$id.'\'';
    $result = @mysql_query($sql, $this->resource);
    if ($result != false && @mysql_num_rows($result) == 1){
      $data = mysql_fetch_row($result);
      return $data[0];
    }else{
      $sql = 'INSERT INTO '.$this->table.' ('.$this->id_col.', ' .
             $this->data_col.', '.$this->time_col.') VALUES (' .
             '\''.$id.'\', \'\', NOW())';

      if (@mysql_query($sql, $this->resource)){
        return '';
      }
      $error = 'MySQLSessionStorage cannot create new record for id "%s"';
      $error = sprintf($error, $id);
      throw new Exception($error);
    }
  }

  public function sessionWrite($id, &$data){
    $id   = mysql_real_escape_string($id, $this->resource);
    $data = mysql_real_escape_string($data, $this->resource);
    $sql = 'UPDATE '.$this->table.' ' .
           'SET '.$this->data_col.' = \''.$data.'\', ' .
           $this->time_col.' = NOW() ' .
           'WHERE '.$this->id_col.' = \''.$id.'\'';

    if (@mysql_query($sql, $this->resource)){
      return true;
    }
    $error = 'MySQLSessionStorage cannot write session data for id "%s"';
    $error = sprintf($error, $id);
    throw new Exception($error);
  }

  public function shutdown(){
  }
}
Piotr M.