简体   繁体   中英

Inserting session user data in php

---------------START-UPDATE-----------------

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

---------------END-UPDATE-----------------

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

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM