How to edit Mysql records with an out-of-order PHP table

Asked

Viewed 353 times

-1

I’m creating a document request system, and on it is a page that shows all pending requests. These requests are recorded in a Mysql table and shown as in the image below.

Os dados são exibidos por ordem do ID.

The problem is that in the "Actions" field it is only possible to execute something in the order of the table (Borrow or Delete the first one, and then manage to do this with the next one and so on).

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

Delete = Delete from order table

How do I manage to execute the action on any of the list, without having to respect the order of the records shown?

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

?>
  • I don’t understand the question. Rephrase, please.

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 possible to submit the form.

Reason why when deleting the first line you can perform the next one and so on

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

Do so 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>";
 }

Important: The variable $ _SERVER ["PHP_SELF"] can be used by hackers!

How to avoid?

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

More information

0

Thank you. There was no attempt on my part to put the </form> inside PHP, I put it out, closing the form as a whole, and it worked.

[EDIT]

I didn’t quite understand what you said about building my form. I did the procedure I mentioned above and I thought the problem was solved, but in fact, before I did that, if I clicked on any input, other than the first one, I received no feedback. By placing the </form> out of PHP, inputs returned actions, but only in the last table record.

I re-filed my form like you mentioned and now it’s all right.

Thank you very much!!

Browser other questions tagged

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