i am trying to insert multiple check box data in one time in rest api web service. But i can insert only one data. This is my controller method
public function insert_code_post(){
$user_id = $this->input->post('user_id');
$code_id = $this->input->post('code_id');
$records = array();
for($i=0; $i < count($code_id); $i++){
$records[] = array('user_id' => $user_id, 'code_id' => $code_id[$i]);
}
$res = $this->my_model->insert_data($records);
if($res)
{
$data= array();
$data["error"]= false;
$data["status"] = 200;
$data["message"] = "Inserted...";
$this->response($data, 200);
}
}
model method
public function insert_data($data)
{
foreach($data as $key => $value){
$query = "insert into code (user_id, code_id) values ('".$value['user_id']."','".$value['code_id']."')";
return $this->db->query($query);
}
}
this is my client code, i use curl with simple html fields for test
<?php
if(!empty($_POST['check_list'])) {
$baseUrl = 'http://localhost/api/my_controller/insert_code';
$params = array();
foreach($_POST['check_list'] as $check) {
//echo $check;
$params = array(
"user_id" => '2',
"code_id" => $check);
}
$curl = curl_init() or die(curl_error());
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_URL, $baseUrl);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
$jobId = curl_exec($curl);
echo $jobId;
curl_close($curl);
}
?>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
</head>
<body>
<form action="insert_multiple_CI.php" method="post">
<input type="checkbox" name="check_list[]" value="1">
<input type="checkbox" name="check_list[]" value="2">
<input type="checkbox" name="check_list[]" value="3">
<input type="checkbox" name="check_list[]" value="4">
<input type="checkbox" name="check_list[]" value="5">
<input type="submit" />
</form>
</body>
</html>
Someone suggest me how do i able to insert multiple data to my api, i tried many methods and also check related question in forum but not able solve.