Update in database table

Asked

Viewed 148 times

0

I have this code to make updatand the countryside estado as completed and be the only field that can be edited:

<?php  

    $servername = "xxx.xxx.x.xx";
    $username = "xxxxx";
    $password = "xxxxxxx";
    $dbname = "xxxxxxxx";

    $conn = new mysqli($servername, $username, $password, $dbname);
    $conn->set_charset('utf8');

    $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>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="checkbox" name= "Id[]" value="'.$rows_cursos['Id'].'"></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>";


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



    ?>

    <?php  
    if(isset($_POST['registar']))
    {
    $servername = "xxx.xxx.x.xx";
    $username = "xxxx";
    $password = "xxxxxxx";
    $dbname = "xxxxxx";

    $conn = new mysqli($servername, $username, $password, $dbname);
    $conn->set_charset('utf8');

    $id= $_POST['Id'];
    $estado= $_POST['Estado'];

        $conn->query("UPDATE RegistoManutencao SET Estado='$estado' WHERE Id=".$Id); 


    }
    ?>

But when does the update table, fields are empty....

  • I need to see the HTML form to be sure the cause of the problem is really if the value is going to the method POST

  • But this is independent of the form where the insert. Here I show the query and in the table of the query I want to do the update of the status field with the value completed

  • I don’t understand what the problem is não me faz o update na tabela da coluna checkbox com o nome estado:

  • this verb is correct? registar ... Registo

  • and how to recover values via Post outside the form?

  • So it is not possible to enter the value of the Status Column in the table where I register with the form? I thought it would recover the values when using the Post

  • You have to put everything in the form (inputs, select, etcc, with name) to be able to submit and recover via POST

  • You can put just one example?

  • Table data can be edited?

Show 4 more comments

2 answers

0


Second comment, Eles não podem ser editados, só a coluna estado, and as the column Estado has invariable value value="Concluído"

I believe this way meets what you want:

update in the column estado of) id(s) marked(s).

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

$servername = "xxx.xxx.x.xx";
$username = "xxxxx";
$password = "xxxxxxx";
$dbname = "xxxxxxx";

$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset('utf8');

$registro = $_POST['Id'];

foreach($registro as $value) { 
    $conn->query("UPDATE RegistoDiario SET Estado='concluido' WHERE Id=" . $value); 
}
............
............

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['Dia'].'</td>';

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

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

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

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

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

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

    $tabela1 .= '<td> <input type="checkbox" name= "Id[]" value="'.$rows_cursos['Id'].'"></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='Registar'>";

    echo "</form>";
?>

$registro = $_POST['Id']; will return an array of checkboxes marked once the names of the checkboxes contains [] (brackets) after the names

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

Then just iterate the array with the update

foreach($registro as $value) { 
    $conn->query("UPDATE RegistoDiario SET Estado='concluido' WHERE Id=" . $value);
}

With input type="radio", Completed or Pending

Replace this line

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

along that line

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

and in PHP replace foreach

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

    $conn->query("UPDATE RegistoDiario SET Estado='".$estado."' Where Id='".$Id."'"); 

}
  • First you build a table with columns filled with database data, right? Hence you want what? that this data is sent to UPDATE? But they can be edited or not?

  • They cannot be edited, just the status column. I did as example and kept the whole structure as I have in question, but did not do the update on the table

  • after update puts echo $sql1; and see what returns

  • Return this, UPDATE RegistoDiario SET Id = '11', DataRegisto = '2018-02-09', Dia = 'Sexta-Feira', TipoRefeicao = 'Normal', Refeicao = 'Almoço', Hora = '12:00', Motivo = '', Sugestao = '', Colaborador = 'Fátima', Estado = '' WHERE Id = '11', but the id I’m editing is the 1. I want him to just put as completed in the status column the ones I put the visa. Is it possible? As it stands, is taking on the latter Id, but even putting the visa in the last it does not update that line

  • In the table returns all ids, from 1 to 11. Then I put the visa in the line of id=1 and in the echo of update it shows that made the WHERE Id = '11', but if put the visa on that line it does not do the update in the same to that line

  • Boy, I’ll create a table and play on my server, can you give me the table?

  • I put the table on top of the question

  • went bad in my bank Script line: 1 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE RegistoDiario ( Id int(11) NOT NULL AUTO_INCREMENT, Dat' at line 1&#xA;

  • That’s what it means?

  • It means I created a table manually, then I see what this error is

  • I figured out the problem, I’ll fix it here and then edit the answer

  • Okay, then you can tell me where the problem was so I can figure out where I was wrong?

  • wanted the same thing you helped me do with input type="checkbox", but now intended it to be with a input type="radio", where it could place or Completed or Pending

  • @Added junior in response with input type="radio"

  • worked, but now I have another problem, I edited the question

  • @Junior each case should be posted in a new question so that the answer is not without understanding

  • I edited this question so I wouldn’t be posting a new question on the same topic, but it won’t happen again. Should I ask the question again as I had? So to answer me I ask a new question?

  • @Junior, exactly, see that my answer has become meaningless for the edited question. Posting as a new question, other users may also answer

  • I already made the change and already created a new question.... and thank you for explaining the situation of changing the question

Show 14 more comments

-1

Try to close the TAG input:

$tabela1 .= '<td> <input type="checkbox" name= "Estado" value="Concluído"</td>';

Thus:

$tabela1 .= '<td> <input type="checkbox" name= "Estado" value="Concluído" /></td>';
  • Didn’t solve, doesn’t do the update on the table

  • Now that I’ve seen it, the checkbox line is off the FORM, I’m sure?

  • yes, the checkbox line only appears when I show the query in the table and when I insert the form there are only these fields: Id, DataRegisto,&#xA; Dia,&#xA; TipoRefeicao,&#xA; Refeicao,&#xA; Hora,&#xA; Motivo,&#xA; Sugestao,&#xA; Colaborador

Browser other questions tagged

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