Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我需要在数据库中插入一个数组值_Php_Json_Codeigniter - Fatal编程技术网

Php 我需要在数据库中插入一个数组值

Php 我需要在数据库中插入一个数组值,php,json,codeigniter,Php,Json,Codeigniter,我正在使用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 s

我正在使用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>

在这个文件中,我使用了一个函数save将数据保存到我的表中,从输入post中删除
json\u 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);
}
公共功能服务()
$id=$this->session->userdata('employee');
$id_array=$this->input->post(“服务”);
对于($i=0;$i$id,'service\u id'=>$id\u array[$i]);
$this->add\u service\u model->save($data\u to\u store);
}
}
$data['addservice']=$this->add_service_model->get_addservices();
$data['main_content']='admin/service_limition/newview';
$this->load->view('includes/template',$data);
}

你的模型功能在哪里???我编辑了我的帖子。。请参考它显示的错误是什么?我得到了一个白色屏幕。。我找不到插入到我的表中的值。错误号:1048列“服务id”不能为null插入
addservice
employee\u id
service\u id
)值('4534',null)文件名:C:\xampp\htdocs\elfanto\elfanto\billing\system\database\DB\u driver.php行号:330我现在没有收到null错误。但是没有插入我的值。它只能插入1个ID,
$this->input->post(“服务”)
的值是多少,现在您的值开始插入数据库$此->输入->发布(“服务”)用于将id发布到视图中的表单中
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);
}