如何在数据库中插入多个下拉值?

时间:2019-05-06 05:31:09

标签: codeigniter

我的html中有许多下拉列表,我想从数据库中的某个下拉列表中发送每个值(我打开了该下拉列表,并选择了应该使用该下拉名称的任何值)。

数据库将这样显示,该用户从“ ABC下拉菜单”中选择了值1,从“ XYZ”下拉菜单中选择了值3,依此类推,而那些我没有选择的下拉菜单不应进入数据库。

 <pre>
    <tr class="evenrow">
        <td align="center"><input type="hidden" name="report_id[]" id="report_id" value="1">1</td>
        <td>Customer Balances</td>
        <td>
            <select  name="printer[]" id="printer" class="form-control" title="">
                <option value="">Browser support</option>
                <option value="1">QL500&nbsp;-&nbsp;Label printer</option>
                <option value="2">Samsung&nbsp;-&nbsp;Main network printer</option>
                <option value="3">Local&nbsp;-&nbsp;Local print server at user IP</option>
            </select>
        </td>
    </tr>

    <tr class="evenrow">
        <td align="center"><input type="hidden" name="report_id[]" id="report_id" value="2">2</td>
        <td>Customer Balances</td>
        <td>
            <select  name="printer[]" id="printer" class="form-control" title="">
                <option value="">Browser support</option>
                <option value="1">QL500&nbsp;-&nbsp;Label printer</option>
                <option value="2">Samsung&nbsp;-&nbsp;Main network printer</option>
                <option value="3">Local&nbsp;-&nbsp;Local print server at user IP</option>
            </select>

        </td>
    </tr>

    <tr class="evenrow">
        <td align="center"><input type="hidden" name="report_id[]" id="report_id" value="3">3</td>
        <td>Customer Balances</td>
        <td>
            <select  name="printer[]" id="printer" class="form-control" title="">
                <option value="">Browser support</option>
                <option value="1">QL500&nbsp;-&nbsp;Label printer</option>
                <option value="2">Samsung&nbsp;-&nbsp;Main network printer</option>
                <option value="3">Local&nbsp;-&nbsp;Local print server at user IP</option>
            </select>

        </td>
    </tr>

        <tr class="evenrow">
        <td align="center"><input type="hidden" name="report_id[]" id="report_id" value="4">4</td>
        <td>Customer Balances</td>
        <td>
            <select  name="printer[]" id="printer" class="form-control" title="">
                <option value="">Browser support</option>
                <option value="1">QL500&nbsp;-&nbsp;Label printer</option>
                <option value="2">Samsung&nbsp;-&nbsp;Main network printer</option>
                <option value="3">Local&nbsp;-&nbsp;Local print server at user IP</option>
            </select>

        </td>
    </tr>


    </table>



    </div>

    <!-- /.box-body -->
    </div>
       <center>
            <div class="input-group">
                <input type="submit" name="submit" id="submit" value="Add New" class="btn btn-info pull-right">
            </div>
    </center>
    </pre>

2 个答案:

答案 0 :(得分:0)

为什么不使用不同的名称而不是完全相同的名称。

Report1, Printer1
Report2, Printer2
Report3, Printer3

在控制器中,您可以通过名称来捕获它们...

<table>
   <tr class="evenrow">
        <td align="center"><input type="hidden" name="report_id1" id="report_id" value="1">1</td>
        <td>Customer Balances</td>
        <td>
            <select  name="printer1" id="printer" class="form-control" title="">
                <option value="">Browser Support</option>
                <option value="1">Text</option>
                <option value="2">Text</option>
                <option value="3">Text</option>
            </select>
        </td>
    </tr>

    <tr class="evenrow">
        <td align="center"><input type="hidden" name="report_id2" id="report_id" value="2">2</td>
        <td>Customer Balances</td>
        <td>
            <select  name="printer2" id="printer" class="form-control" title="">
                <option value="">Browser Support</option>
                <option value="1">Text</option>
                <option value="2">Text</option>
                <option value="3">Text</option>
            </select>

        </td>
    </tr>
    </table>

然后在控制器中:

$report1 = $this->input->post('report1');
$report2 = $this->input->post('report2');
$printer1= $this->input->post('printer1');
$printer2= $this->input->post('printer2');

答案 1 :(得分:0)

您使用数组作为选择框名称,并且所有三个选择框都具有相同的名称,因此,如果要存储所有不同的节值,请更改选择框名称,以便在控制器中获得2个不同的数组,或者可以轻松地从哪个节中识别它有一个客户json

$selected_option= array("section1"=>$this->input->post('printer1',"section2"=>$this->input->post('printer2'); $selected_option_json= json_encode($selected_option); 将$ selected_option_json(带有json字符串)存储在db

希望它对您有帮助。