【问题标题】:I need to insert a array value in the data base我需要在数据库中插入一个数组值
【发布时间】:2015-07-04 10:02:26
【问题描述】:

我正在使用 codeigniter 框架。我有一个 id 数组,要保存在表的 service_id 列中。我需要在我的表中存储一个 id 数组。我的表结构如下所示。

CREATE TABLE IF NOT EXISTS `addservice` (
  `employee_id` int(10) NOT NULL,
  `service_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

我为此写了一个控制器:

class service extends CI_Controller
{
public function services()
    {

    $id = $this->session->userdata('employee');
    $id_array = json_encode($this->input->post("services"));
    $data_to_store=array('employee_id'=>$id,'service_id'=>$id_array));
    $this->add_service_model->save($data_to_store);
    $data['addservice'] =    $this->add_service_model->get_addservices();   
            $data['main_content'] = 'admin/service_limitation/newview';
        $this->load->view('includes/template', $data);  

    }
}
 I used json_encode function. I dont get any error but i am getting a white screen. I wrote a view file where i post my id as check box.In this view file i am posting the service_id as a array and post them to my controller. My view file are:
<table class="table table-striped table-bordered table-condensed">
            <thead>
              <tr>
                <th class="header">Service id</th>
                <th class="yellow header headerSortDown">Service name </th>
                <th class="green header">Service catogary</th>
                <th class="red header">Service tax</th>
                <th class="red header">Service length</th>
                <th class="red header">Service price</th>
                <th class="red header">Actions</th>
              </tr>
            </thead>
            <tbody>
              <?php
              foreach($service as $row)
              {
                echo '<tr>';
                echo '<td>'.$row['id'].'</td>';
                echo '<td>'.$row['service_name'].'</td>';
                echo '<td>'.$row['category'].'</td>';
                echo '<td>'.$row['service_tax'].'</td>';
                echo '<td>'.$row['service_length'].'</td>';
                echo '<td>'.$row['service_price'].'</td>';
                echo '<td class="crud-actions">
                 <input type="checkbox" value="'.$row['id'].'" name="services[]"/>

                </td>';
                echo '</tr>';
              }
              ?>      
            </tbody>
          </table>

我的模型文件

class mymodel extends CI_Model{

public function get_addservices()
    {
    $this->db->select('*');
        $this->db->from('addservice');
    $query = $this->db->get();
        return $query->result_array(); 
    }
        public function save($data)
    {
    $insert = $this->db->insert('addservice', $data);
        return $insert;

    }
}

在这个文件中,我使用了一个函数 save 将数据保存到我的表中

【问题讨论】:

  • 你的模型函数在哪里???
  • 我编辑了我的帖子.. 请参考
  • 显示的错误是什么?
  • 我得到一个白屏..我发现我的表中没有插入任何值。

标签: php json codeigniter


【解决方案1】:

从输入帖子中删除json_encode并使用for循环获取数组的值

控制器

public function services()
$id = $this->session->userdata('employee');
$id_array = $this->input->post("services");
for ($i = 0; $i < count($id_array); $i++) {// add for loop here
    if(isset($id_array[$i])&& $id_array[$i]!=""){
    $data_to_store = array('employee_id' => $id, 'service_id' => $id_array[$i]);
    $this->add_service_model->save($data_to_store);
   }
}
$data['addservice'] = $this->add_service_model->get_addservices();
$data['main_content'] = 'admin/service_limitation/newview';
$this->load->view('includes/template', $data);
}

【讨论】:

  • 错误号:1048 列 'service_id' 不能为空 INSERT INTO addserviceemployee_idservice_id)值('4534',NULL)文件名:C:\xampp\htdocs\ elfanto\elfanto_billing\system\database\DB_driver.php 行号:330
  • 我现在没有收到 NULL 错误。但我的价值观没有被插入。它只能插入 1 个 id
  • $this-&gt;input-&gt;post("services")的值是多少
  • 现在你的值开始插入你的数据库!!
  • $this->input->post("services") 用于获取以我认为的形式发布的 id
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-13
  • 1970-01-01
相关资源
最近更新 更多