Codeigniter-另一个表的设置id等于另一个表的id

时间:2018-01-31 10:15:20

标签: php mysql codeigniter

我坚持将现有客户ID设置为等于购买ID。这是入门的典范。

在else块上,错误显示:

  

"消息:无法转换类CI_DB_mysqli_result的对象   字符串"

此行上的错误:"$purchase['cust_id'] = $cust;"

我是php和Codeigniter的初学者

    Class Entry_m extends CI_Controller{
    public function create_submit($customer, $purchase){

    $array = array('lname' => $customer['lname'], 'fname' => $customer['fname'], 'mn' => $customer['mn']);
    $this->db->like($array);
    $query = $this->db->get('customer');
    $count = $query->num_rows();
    if($count ===0)
    {
        $this->db->insert('customer', $customer);
        $cust_id = $this->db->insert_id();

        $purchase['cust_id'] = $cust_id;
        $this->db->insert('purchase', $purchase);
        return $cust_id = $this->db->insert_id();
    }else{
        $cust = $this->db->select('cust_id')->where($array)->get('customer');
        $purchase['cust_id'] = $cust;
        $this->db->insert('purchase', $purchase);   
    }
}

}

2 个答案:

答案 0 :(得分:0)

相反

    $this->db->insert('customer', $customer);
    $cust_id = $this->db->insert_id();

使用

    $cust_id = $this->db->insert_id('customer', $customer);

答案 1 :(得分:0)

试试这个

$cust = $this->db->select('cust_id')->where($array)->get('customer')->row()->cust_id;