【问题标题】:database insert failure in codeignitercodeigniter 中的数据库插入失败
【发布时间】:2016-10-11 10:59:54
【问题描述】:

我正在尝试将我的数组值发送到数据库。但是有一个数据库问题错误信息。消息显示 - 'field list' 中的未知列 'Array' 。我的代码有什么问题?

来自页面:

 <form action="<?php echo base_url()?>Welcome/form_data" method="post">

<?php
$x=01;

for ($i = 0; $i <= 7; $i++) {  
    ?>

<label><?php echo $x++ ?>.</label>
<input type="text" value="<?php echo('L1' . rand(0, 10000000000) . 'MLM' . rand() . '00'); ?>" readonly="" name="screct_pin[]">
<input type="number" value="13<?php echo $x++ ?>5" name="user_id[]">
<br>


<?php }?>


<br>
<input type="submit">

控制器

public function form_data(){

    $this->WelcomeModel->form_data_info();
    redirect('Welcome');

}

型号:

public function form_data_info() {

    $data = array();
    $data['screct_pin'] = $this->input->post('screct_pin');
    $data['user_id'] = $this->input->post('user_id');

    $this->db->insert('tbl_pin',$data);
} 

【问题讨论】:

    标签: php mysql database codeigniter


    【解决方案1】:

    这里你在值中插入数组,所以 PHP 不能在列中存储数组。 所以在这里你需要遍历循环中的每个值并在数据库中插入值,如下所示..

    <?php
    
    public function form_data_info() 
    {
    
        $screct_pin_array = $this->input->post('screct_pin');
        $user_id_array = $this->input->post('user_id');
        for($i = 0; $i<= count($screct_pin_array); $i++)
        {
           $data = array();
           $data['screct_pin'] = $screct_pin_array[$i];
           $data['user_id'] = $user_id_array[$i];
           $this->db->insert('tbl_pin',$data);
        }   
    } 
    
    ?>
    

    【讨论】:

    • 谢谢。你让我开心
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    • 2021-04-09
    • 2018-01-17
    • 1970-01-01
    相关资源
    最近更新 更多