来自POST

时间:2017-03-27 14:52:36

标签: php arrays codeigniter post null

更新:

    Error Number: 42S22/1054

Unknown column 'Array' in 'field list'

INSERT INTO `attendance` (`event_date`, `event_time`, `event_info`, `player_id`, `is_present`, `notes`) VALUES ('2017-03-08', '11:00:00 AM', 'other', NULL, Array, 'notes')

' NULL'应该是' player_id'和'阵列'应该存在或不存在。非常坚持这一点,所以感谢你们所有的帮助,到目前为止,我真的很感激。

我试图创建一个出勤表,该表可以保存一个人的身份,然后有两列 - 1表示现在,另一列表示缺席。所以我会一次性向数据库中添加多行。

我刚从视图中添加了代码,因为我知道我的控制器和模型很好,因为其他输入字段工作正常。

对于每一行,我希望它输入player_id以及它们是present还是absent。在我的数据库中,is_present为enum yesno。我知道这可能没有接近正确的地方,所以感谢任何建议。

<div class="container-fluid">  
<div class="form-group"> 
        <div class=".col-xs-12 .col-md-6">
          <h2 class="brand-before text-center"></h2>
<table class="table table-sm table-bordered tabular_datable-condensed table-hover" id="playertable" name="player_id"> 

  <tr>
  <th>Player ID</th>
  <th>Player first name</th>
  <th>Player surname</th>
  <th>Add</th>
  </tr>

   <tr class="clickable-row">

<?php foreach ($query->result_array() as $row): {?>
    <tr>
        <td><?php echo $row['player_id'];?></td>

        <td><?php echo $row['player_first_name'];?></td>

        <td><?php echo $row['player_last_name'];?></td>

<td align="left">
 <label>
 <input type="radio" name="attendance[<?php echo $row['player_id']; ?>]" value="Yes" <?php echo set_radio('attendance', 'Yes', TRUE); ?> >Present
 </label>
&emsp;
 <label>
 <input type="radio" name="attendance[<?php echo $row['player_id']; ?>]" value="No" <?php echo set_radio('attendance', 'No', TRUE); ?> >Absent
 </label></td>

    <?php } ?>
<?php endforeach; ?>

</tr>
    </table>

控制器

function add_attendance(){

    $event_date=$this->input->post('event_date');
    $event_time=$this->input->post('event_time');
    $event_info=$this->input->post('event_info');
    $player_id=$this->input->post('player_id');
    $is_present=$this->input->post('attendance');
    $notes=$this->input->post('notes');


    $data = array(
        'event_date'=>$event_date,
        'event_time'=>$event_time,
        'event_info'=>$event_info,
        'player_id'=>$player_id,
        'is_present'=>$is_present,
        'notes'=>$notes

);

3 个答案:

答案 0 :(得分:1)

<table class="table table-sm table-bordered tabular_datable-condensed table-hover" id="playertable" name="player_id">

请删除表格name=player_id

以下为视图

示例
<table>
    <thead>
        <tr>
            <th>Player ID</th>
            <th>Player name</th>
            <th>Attending</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($query->result_array() as $row) : ?>
        <tr>
            <!-- hidden input for player_id -->
            <?php echo form_hidden('player_id[' .$row["player_id"]. ']', $row['player_id']); ?>
            <!-- /hidden input for player_id -->
            <td><?php echo $row['player_id']; ?></td>
            <td><?php echo $row['player_name']; ?></td>
            <td>
                <label>
                    <input type="radio" name="attendance[<?php echo $row['player_id']; ?>]" value="Yes" <?php echo set_radio('attendance', 'Yes', TRUE); ?> >Present
                </label>
                <label>
                    <input type="radio" name="attendance[<?php echo $row['player_id']; ?>]" value="No" <?php echo set_radio('attendance', 'No'); ?> >Absent
                </label>
            </td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

Example

提交后,会发布多个阵列

<强>控制器

public function add()
{
    $player_id = $this->input->post('player_id');
    $is_present = $this->input->post('attendance');

    $i=1;
    $data = array();

    // formating array posts
    foreach ($player_id as $k => $val) {
        $data[] = array('player_id' => $val, 'is_present' => $is_present[$i]);
        $i++;
    }

    echo var_dump($data);
}

这里输出的帖子是:

array(3) {
  [0]=>
  array(2) {
    ["player_id"]=>
    string(1) "1"
    ["is_present"]=>
    string(3) "Yes"
  }
  [1]=>
  array(2) {
    ["player_id"]=>
    string(1) "2"
    ["is_present"]=>
    string(2) "No"
  }
  [2]=>
  array(2) {
    ["player_id"]=>
    string(1) "3"
    ["is_present"]=>
    string(3) "Yes"
  }
}

接下来,multiple insert

的模型调用$this->db->insert_batch()

答案 1 :(得分:0)

修正输入

 <label>
 <input type="radio" name="attendance<?php echo $row['player_id']; ?>" 
 value="yes"<?php if (isset($attendance) && $attendance=="yes") 
 echo "checked";?>  > Present
 </label>

答案 2 :(得分:0)

检查使用echo "<pre>"; print_r($this->input->post()); exit;向控制器发布的输入是什么,然后找到输入问题的最新信息。同时从单选按钮中删除阵列。