简体   繁体   English

CodeIgniter无法更新数据库

[英]CodeIgniter unable to update database

I have a problem regarding the user login in my web application. 关于Web应用程序中的用户登录,我遇到了问题。 What I'm trying to do is to update a particular user's last_online_at value with the current date and time before he / she will be landed to the main page. 我想要做的是在用户登陆到主页之前,用当前日期和时间更新特定用户的last_online_at值。 However, it did not update as I expect it to do so. 但是,它没有像我期望的那样更新。

Here is my code: 这是我的代码:

...

 if($result != FALSE){
    $this->db->select('name');
    $this->db->from('user_type');
    $this->db->where("id ='" . $result[0]->user_type_id . "'");

    $query = $this->db->get();

    $session_data = array('first_name' => $result[0]->first_name,
                          'last_name' => $result[0]->last_name,
                          'username' => $result[0]->username,
                          'email' => $result[0]->email,
                          'id' => $result[0]->user_id,
                          'created_at' => date_parse($result[0]->created_at),
                          'recent_searches' => array(),
                          'food_tray' => array('Sample', 'Test', 'Qwerty'),
                          'logged_in' => TRUE,
                          'user_type' => $query->row()->name,
                          'user_type_id' => $result[0]->user_type_id,
                          'user_privileges' => array());

    $this->session->set_userdata($session_data);

    // LINE OF CODE RESPONSIBLE FOR UPDATING `last_online_at` VALUE //

    $data = array('last_online_at' => date('Y-m-d H:i:s'));

    $this->db->update('user', $data, 'id =' . $session_data['id']);

    /////////////////////////////////////////////////////////////////

    if($session_data['user_type'] == 'Customer'){
        // REDIRECT TO USER PAGE
        redirect(base_url() . "index.php/main");
    }else if($session_data['user_type'] == 'Aggregator' ||
             $session_data['user_type'] == 'Restaurant Owner' ||
             $session_data['user_type'] == 'System Admin'){
        // REDIRECT TO ADMIN DASHBOARD
        redirect(base_url() . "index.php/admin?page_view=admin_dash");
    }               
}

As you can see from the given code snippet, I used the variable $session_data['id'] to substitute as argument value to the where clause in the update query, and that's the one that doesn't work. 从给定的代码片段中可以看到,我使用了变量$session_data['id']作为更新查询中的where子句的参数值,而那是行不通的。 But when I replace it to a static value, for example, 1, it will work smoothly and update record based on the given static value 但是,当我将其替换为静态值(例如1)时,它将正常运行并根据给定的静态值更新记录

Am I doing something wrong with this? 我在做错什么吗? Any help would be appreciated 任何帮助,将不胜感激

So first you need to ensure that $session_data['id'] is has the correct value in it. 因此,首先您需要确保$session_data['id']的值正确。

So before it tries to use print to what exactly contain by $session_data 因此,在尝试使用print来完全包含$session_data包含的内容

echo "<pre>";
print_r($session_data);
echo "</pre>";

I also suggest you use CodeIgniter log. 我还建议您使用CodeIgniter日志。

log_message('debug', 'Your Session Data: '.print_r($session_data, TRUE));
log_message('debug', 'You can see last executed query also: '.print_r($this->db->last_query(), TRUE));

Before using this please enable the log form config. 使用此功能之前,请启用日志表单配置。

This technique helps you to get what happens in your code. 此技术可帮助您了解代码中发生的情况。

And for update, the last login date try this: 对于更新,最后登录日期请尝试以下操作:

$data = array('last_online_at' => date('Y-m-d H:i:s'));
$this->db->where('id', $session_data['id']);
$this->db->update('user', $data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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