Beep Beep - 5 months ago 8x
SQL Question

Inserting session user data in php


I added the session in the code and did not logout then login to refresh the variable .. my bad, thanks guy.


I am using Codeigniter 3.0, I can read the session data, I have tested this by a echo. however when i try to insert into the table I get this error.


Error Number: 1054

Unknown column 'LoginID' in 'field list'

INSERT INTO `Report_Comments` (`Comments`, `ReportID`, `LoginID`) VALUES (',l;', '53', NULL)

Filename: models/report/Report_model.php

Line Number: 58

Code (Model)

function create_comment()
$new_comment = array(
'Comments' => $this->input->post('Comments'),
'ReportID' => $this->input->post('ReportID'),
'UserID' => $this->session->userdata('LoginID')

$insert = $this->db->insert('Report_Comments', $new_comment);
return $insert;


It is always a good idea to double check that you have values assigned to variables before you use them. This revised model function provides some checking.

  function create_comment()
    $comments = $this->input->post('Comments');
    $reportID = $this->input->post('ReportID');
    $userID = $this->session->userdata('LoginID');
    if(isset($reportID) && isset($userID))
      $new_comment = array(
          'Comments' => isset($comments) ? $comments : "",
          'ReportID' => $reportID,
          'UserID' => $userID
      return $this->db->insert('Report_Comments', $new_comment);
    return FALSE;

The above operates with the idea that the insert should not be attempted unless the ReportID and UserID values are set with some value. If no comments are present then an empty string will be save to the db.