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条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路