dongshijiao2363 2015-06-09 10:21
浏览 55
已采纳

无法在mysql中插入多个复选框数据

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.

  • 写回答

2条回答 默认 最新

  • doupu1957 2015-06-09 18:42
    关注

    I think there is problem with preparing request.

    <?php
    
    if(!empty($_POST['check_list'])) {
        $baseUrl = 'http://localhost/api/my_controller/insert_code';
    
        // I see you are iterating code_id in rest handler. so it should be array.
        $params = array("user_id" => '2',
                        "code_id" => array());
        foreach($_POST['check_list'] as $check) {
                //echo $check; 
                $params["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);          
    
    }
    
    ?>
    

    You can also update your model code as suggested by Saty on above code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统
  • ¥15 关于wkernell.PDB加载的问题,如何解决?(语言-c#|开发工具-vscode)
  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥20 搭建awx,试了很多版本都有错