How to edit MySQL records with a PHP table out of order

0

I'm creating a document request system, and it has a page where it shows all the pending requests. These requests are recorded in a MySQL table and shown as in the image below.

Theproblemisthatinthe"Actions" field you can only execute something in the order of the table (Lend or Delete the first one, then you can do this with the next one and so on).

  

Lend = Insert into another table (MySQL) and delete the table (MySQL) of requests

     

Delete = Delete from order table

How can I perform the action on any of the list, without having to respect the order of the displayed records?

Commands I'm using:

$resultado  = mysqli_query($conexao, "SELECT * FROM pedidos ORDER BY  'id'");
$linhas     = mysqli_num_rows($resultado);
$linhas1    = mysqli_num_rows($resultado);

Form / Table

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
    <?php
    while($linhas = mysqli_fetch_array($resultado)){

        $data = $linhas['dataHora'];
        $data = strtotime($data);
        echo "<tr>";
        /* echo "<td><input type='checkbox' class='checkthis' /></td>";*/
        echo "<td></td>";
        echo "<td>".$linhas['pasta']."</td>";
        echo "<td>".$linhas['codigoPaciente']."</td>";
        echo "<td>".$linhas['nomePaciente']."</td>";
        echo "<td>".$linhas['motivo']."</td>";
        echo "<td>".$linhas['solicitante']."</td>";
        echo "<td>".$linhas['setor']."</td>";
        echo "<input type='hidden' name='id' value='".$linhas['id_pedidos']."'>";
        echo "<td>".date('d/m/Y - H:i', $data)."</td>";
        echo "<td>  <input type='submit' tittle='Emprestar'  value='E' name='SendEmprestar' class='btn btn-sm btn-primary'> <input type='submit' value='A' name='SendDelete' class='btn btn-sm btn-warning'>";
        echo "</tr>";
        echo "</form>";
        }
    ?>

Process the request

        <?php
        $SendEmprestar = filter_input(INPUT_POST, 'SendEmprestar', FILTER_SANITIZE_STRING);
        if($SendEmprestar){
$id         =  isset($_POST['id']) ? $_POST['id'] : ''; 
$resultado  = mysqli_query($conexao, "SELECT * FROM pedidos WHERE id_pedidos = $id");
$linhas     = mysqli_num_rows($resultado);
$linhas     = mysqli_fetch_array($resultado);

$id_pedidos     = $linhas['id_pedidos'];
$pasta          = $linhas['pasta'];
$nomePaciente   = $linhas['nomePaciente'];
$solicitante    = $linhas['solicitante'];
$motivo         = $linhas['motivo'];
$codigoPaciente = $linhas['codigoPaciente'];
$setor          = $linhas['setor'];
$colaborador    = isset($_SESSION['nome']) ? $_SESSION['nome'] : '';

$emprestar = "
INSERT INTO emprestados(
        pasta, 
        nomePaciente, 
        solicitante, 
        motivo, 
        codigoPaciente,
        setor,
        colaborador) 
VALUES ('$pasta', 
        '$nomePaciente', 
        '$solicitante', 
        '$motivo', 
        '$codigoPaciente',
        '$setor',
        '$colaborador')";
$pendente = "
INSERT INTO pendentes(
        pasta, 
        nomePaciente, 
        solicitante, 
        motivo, 
        codigoPaciente,
        setor,
        colaborador) 
VALUES ('$pasta', 
        '$nomePaciente', 
        '$solicitante', 
        '$motivo', 
        '$codigoPaciente',
        '$setor',
        '$colaborador')";

$salvar     = mysqli_query($conexao, $emprestar);   
$salvar2    = mysqli_query($conexao, $pendente);
if ($salvar and $salvar2 =! 0){
            $query      =   mysqli_query($conexao, "DELETE FROM pedidos WHERE id_pedidos = $id");
                    if($query != 0){
                    echo "<div class='alert alert-success' role='alert'>";
                    echo "Emprestado com sucesso!";
                    echo "</div>";
                    echo "<script>deletePedido()</script>";
}else{
                    echo "Solicitação invalída.";
                    echo "<script>deletePedido()</script>"; 
        }
}else{
                    echo "Erro na solicitação!";
                    echo "<script>deletePedido()</script>"; 
}               }


        $SendDelete = filter_input(INPUT_POST, 'SendDelete', FILTER_SANITIZE_STRING);
        if($SendDelete){
                $id         =  isset($_POST['id']) ? $_POST['id'] : ''; 
                $query      =   mysqli_query($conexao, "DELETE FROM pedidos WHERE id_pedidos = $id");
                if($query != 0){
                        echo "<div class='alert alert-warning' role='alert'>";
                        echo "Pedido excluído com sucesso!";
                        echo "<script>deletePedido()</script>";
                        echo "</div>";
                }else{
                        echo "<div class='alert alert-danger' role='alert'>";
                        echo "Solicitação invalída.";
                        echo "<script>deletePedido()</script>";
                        echo "</div>";
                }
        }

?>
    
asked by anonymous 28.07.2018 / 20:52

2 answers

0

The way your form is being built,

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
    ..............
    ..............
    <tr>
    <td><input type='hidden' name='id' value='1'> 
    <input type='submit' tittle='Emprestar'  value='E' name='SendEmprestar' class='btn btn-sm btn-primary'>
    <input type='submit' value='A' name='SendDelete' class='btn btn-sm btn-warning'>
    </tr>
</form>

    ............
    ............
    <tr>
    <td><input type='hidden' name='id' value='2'>
    <input type='submit' tittle='Emprestar'  value='E' name='SendEmprestar' class='btn btn-sm btn-primary'>
    <input type='submit' value='A' name='SendDelete' class='btn btn-sm btn-warning'>
    </tr>
</form>

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

Only the first row of the table will be able to submit the form.

  

Reason why when you delete the first line you can do the next and so on

Note that on the second line there is only the closing tag </form>

Do this below (since there are several inputs with name='id' )

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
    ..............
    ..............
    <tr>
    <td><input type='hidden' name='id' value='1'> 
    <input type='submit' tittle='Emprestar'  value='E' name='SendEmprestar' class='btn btn-sm btn-primary'>
    <input type='submit' value='A' name='SendDelete' class='btn btn-sm btn-warning'>
    </tr>
</form>

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
    ............
    ............
    <tr>
    <td><input type='hidden' name='id' value='2'>
    <input type='submit' tittle='Emprestar'  value='E' name='SendEmprestar' class='btn btn-sm btn-primary'>
    <input type='submit' value='A' name='SendDelete' class='btn btn-sm btn-warning'>
    </tr>
  </form>


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

** Your PHP **

while($linhas = mysqli_fetch_array($resultado)){
  echo "<form method=\"POST\" action=\"".$_SERVER['PHP_SELF']."\">";
    $data = $linhas['dataHora'];
    $data = strtotime($data);
    echo "<tr>";
    /* echo "<td><input type='checkbox' class='checkthis' /></td>";*/
    echo "<td></td>";
    echo "<td>".$linhas['pasta']."</td>";
    echo "<td>".$linhas['codigoPaciente']."</td>";
    echo "<td>".$linhas['nomePaciente']."</td>";
    echo "<td>".$linhas['motivo']."</td>";
    echo "<td>".$linhas['solicitante']."</td>";
    echo "<td>".$linhas['setor']."</td>";
    echo "<input type='hidden' name='id' value='".$linhas['id_pedidos']."'>";
    echo "<td>".date('d/m/Y - H:i', $data)."</td>";
    echo "<td>  <input type='submit' tittle='Emprestar'  value='E' name='SendEmprestar' class='btn btn-sm btn-primary'> <input type='submit' value='A' name='SendDelete' class='btn btn-sm btn-warning'>";
    echo "</tr>";
   echo "</form>";
 }
    
28.07.2018 / 22:14
0

Thank you. I had not bothered to put </form> inside PHP, put it outside, closing the form as a whole, and it worked.

[EDIT]

I did not quite understand what you mentioned about building my form. I did the above procedure and found that the problem was solved, but in fact, before doing this if I clicked on some input, other than the first one, I did not get any feedback. By putting </form> out of PHP, the inputs returned actions, but only in the last record of the table.

I have refined my form as you mentioned and now everything is fine.

Thank you so much !!

    
28.07.2018 / 23:09