【问题标题】:Pass array from checkboxes and insert multiple rows to database CodeIgniter从复选框传递数组并将多行插入数据库 CodeIgniter
【发布时间】:2018-09-23 01:34:56
【问题描述】:

这是我的看法:

<?php foreach ($list_peserta as $show): ?>
                <label>
                <input type="checkbox" name="undangan[]" value="<?php echo $show->email ?>" />
                <?php echo $show->nama ?></label>
              <?php endforeach; ?>
            </div>
          </div>
          <div class="form-actions">
            <input type="submit" value="Kirim" class="btn btn-info">
          </div>
          <?php echo form_close(); ?>

我的控制器:

$data   =   array();
        $count  =   count($this->input->get_post['undangan']);
        for ($i = 0; $i <= $count ; $i++) {
            $data[] =   array(  
                                'id_acara'      =>  $this->input->post['id'][$i],
                                'email_peserta' =>  $this->input->post['undangan'][$i],
                                'status'        =>  ['Diundang'][$i]
                            );

            $this->db->insert_batch('kehadiran', $data);

我收到此错误: enter image description here

请帮忙!

【问题讨论】:

  • 请避免使用链接和/或图片来提供内容、代码和错误。如果您将文本复制粘贴到您的问题中,您的问题将会得到改善。通常错误消息被格式化为引号。

标签: php arrays codeigniter checkbox


【解决方案1】:

我认为你在这行中有一个错字。不要使用方括号,否则它看起来像数组引用。

$count  =   count($this->input->get_post['undangan']);

我觉得应该是括号……

$count  =   count($this->input->get_post('undangan'));

【讨论】:

    【解决方案2】:

    所以你得到一个错误,基本上说$this-&gt;input-&gt;get_post['undangan'] 不是一个数组。您首先应该检查var_dump 给您带来的价值。

    您可能会注意到您的 get post 语法不正确:

    https://www.codeigniter.com/user_guide/libraries/input.html#CI_Input::get_post

    $this-&gt;input-&gt;get_post(‘some_data’, TRUE);

    遵循该方案后,您应该不会再遇到问题了。如果您仍然是,则需要验证变量是否正在发送。当我不知道变量是 get 还是 post 时,我不知道有什么情况,所以我只会将其设为其中之一,而不是使用 get_post

    通常,验证您的输入也是一个好主意。

    $var = $this->input->post('somevar');
    
    if (is_null($var)) {
        show_error('missing params');
    } else {
        // your logic
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-23
      • 2012-04-23
      • 1970-01-01
      • 2012-02-07
      相关资源
      最近更新 更多