Only the first row of a PHP PivotTable is written to the MySQL Bank

0

Only the first row of the table is writing to the database, I assume that the problem is in the foreach ( $_POST['data'] as $key => $value) loop. Clicking the "Add" button will insert a new row from the table via JS, in the database an assumption can have several 1: n observations.

Page:

<divclass="form-horizontal">
    <div class="table-responsive table-obs">
        <table class="table table-bordered table-striped table-highlight">
            <thead>
            <div class="row">
                <th class="col-xs-1">Data</th>
                <th class="col-xs-2">Horário</th>
                <th class="col-xs-3">Horas realizadas</th>
                <th class="col-xs-6">Atividades realizadas/Obsercações</th>
            </div>
            </thead>
            <tbody id="tabelaCorpo">
                <tr id="linhaParaClonar">
                    <td><input type="date" class="form-control" name="data[0]"></td>
                    <td><input type="time" class="form-control" name="horario[0]"></td>
                    <td><input type="number" class="form-control" name="horas-realizadas[0]"></td>
                    <td><input type="text" class="form-control " name="obs[0]"></td>
                    <td><input type="button" onclick="removerLinha(this)" class="btnX btn btn-danger" value="X"></td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
<div class="col-sm-offset-10 col-sm-11">
    <input type="button" onclick="adicionarLinha()" value="Adicionar" class="btn btn-success"/>
</div>

<div class="row" id="box-cinza-inferior">
    <div class="col-lg-offset-1 col-sm-2">
        <a href="controle-de-fluxo.php" class="btn btn-success">Voltar</a>
    </div>
    <div class="col-lg-offset-9">
        <button type="submit" name="btnConcluir" value="0" class="btn btn-success">Salvar</button>
        <button type="submit" name="btnConcluir" value="1" class="btn btn-success">Concluir</button>
    </div>
</div>

Index.js

//Adicionar nova linha na tabela Assiduidade
var max = 10;   //max de 10 campos
var z = 1;
function adicionarLinha() {
    if (z <= max) {
        $('#tabelaCorpo').append('<tr id="linha">\
            <td><input type="date" name="data[' + z + ']" class="form-control" /></td>\
            <td><input type="time" name="horario[' + z + ']" class="form-control" /></td>\
            <td><input type="number" name="horas-realizadas[' + z + ']" class="form-control" /></td>\
            <td><input type="text" name="obs[' + z + ']" class="form-control " /></td>\n\
            <td><input type="button" onclick="removerLinha(this)" class="btnX btn btn-danger" value="X"></td>\
        </tr>');
        z++;
    }
}

Received-assiduity.php

The other fields I am able to receive normally, but the table data is writing only the first row in the database

// vincular alunos a tabela observações
foreach ( $_POST['data'] as $key => $value) {
    $data = $_POST['data'][$key];
    $horario = $_POST['horario'][$key];
    $horario = $horario . ":00";
    $horas = $_POST['horas-realizadas'][$key];
    $horas = $horas . ":00:00";
    $obs = $_POST['obs'][$key];

    //echo $data." - ".$horario." - ".$horas." - ".$obs."<br>";

    $sql_code_2 = "INSERT INTO observacoes (id_assiduidade, hr_realizadas, dt, hr_inicio, obs) VALUES ('$id_assiduidade', '$horas', '$data','$horario', '$obs')";
    $sql_query_2 = $mysqli->query($sql_code_2) or die($mysqli->error);
}
    
asked by anonymous 26.11.2017 / 20:20

1 answer

0

I've been able to solve it, I'll let it be documented if anyone has the same problem.

The solution was to rearrange the HTML structure hierarchy, it looked like this:

<div> <div> <div> <div> <form> </div> </div> </div> </form> </div>

The solution was to open and close all divs within the form

    
20.12.2017 / 18:07