Update with foreach in the database

1

I show this data with select and after the user queries you can edit three fields: Image, Treatment and Status . The State field is doing the update correct in the table, but the input type="file" and input type="text" fields are only correct if you edit one row at a time. Here is the code:

  $result_cursos = "SELECT centrodb.RegistoManutencao.Id,
       DataRegisto,
       Pedido,
       Outro,
       Descricao,
       Funcionario,
       Imagem,
       Tratamento,
       Estado

FROM centrodb.RegistoManutencao LEFT OUTER JOIN centrodb.InfoLuvas

ON centrodb.InfoLuvas.Id = centrodb.RegistoManutencao.Colaborador

WHERE Estado IS NULL OR Estado <> 'Concluído';";
    $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>Nº Registo</th>';

$tabela1 .= '<th>Data</th>';

$tabela1 .= '<th>Pedido</th>';

$tabela1 .= '<th>Outro Local</th>';

$tabela1 .= '<th>Descrição</th>';

$tabela1 .= '<th>Colaborador</th>';

$tabela1 .= '<th>Imagem</th>';

$tabela1 .= '<th>Tratamento</th>';

$tabela1 .= '<th>Estado</th>';

$tabela1 .= '</tr>';

$tabela1 .='</thead>'; 

$tabela1 .='<tbody>';

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

$tabela1 .= '<tr>';

$tabela1 .= '<td>'.$rows_cursos['Id'].'</td>';

$tabela1 .= '<td>'.$rows_cursos['DataRegisto'].'</td>';

$tabela1 .= '<td>'.$rows_cursos['Pedido'].'</td>';

$tabela1 .= '<td>'.$rows_cursos['Outro'].'</td>';

$tabela1 .= '<td>'.$rows_cursos['Descricao'].'</td>';

$tabela1 .= '<td>'.$rows_cursos['Funcionario'].'</td>';

$tabela1 .= '<td> <input type="file" name= "Imagem" id= "Imagem" value="'.$rows_cursos['Imagem'].'"></td>';

$tabela1 .= '<td> <input type="text" name= "Tratamento" id= "Tratamento" value="'.$rows_cursos['Tratamento'].'"></td>';

$tabela1 .= '<td> <input type="radio" name= "Id['.$rows_cursos['Id'].']" value="Pendente"> Pendente  <input type="radio" name= "Id['.$rows_cursos['Id'].']" value="Concluido">Concluido</td>';

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

?>

Where then in the update I create the variables in this way and I do update :

<?php  
if(isset($_POST['registar']))
{
$registro = $_POST['Id'];
$tratamento = $_POST['Tratamento'];
$imagem = $_POST['Imagem'];

foreach($registro as $Id => $estado) { 

    $conn->query("UPDATE RegistoManutencao SET Estado='".$estado."', Imagem = '$imagem', Tratamento = '$tratamento' WHERE Id='".$Id."'"); 
} 


}
?>

I want when editing more than one line, it does the update correct in the rows I'm editing and only makes correct in the state field. In the image and treatment field it is only correct if you do update to one line at a time.

    
asked by anonymous 19.02.2018 / 16:18

2 answers

0
  

In the code change the names of inputs Imagem and Tratamento

     

In the form tag you need to set enctype to "multipart/form-data" which indicates that the form we're using   will work with sending files.

..................
..................

$tabela1 .= '<td> <input type="file" name= "Imagem['.$rows_cursos['Id'].']" id= "Imagem"></td>';

$tabela1 .= '<td> <input type="text" name= "Tratamento['.$rows_cursos['Id'].']" id= "Tratamento" value="'.$rows_cursos['Tratamento'].'"></td>';

$tabela1 .= '<td> <input type="radio" name= "Id['.$rows_cursos['Id'].']" value="Pendente"> Pendente  <input type="radio" name= "Id['.$rows_cursos['Id'].']" value="Concluido"> Concluido</td>';

..................
..................

echo "<form method='POST' action='' enctype='multipart/form-data'>";

No update

<?php  
if(isset($_POST['registar']))
{

$registro = $_POST['Id'];

$tratamento = $_POST['Tratamento'];

    foreach ($registro as $Id => $estado) {

      $imagem = $_FILES['Imagem']['tmp_name'][$Id];
      $tamanho = $_FILES['Imagem']['size'][$Id];
      $fp = fopen($imagem, "rb");
      $conteudo = fread($fp, $tamanho);
      $conteudo = addslashes($conteudo);
      fclose($fp);

        $conn->query("UPDATE RegistoManutencao SET Estado='$registro[$Id]', Imagem = '$conteudo', Tratamento = '$tratamento[$Id]' WHERE Id='".$Id."'");

    }

}
?>

In this way, as the BLOB type column, we save the images directly in the database. Some of the most cited problems with storing images in a database is about creating too much overhead in a database search. I suggest saving them externally in directories and just keep the address (path) of the image in the database.

  

variable $_FILES used to read data from the files that will be sent to the server

$ _ FILES is an array, this way, when the form is submitted, it will have its indexes that are according to the php site

    $_FILES[name];
    $_FILES[type];
    $_FILES[tmp_name];
    $_FILES[error];
    $_FILES[size];

According to the comment

  

In the database I want to save the image url because it takes up less space.

The input should not be of type file, but of type text to enter the URL

$tabela1 .= '<td> <input type="text" name= "Imagem['.$rows_cursos['Id'].']" id= "Imagem"></td>';

The column in the database must be type varchar

And PHP is

$registro = $_POST['Id'];

$tratamento = $_POST['Tratamento'];

$imagem = $_POST['Imagem'];

    foreach ($registro as $Id => $estado) {

        $conn->query("UPDATE RegistoDiario SET Estado='$registro[$Id]', Imagem = '$imagem[$Id]', Tratamento = '$tratamento[$Id]' WHERE Id='".$Id."'");

    }
    
20.02.2018 / 01:31
0
  

Solution example

By what I understand you want to update row by row of a table that is mounted dynamically through php, Here is the link of an example of how to structure your html for your function in php to work.

Remembering This code is just an example, a baseline for you to get what you want. In the code, note that I insert a form for each line and within each form I declare a field of type hidden that stores the record id of that line. With this code , each line will make a request for the line. your php file and you will not need to use foreach , causing the update to be performed on demand: Ex:

if(isset($_POST['registar']))
{
$registro = $_POST['id'];
$tratamento = $_POST['tratamento'];
$imagem = $_POST['imagem'];
$estado = $_POST['estado'];

    $conn->query("UPDATE RegistoManutencao SET Estado='".$estado."', Imagem = '$imagem', Tratamento = '$tratamento' WHERE Id='".$registro."'"); 
  

Another Important Factor!

I saw that you are inserting images into your solution, I do not know how you structured your database to save the images, I do not know if you are saving this image in a blop field or if you are saving only the path of a folder where you move the file. If you have questions about saving images in mysql using php, I suggest this link here

. Check the code and try to implement in your solution, any other questions post here.

Follow the example: here

  

Or check here:

<table border="1">
  <thead>
    <tr>
      <th>Registro</th>
      <th>Imagem</th>
      <th>Estado</th>
      <th>Tratamento</th>
      <th>Ação</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Registro1</td>
      <form method="post" action="meufile.php" enctype="multipart/form-data">
      <input type="hidden" name="id" value="1" />
      <td><input type="file" name="imagem" /></td>
      <td><input type="radio" name="estado" />s | <input type="radio" name="estado" />N</td>
      <td><input type="text" name="tratamento"/></td>
      <td><input type="submit" name="registrar"value="atualizar" /></td>
      </form>
    </tr>
     <tr>
      <td>Registro2</td>
      <form method="post" action="meufile.php" enctype="multipart/form-data">
      <input type="hidden" name="id" value="2" />
      <td><input type="file" name="imagem" /></td>
      <td><input type="radio" name="estado" />s | <input type="radio" name="estado" />N</td>
      <td><input type="text" name="tratamento"/></td>
      <td><input type="submit" name="registrar" value="atualizar" /></td>
      </form>
    </tr>
  </tbody>
</table>
    
19.02.2018 / 21:15