如何使用两个foreach在数据库中插入数据

时间:2018-08-08 13:46:13

标签: php codeigniter codeigniter-3 codeigniter-query-builder

从我的角度来看,我有两个数组。我需要插入彼此对应的数据。

下面是我的视图

<?php foreach ($seats as $seat):?>

        <tr>
            <td><input type="checkbox" name="seat_id[]" value=<?php echo $seat->seatNumber;?>></td>
            <td><?php echo $seat->seatLabel;?></td>
            <td><input type="hidden" name="seat_label[]" value="<?php echo $seat->seatLabel;?>"></td>
        </tr>
        <?php endforeach;?>

从上面的视图来看,seat_id和seat_label带有必须存储在数据库中的值

这是我的控制人

if($this->form_validation->run()){
            $seat_ids = $this->input->post("seat_id[]");
            $seat_labels = $this->input->post("seat_label[]");
            $busNumber = $this->input->post("busNumber");
            $bookingDate = $this->input->post("bookingDate");
            $reportingTime = $this->input->post("reportingTime");
            $departureTime = $this->input->post("departureTime");
            $this->load->model('Queries');
     $insert = $this->Queries->saveMessage($seat_ids, $seat_labels, $busNumber, $bookingDate, $reportingTime, $departureTime);

这是我的模特

public function saveMessage($seat_ids, $seat_labels, $busNumber, $bookingDate, $reportingTime, $departureTime){

    foreach($seat_ids as $seat_id )
          foreach($seat_labels as $seat_label ){
    {
        $record = array(
        'seatNumber' => $seat_id, 
        'seatLabel'  => $seat_label,
        'bookingDate' => $bookingDate,
        'reportingTime' => $reportingTime,
        'departureTime' => $departureTime, 
        'busNumber' => $busNumber,
        'seatUse' => 'Enabled',
        'seatStatus' => 'Available',);
        $this->db->insert('schedule', $record);
    }

5 个答案:

答案 0 :(得分:1)

希望这对您有帮助:

像这样使用insert_batch代替insert

public function saveMessage($seat_ids, $seat_labels, $busNumber, $bookingDate, $reportingTime, $departureTime)
{

    foreach($seat_ids as $key => $seat_id )
    {
            $record[] = array(
            'seatNumber' => $seat_id, 
            'seatLabel'  => $seat_labels[$key],
            'bookingDate' => $bookingDate,
            'reportingTime' => $reportingTime,
            'departureTime' => $departureTime, 
            'busNumber' => $busNumber,
            'seatUse' => 'Enabled',
            'seatStatus' => 'Available');
    }
    $this->db->insert_batch('schedule', $record);
}

更多信息:https://www.codeigniter.com/user_guide/database/query_builder.html#inserting-data

答案 1 :(得分:0)

您的代码应阅读

j

答案 2 :(得分:0)

如果$seat_id$seat_label中的键匹配,则只能使用一个foreach来做到这一点:

foreach($seat_ids as $seat_key => $seat_id ) {
    $record = array(
    'seatNumber' => $seat_id, 
    'seatLabel'  => $seat_label[$seat_key],
    'bookingDate' => $bookingDate,
    'reportingTime' => $reportingTime,
    'departureTime' => $departureTime, 
    'busNumber' => $busNumber,
    'seatUse' => 'Enabled',
    'seatStatus' => 'Available',);
    $this->db->insert('schedule', $record);
}

答案 3 :(得分:0)

考虑到您正在使用JSON提交表单数据和数组之间的顺序,因此得以维持。

您可以尝试:

$i = 0;
foreach($seat_ids as $seat_id )
{
    $record = array(
    'seatNumber' => $seat_id, 
    'seatLabel'  => $seat_labels[$i],
    'bookingDate' => $bookingDate,
    'reportingTime' => $reportingTime,
    'departureTime' => $departureTime, 
    'busNumber' => $busNumber,
    'seatUse' => 'Enabled',
    'seatStatus' => 'Available',);
    $this->db->insert('schedule', $record);
    $i++;
}

答案 4 :(得分:-2)

当前该表格没有座位标签关系。如果您未勾选所有ID,则此后的标签将对应于错误的ID(即ID的发布数组小于标签的数组)

因此,您将必须在表单名称中引入必要的关系:

<input type="checkbox" name="seat[{index}][id]" value="{id}">
<input type="hidden" name="seat[{index}][label]" value="{label}">

如果提交了,您现在可以检查两个框是否都被勾选

// using $_POST for simplicity
$seats = array_filter($_POST['seat'], function (array $row) {
    return count($row) === 2;
});

现在您有了一个带有相应ID /标签的数组,其中只需要一个循环。

编辑:忽略了第二个输入是一个隐藏字段,但这根本不影响问题。