insert in the table only the line that receives value in the date column

1

I have this code:

<?php
$result_cursos = "SELECT nome,
                         Quarto 

FROM centrodb.utentes

WHERE descricaovalencia = 'LAR' AND nome <> 'CLASSE' AND ativo = '1' ORDER BY nome ASC;";

    $resultado_cursos = mysqli_query($conn, $result_cursos);

$tabela1 .= '<div style="float: center" table align="center">';

$tabela1 .= '<table border="5">';

$tabela1 .= '<tr>';

$tabela1 .='<thead>';

$tabela1 .= '<tr>';

$tabela1 .= '<th WIDTH="200" text-align="center">Utente</th>';

$tabela1 .= '<th WIDTH="30" text-align="center">Quarto</th>';

$tabela1 .= '<th WIDTH="80" text-align="center">Data Registo</th>';

$tabela1 .= '<th WIDTH="80" text-align="center">Micção</th>';

$tabela1 .= '<th WIDTH="80" text-align="center">Dejecção</th>';

$tabela1 .= '<th WIDTH="80" text-align="center">Colaborador</th>';

$tabela1 .= '</tr>';

$tabela1 .='</thead>'; 

$tabela1 .='<tbody>';

while($rows_cursos = mysqli_fetch_array($resultado_cursos)) {

$tabela1 .= '<tr>';

$tabela1 .= '<td WIDTH="200" align="center"> <input type="text"  name= "NomeUtente" id= "NomeUtente" value="'.$rows_cursos['nome'].'"></td>';

$tabela1 .= '<td WIDTH="30" align="center"> <input type="text" style="WIDTH="30" align="center" name= "Quarto" id= "Quarto" value="'.$rows_cursos['Quarto'].'"></td>';

$tabela1 .= '<td WIDTH="80" align="center"> <input type="date" name= "DataRegisto" value="echo date("Y-m-d")"</td>';

$tabela1 .= '<td WIDTH="80" align="center"> <input type="checkbox" name= "Miccao" value="Realizado"></td>';

$tabela1 .= '<td WIDTH="80" align="center"> <input type="checkbox" name= "Dejeccao" value="Realizado"></td>';

    $tabela1 .= '<td WIDTH="80" align="center"> <select name="Colaborador" id="Colaborador">
   <option></option>
    <option value="1">teste</option>

$tabela1 .= '</tr>'; 
}
$tabela1 .= '</tr>';

$tabela1 .='</tbody>'; 

$tabela1 .= '</table>';

$tabela1 .= '</div>';

echo "<form method='POST' action=''>";
echo $tabela1;   

echo "<input type='submit' name='registar' value='Registo'>";

echo "</form>";

echo "</br>";
echo "</br>";

?>

Now I use this code to insert into the database table:

<?php
if(isset($_POST['registar']))
{
$utente = $_POST['NomeUtente'];
$quarto = $_POST['Quarto'];
$data = $_POST['DataRegisto'];
$miccao = $_POST['Miccao'];
$dejeccao = $_POST['Dejeccao'];
$colaborador = $_POST['Colaborador'];

    $sql = "INSERT INTO registoMiDe (NomeUtente, Quarto, DataRegisto, Miccao, Dejeccao, Colaborador) VALUES ('$utente', '$quarto', '$data', '$miccao', '$dejeccao', '$colaborador' )";

    if ($conn->query($sql) === TRUE);

    //Count total number of rows
    $rowCount = $query->num_rows;

}

?>

The problem is that it only inserts if you fill in the last line of the table that shows in the image. I intend it to insert the rows that I fill in the table of the image and insert the fields all of the line. I'm not doing update , I really want to insert into

    
asked by anonymous 22.03.2018 / 19:15

1 answer

1

Well, you have a single command, it is only natural that you enter 1x. If you have multiple rows in the table and want to insert all you need to execute multiple inserts , or run the command multiple times within a loop .

You need to rename the controls to say that it is an array , for example name="NomeUtente[]" . Do this with all other fields.

Then you make a loop , with for for example, counting how much data there is, using count . The code would look something like:

for ($i=0;$i<count($_POST["NomeUtente"]);$i++) {
   $utente = $_POST['NomeUtente'][$i];
   // faça o mesmo com os outros campos, usando o índice $i ...

   $sql = "INSERT INTO registoMiDe (NomeUtente, Quarto, DataRegisto, Miccao, Dejeccao, Colaborador) VALUES ('$utente', '$quarto', '$data', '$miccao', '$dejeccao', '$colaborador' )";

   // dai executa o $sql inserindo cada dado ...
}

You can also generate a single command with multiple values and run insert once, see this other question if you are interested: How to write multiple records to a table at once MySQL

    
23.03.2018 / 12:16