2017-03-27 54 views
0

UPDATE: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' 和 '陣列' 應該是存在或不存在。非常堅持這個,所以非常感謝你的幫助,我非常感謝。

我想創建一個出席表,它將保存一個人的ID,然後有兩列 - 1爲現在和另一個標記爲缺席。所以我會一次添加多行到數據庫。

我剛剛添加了視圖中的代碼,因爲我知道我的控制器和模型很好,因爲其他輸入字段工作正常。

對於每一行我希望它輸入player_id以及它們是否是presentabsent。在我的數據庫中,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 

); 
+0

你想與性別做什麼? –

+0

@KristjanKica對不起,這只是一個錯字 - 它出席代碼 – Robyn

+0

也許你想關閉輸入標籤之前的這段代碼?不關閉標籤標籤<?php if(isset($ attendance)&& $ attendance ==「yes」)echo「checked」;?> –

回答

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

請刪除表格attr 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" 
    } 
} 

下一個模型調用$this->db->insert_batch()multiple insert

+0

非常感謝你!所有工作出色 – Robyn

+0

很高興能夠幫助 – ichadhr

0

修復輸入

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

謝謝我改變了這一點。我還在模型代碼中添加了incase,這裏還有一個問題,因爲它仍顯示爲試圖插入到我的錯誤日誌中的空值。 – Robyn

0

檢查哪些輸入開始使用echo "<pre>"; print_r($this->input->post()); exit;,然後找到輸入什麼問題發佈到控制器。從單選按鈕中刪除數組。