Only the first row of a PHP dynamic table is saved in Mysql Database

Asked

Viewed 158 times

0

Only the first row of the table is recording in the database, I notice that the problem is in the loop foreach ( $_POST['data'] as $key => $value). By clicking the "Add" button a new row of the table will be inserted through JS, in the database an assiduidade can have several observations 1:n.

inserir a descrição da imagem aqui

Page:

<div class="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++;
    }
}

Receive-assiduity.php

The other fields I’m able to receive normally, but the table data is only recording 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);
}
  • 8just send a line even, you have to create a loop for that

  • That is not what happened here, see http://kithomepage.com/sos/gravado.PNG

  • You can test here http://kithomepage.com/sos/primeiraLinha.php

  • Leo, an assiduity can have several observations, i.e., a 1:n table, in his example each observation has a different id in the assiduity.

  • 1

    If you want to enter multiple values, use Prepared statements taking advantage that in mysqli they are real (and not simulated, as those of PDO were long by default), or else generate multiple VALUES inputs at once. Either of the two solutions is better than repeating the darlings every row.

1 answer

0


I managed to solve, I will leave documented in case someone goes through the same problem.

The solution was to fix the hierarchy of the HTML structure, it was like this:

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.