简体   繁体   English

在数据库中提交数组类型输入文件

[英]Submit Array type input file in database

I have a form, which has two fields: "product_name" and "product_q" and in my form i have an option to increase the number of fields in product_name and product_q 我有一个表单,其中有两个字段:“ product_name”和“ product_q”,在我的表单中,我可以选择增加product_nameproduct_q的字段数

 <select name="product_name[]" id="product_name">
 <option value="please select"></option> 
 <option value="1">demo1</option> 
 <option value="2">demo2</option> 

 </select>

<select name="product_q[]" id="product_q">
 <option value="please select"></option> 
 <option value="1">1</option> 
 <option value="2">2</option> 

 </select>

and an action page like 还有一个动作页面

 public function order(){


    $Product_q = $this->input->post('quantity');
    $sponserid = $this->session->userdata('number');
    $pname['product_name'] = $this->input->post('product_name');
    foreach($Product_q as $key => $value){
    $data['Product_q'] = $value;
    $data['Product_Name'] = $pname['product_name'][$key]; 
    $this->mymodel->insert_items($data);

    }

and my model 和我的模特

function insert_items($data){
    $this->db->insert("lucky_order", $data);
    return;

}

Please help to insert data database my form look like this: 请帮助将数据数据库插入我的表单,如下所示:

FORM with one field 一份表格
一份表格

Form with two field 两场形式
两场形式

We cannot insert array directly into database so before inserting it to database we need to encode array into json. 我们无法将数组直接插入数据库,因此在将其插入数据库之前,我们需要将数组编码为json。 You can do it by following <?php json_encode($data); ?> 您可以通过<?php json_encode($data); ?> <?php json_encode($data); ?> and then later where you want fetch this data then you can simply decode that data back to array by using following method <?php json_decode($data); ?> <?php json_encode($data); ?> ,然后在以后要获取此数据的位置,则可以使用以下方法简单地将该数据解码回数组: <?php json_decode($data); ?> <?php json_decode($data); ?>

you can try this : 你可以试试这个:

<select name="product_name[]" class="product_name">
 <option value="please select"></option> 
 <option value="1">demo1</option> 
 <option value="2">demo2</option> 

 </select>

<select name="product_q[]" class="product_q">
 <option value="please select"></option> 
 <option value="1">1</option> 
 <option value="2">2</option> 

 </select> 
<input type="hidden" name="product_count" value="1" />



You have to increment /decrement the product_count value on addition 
or removal or product 

for controller : 对于控制器:

 loop your function in model for inserting into database 

for times of product_count and the values for each using $key something like this : 对于product_count的时间以及每个使用$ key的值,如下所示:

        for ($i=0; $i<$_POST['product_count']; $i++){

call the function of insert by passing the each value using above $i variable. 通过使用$ i变量传递每个值来调用insert函数。 like 喜欢

         $_POST['product_name'][$i]; 

this will get you your first product name similarly for all 
   and you can call the model function and pass the above values each 
time with incremented value of $i 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM