Update with foreach in the database

Asked

Viewed 586 times

0

I show this data with the select and after the user query can edit three fields: Image, Treatment and Status. The State camp is doing the update correct in table, but the fields input type="file" and input type="text" is only correct if you edit one line at a time. This 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 later in the update I create the variables this way and 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 that when editing more than one line, it does the update is correct in the lines I am editing and only makes it correct in the status field. In the field image and treatment is only correct if you do the update one line at a time.

  • Could you better contextualize the question? Describing the scenario to be applied to the solution? If you can describe it I believe it will be easier for the community to help you

  • The problem is that when I’m doing the update The column image and treatment will always fetch the last line both in white and with data, because it does not know the id of the line I’m making the update

  • @Junior You can create buttons in each row of the table that make the update run unique (for each row you click) (this would be for updating image and processing). Or you can try sending the entire table by renaming the fields to an array. Make sure the way you are doing you are sending all the lihas of the table. Give a print_r($_POST) and check if there is an element array in the post. Later I can make an example for you

2 answers

0

Solution example

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

Remembering This code is just an example, a baseline for you to get what you want. In the code ,note that I enter a form for each line and within each form I declare a field of type Hidden that stores the id of the record of that line.With this code , each line will make a request for your php file and you will not need to use the foreach, causing the update to be carried out 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 in 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 utilizing php, I index this link here

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

Follow suit :here

Or look around 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>

0


In the code change the names of inputs Imagemand Tratamento

In the form tag you need to define the enctype as "multipart/form-data" which indicates that the form we are using will work with uploading 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."'");

    }

}
?>

Thus, being the column of type BLOB, we save the images directly in the database. Some of the most cited problems regarding the storage of images in a database is related to creating a lot of 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 for reading data from files that will be sent to the server

$_FILES is an array, so 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 less space.

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

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

The column in the seat shall be 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."'");

    }
  • this way I send even the image to the database or upload the path to the database?

  • 1

    @Junior, as in your question input type="file", Thus, if the column is of type BLOB, we save the images directly in the database. Some of the most cited problems regarding the storage of images in a database is related to creating a lot of overhead in a database search. Sugiro salvá-las externamente em diretórios e apenas manter o endereço (caminho) da imagem na base de dados.

  • I’m already saving the image name and extension, but how do I save the images in an external directory? because I do that but then I can’t see the images. I’m saving it this way: link

  • Can you edit the answer for me to see? I will upload photos taken from a tablet to the database

  • I experimented with the full path and it does not happen anyway, the image never appears in the query.... you can see the other question I asked, that I even put the link in the comment above, to see if I am doing the correct update of the name and extension of the image and then how I can display it?

  • You can’t do that here, because the country is input type="file" and the code is to save image directly in the database.

  • @Junior, First of all you have to decide what you want to store in the database. The image? The image URL? Or both (rs, this is good, both of them, because)

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

Show 3 more comments

Browser other questions tagged

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