insert only the row that receives value in the date column

Asked

Viewed 60 times

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>";

?>

I now 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 you only enter if you fill in the last row of the table I show in the image. I want it to insert the rows I fill into the image table and insert all the fields in the row. I’m not making a update, I really want to make a insert into

  • which is not working, which error?

  • @Ricardo Punctual, it does not give error and inserts, but only inserts the last line, I intend that it inserts all the lines I fill

1 answer

1


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

You need to change the name of the controls to say it’s a array, for example name="NomeUtente[]". Do it with all the other fields.

Then you make a loop, with for for example, counting how much data there is, using count. The code would be 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 execute the insert once, see this other question if you are interested: How to write multiple records in a table at the same time Mysql

Browser other questions tagged

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