如何使用codeigniter在数据库中保存数据?

时间:2017-07-22 12:42:54

标签: database codeigniter-3

public function registration_insert($data) {
    //$this->db->trans_start();
    //echo "<pre>";print_r($_POST);die;

// Query to check whether username already exist or not
$condition = "user_name =" . "'" . $data['user_name'] . "'";
$this->db->select('*');
$this->db->from('user_login');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
// Query to insert data in database
$this->db->insert('user_login', $data);
if ($this->db->affected_rows() > 0) {
return true;
}

这是我的计划。它没有执行此代码。我想将数据存储在数据库中。明确地,使用print_r(),它会发布数据但不会将其存储在数据库中,并且显示错误。

1 个答案:

答案 0 :(得分:0)

首先,我想检查你的$ data变量,如果$ data不是一个具有相同属性名的对象,就像你的数据库表列那样会给你一个问题。

我的意思是,例如,如果你的user_login表有拖曳列(id,user_name),你应该在$ data对象中有相同的属性名称

并验证您的查询是否正常只是您的代码将是这样的:

$q = $this->db->insert('user_login', $data);
 if ($q) {
        return $this->db->insert_id();
    } else {
        return false;
    }