I have this code:
<?php
$j=0;
while($rows_cursos = mysqli_fetch_array($resultado_cursos)) {
$tabela1 .= '<tr>';
$tabela1 .= '<td> <input type="text" size="45" name= "NomeUtente[]" id= "NomeUtente" value="'.$rows_cursos['nome'].'"></td>';
$tabela1 .= '<td> <input type="text" size="1" name= "Quarto[]" id= "Quarto" value="'.$rows_cursos['Quarto'].'"></td>';
$tabela1 .= '<td> <input type="datetime" name= "DataRegisto[]" id= "DataRegisto" value="'. date("Y-m-d H:i:s") .'"></td>';
$tabela1 .= '<td> <input type="checkbox" name= "Miccao['.$j.']"> Realizado <input type="text" name= "Tipo1[]" id= "Tipo1" size="30" ></td>';
$tabela1 .= '<td> <input type="checkbox" name= "Dejeccao['.$j.']"> Realizado <input type="text" name= "Tipo[]" id= "Tipo" size="30" ></td>';
$tabela1 .= '<td> <select name="Colaborador[]" id="Colaborador">
<option value="xxxxxxxxxxxx">xxxxxxxxxxxxx</option>
</select></td>';
$tabela1 .= '</tr>';
$j++;
}
$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>";
?>
To insert I have the following:
<?php
if(isset($_POST['registar']))
{
$NomeUtente = $_POST['NomeUtente'];
for ($i=0;$i<count($_POST["NomeUtente"]);$i++) {
$utente = $_POST['NomeUtente'][$i];
$quarto = $_POST['Quarto'][$i];
$data = $_POST['DataRegisto'][$i];
$miccao = $_POST['Miccao'][$i];
$tipo1 = $_POST['Tipo1'][$i];
$dejeccao = $_POST['Dejeccao'][$i];
$tipo = $_POST['Tipo'][$i];
$colaborador = $_POST['Colaborador'][$i];
$miccao = $miccao == "on" ? "Realizado" : "";
$dejeccao = $dejeccao == "on" ? "Realizado" : "";
$sql = "INSERT INTO registoMiDe (NomeUtente, Quarto, DataRegisto, Miccao, Tipo1, Dejeccao, Tipo, Colaborador) VALUES ('$utente', '$quarto', '$data', '$hora', '$miccao', '$tipo1', '$dejeccao', '$tipo', '$colaborador')";
$res = mysqli_query($conn,$sql);
}
}
?>
Now I should only insert in the table the rows in which I select one or both checkboxes, but this is inserting the rows all when I make register in the table of the database. It was working fine, as long as it did not display the value in the DataRegist field
This was the change I made and it did not work:
$tabela1 .= '<td> <input type="datetime-local" name= "DataRegisto[]" id= "DataRegisto" ></td>';
for
$tabela1 .= '<td> <input type="datetime" name= "DataRegisto[]" id= "DataRegisto" value="'. date("Y-m-d H:i:s") .'"></td>';