Harsh Jain Harsh Jain - 2 months ago 6
PHP Question

How can I pass NOW() function in the array for model argument in codeigniter?

This is my array for insert the data into the database with table name is

borrow_user
and
NOW()
is use for the
avt_date
field name in
borrow_user
table.

How can I pass
NOW()
function in the array for model argument in codeigniter?

$data = array(
'user_name'=>$name,user_name is a field name in table



'user_father_name'=>$this->input->post('father_name'),//user_father is a field name in table


'user_dob'=>$this->input->post('DOB'), //user_dob is a field name in table


'user_email'=>$this->input->post('email'), // user_email is a field name in table


'user_pass'=>md5($pass), //user_pass is a field name in table



'user_address'=>$this->input->post('address'),



'phn'=>$this->input->post('phn'),


'user_type'=>2,

'avt_date'=>NOW()); //now() function is not working given an error .



$this->load->model('login_user');


$regis_id=$this->login_user->regis_b($data);
//model class code

public function regis_b($data)
{

$arr=array('user_email'=>$data['user_email']);


$this->db->insert('borrow_user', $data);


$sel_id=$this->db->select('id')
->from('borrow_user')
->where($arr)
->get();


if($sel_id->num_rows())
{


return $sel_id->row()->id;


}else{


return FALSE;
}


}

Answer

You can't pass an SQL function through the active record insert function like this.

But there are two ways you can get the same result:

1. Set the timestamp in PHP

'avt_date'=>date('Y-m-d H:i:s')

2. Set it just before the insert statement

You'll need to remove avt_date from your $data array. Insert will group the previous set statements with the data passed in to make one full query.

$this->db->set('avt_date', 'NOW()', FALSE);
$this->db->insert('borrow_user', $data);