如何在codeigniter中使用concat函数和更新?

时间:2017-07-26 09:46:23

标签: php codeigniter

代码:

if($this->input->post('submit'))
{

    $data = array(
            'admin_id' => $this->input->post('admin'), 
            );
    $this->db->select('*');
    $this->db->from('assign_menu_admin');
    $query = $this->db->get();
    $result = $query->result_array();
    foreach ($result as $row) 
    {
        $where = "menu_link = '".$row['menu_link']."'";
        $this->db->where($where);
        $this->db->set('admin_id', "CONCAT(admin_id,',','".$data."')", FALSE); 
        $query = $this->db->update('assign_menu_admin');
        echo $this->db->last_query();
    }
}

在这段代码中,我有一个名为assign_menu_admin和admin_id name列的表。现在,我想使用concat函数来显示我的admin_id,如1,2,3,4,但它显示错误。我该如何解决这个错误?请帮助我。

enter image description here

谢谢

5 个答案:

答案 0 :(得分:0)

您可以找到下面提到的解决方案。

您正在将数据从php传递到数据库,因此您可以使用implode()函数。

<强>语法

  

<强>破灭(分离器,数组)

<强>参数

separator:  Optional. Specifies what to put between the array elements. Default is "" (an empty string)
array    :  Required. The array to join to a string

<强>解决方案

$this->db->set('admin_id', implode(',',$this->input->post('admin')));

如果不起作用,请告诉我。

答案 1 :(得分:0)

使用implode()功能:

 $this->db->set('admin_id', implode(",", $data) ), FALSE); 

这会将每个数组值与COMMA字符粘合在一起并返回一个字符串。

输出将是:

 $this->db->set('admin_id', '1,2,3,4', FALSE); 

答案 2 :(得分:0)

您还可以简化您的工作:

    //columns with data you are updating
    $data = [
       //use php implode function to turn an array into a string with each item separated by a ","
       'admin_id'=>implode(',', $this->input->post('admin'))
    ];

    //you can also cast an array of where statements as well
    $this->db->where('menu_link', $row['menu_link'])
    //table to update, followed by columns and data you are updating 
    ->update('assign_menu_admin', $data);

答案 3 :(得分:0)

更改此行

$this->db->set('admin_id', "CONCAT(admin_id,',','".$data['admin_id']."')", FALSE); 

答案 4 :(得分:0)

您在concat中放置的SELECT ENB.SAP_ID AS SAP_ID,CAND.CANDIDATEID AS CANDIDATE_ID,ENB.R4G_STATE AS STATE,ENB.SITE_TYPE AS SITE_TYPE,CAND.SITENAME AS SITE_NAME, CAND.STATUS AS CANDIDATESTATUS ,ENB.SITEID AS SITEID,ENB.PRIORITY_SITE AS PRIORITYSITE,ENB.CIRCLE AS CIRCLE, DECODE (VTS.STATUS_NAME, null, 'Fill Vendor Survey form', VTS.STATUS_NAME) AS STATUS_NAME, ENB.LATITUDE, ENB.LONGITUDE, VTS.STATUS_ID,VT.VSAT_DETAIL_ID FROM R4G_OSP.ENODEB ENB INNER JOIN R4G_OSP.CANDIDATE CAND ON ENB.SAP_ID = CAND.SAP_ID LEFT JOIN TBL_VSAT_MST_DETAIL VT ON ENB.SAP_ID = VT.SAP_ID LEFT JOIN TBL_VSAT_STATUS_MST VTS ON VT.STATUS_ID = VTS.STATUS_ID WHERE ENB.SCOPE = 'EnodeB-Connected_MW' AND ENB.SITEBACKHAUL = 'MW' AND CAND.STATUS = 'Fill Vendor Survey Form' AND UPPER(STATE) IN (SELECT REGEXP_SUBSTR(UPPER('Bihar'),'[^,]+', 1, LEVEL) AS RFIELDS FROM DUAL CONNECT BY REGEXP_SUBSTR(UPPER('Bihar'), '[^,]+', 1, LEVEL) IS NOT NULL) AND VT.STATUS_ID IS NULL OR VT.STATUS_ID = 4; $data,您不能像这样使用用户数组。

替换此行:

array

这一行:

$this->db->set('admin_id', "CONCAT(admin_id,',','".$data."')", FALSE);