【问题标题】:How to insert data in Json Format in Codeigniter如何在 Codeigniter 中插入 Json 格式的数据
【发布时间】:2019-07-09 11:27:12
【问题描述】:

我的数据库中有 3 个字段,我想逐行添加数据,请帮我在数据库中添加这些数据。

【问题讨论】:

    标签: arrays json codeigniter codeigniter-3


    【解决方案1】:

    使用此示例将 json 数据插入数据库表中

    $data = array(
        'name'=>$this->input->post('name'),
        'mobile'=>$this->input->post('phone_number'),
        'email'=>$this->input->post('email'),
        'message'=>$this->input->post('message'),
        'contact_for'=>$this->input->post('user_type'),
        'ip_address'=>$this->input->ip_address(),
        'browser_info'=>$this->agent->browser().' - '.$this->agent->version(),
     ); 
    
      $json = json_encode($data);
      $insertField = array('json_field' => $json);
    
      $this->db->insert('tbl_name', $insertField);
    
      if ($this->db->affected_rows() > 0) {
         $insert_id = $this->db->insert_id();
         return $insert_id;
      }
      return false;
    

    【讨论】:

      【解决方案2】:

      你可以使用

      $json = json_encode($data);
      

      https://www.php.net/manual/en/function.json-encode.php

      【讨论】:

        【解决方案3】:
        $response = array('status' => 'OK');
        
        $this->output
                ->set_status_header(200)
                ->set_content_type('application/json', 'utf-8')
                ->set_output(json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES))
            ->_display();
        exit;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-07-02
          • 1970-01-01
          • 2018-12-21
          • 1970-01-01
          • 2021-02-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多