【问题标题】:Inserting session user data in php在php中插入会话用户数据
【发布时间】:2016-04-27 20:36:11
【问题描述】:

----------------开始-更新-----

我在代码中添加了会话并且没有注销然后登录以刷新变量.. 我的错,谢谢家伙。

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

我正在使用 Codeigniter 3.0,我可以读取会话数据,我已经通过回显测试了这一点。但是,当我尝试插入表格时,我得到了这个错误。

错误

  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

代码(模型)

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;
    }

【问题讨论】:

  • 您的Report_Comments 表中有LoginId 字段吗?
  • 是的,因为这里有 'UserID' => $this->session->userdata('LoginID') 而不是 LoginID
  • 如果有效则很难修复。哈哈。可能是会话超时。我将提供一个有助于发现缺失输入问题的答案。
  • 喝点啤酒 LMAO :D
  • 在调用此模型/方法的控制器中确保用户早日登录是一个非常好的主意

标签: php sql codeigniter model-view-controller


【解决方案1】:

在使用变量之前仔细检查变量是否已赋值总是一个好主意。这个修改后的模型函数提供了一些检查。

  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;
  }

上述操作的想法是,除非ReportIDUserID 值设置为某个值,否则不应尝试插入。如果不存在 cmets,则将一个空字符串保存到数据库中。

【讨论】:

  • 不错的提示,谢谢老兄,质疑返回 false 我可以返回警告框吗?
  • 我建议控制器对模型的返回进行操作。但是是的,控制器可以提供一些关于未能保存评论的反馈。
猜你喜欢
  • 2016-05-31
  • 2017-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-19
  • 1970-01-01
相关资源
最近更新 更多