CRUD in PHP and MYSQL - Delete

0

I'm having problems with a crud in php and myqsl . My code does not seem to be getting the id to perform the actions. The site has an administrative part where I view the registered users and can edit, delete and detail on each user.

Here is the code for the list , it is associated with an html (users.html). In users.html I get php via javascript - I explain why I do this: I want to use php only to perform actions not to display the actions, I want it to be behind the actions. In the list I have 3 buttons that will perform such actions. But he does not get the id to do it.

<?php

// exibir error
ini_set('display_errors',1);
ini_set('display_startup_erros',1);
error_reporting(E_ALL);


include 'conecta.php';

//  monta a query
$sql = "SELECT id_user, usuario, nome, sobrenome FROM  usuario";
// executa a query
$result = $conn->query($sql);


if ($result) {

    while ($aux_query = $result->fetch_assoc()) 
    { ?>


    <!-- Modal de Delete--> 
        <div class="modal fade" id="delete-modal" tabindex="-1" role="dialog" aria-labelledby="modalLabel">
            <div class="modal-dialog" role="document">
               <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Fechar">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>          
                    <div class="modal-body"> 
                        Deseja realmente excluir este usuário?          
                    </div>         
                    <div class="modal-footer">                
                        <a id="confirm" class="btn btn-primary"  href="excluir_user.php?edit=<?php echo $row['id_user']; ?>">Sim</a>             
                        <a id="cancel" class="btn btn-default" data-dismiss="modal">N&atilde;o</a> 
                    </div>        
                </div>        
            </div>       
        </div> <!-- /.modal -->

        <tr>
          <th>
            <div class="media align-items-center">
              <a href="#" class="avatar rounded-circle mr-3">
                <img alt="Image placeholder" src="../assets/img/theme/perfil1.jpg">
              </a>
              <div class="media-body">
                <input type="hidden"><?php echo'' .$aux_query["id_user"]; ?></input>
                <span class="mb-0 text-sm"><?php echo'' .$aux_query["usuario"]; ?></span>
              </div>
            </div>
          </th>
          <th>
            <div class="media align-items-center">
              <div class="media-body">
                <span class="mb-0 text-sm"><?php echo '' .$aux_query["nome"]; ?></span>
              </div>
            </div>
          </th>
          <th>
            <div class="media align-items-center">
              <div class="media-body">
                <span class="mb-0 text-sm"><?php echo '' .$aux_query["sobrenome"]; ?></span>
              </div>
            </div>
          </th>
          <td class="text-right">
            <div class="dropdown">
              <a class="btn btn-sm btn-icon-only text-light" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <i class="fas fa-ellipsis-v"></i>
              </a>
              <div class="dropdown-menu dropdown-menu-right dropdown-menu-arrow">
                <a href="detalhar_user.html?id= <?php echo ''.$aux_query["id_user"];?>" class="dropdown-item" >Detalhes</a>

                <a  href="atualizar_user.html?id=<?php echo ''.$aux_query["id_user"];?>" >Editar</a>
                <button class="dropdown-item" data-toggle="modal" data-target="#delete-modal">Excluir</button>
              </div>
            </div>
          </td>
        </tr> <?php
    }

    $result->free();

} else {
    echo "Erro: " . $sql . "<br>" . $conn->error;
}

// fecha ponto de conexão
$conn->close();

?>

UPDATE 1:

Here is the exclui_user.php code and gives error in line 11 ($ id = $ _POST ["id_user"];)

<?php 
// exibir error ini_set('display_errors',1); 
ini_set('display_startup_erros',1); 
error_reporting(E_ALL); 

include 'conecta.php'; 

$id = $_POST["id_user"]; 

// monta a query 
$sql = "DELETE FROM usuario WHERE id_user = '$id'";

// executa a query 
$result = $conn->query($sql); 

if ($result) { 
    echo "Usuario excluído!"; 
} else { 
    echo "Erro: " . $sql . "<br>" . $conn->error;
} 

// fecha ponto de conexão 
$conn->close(); 

?>
    
asked by anonymous 15.10.2018 / 22:12

1 answer

0

Hello,

The line where you have the delete button is currently like this:

 <a id="confirm" class="btn btn-primary"  href="excluir_user.php?edit=<?php echo $row['id_user']; ?>">Sim</a>

It would not be:

 <a id="confirm" class="btn btn-primary"  href="excluir_user.php?id=<?php echo $row['id_user']; ?>">Sim</a>

Note the href="#"># and href="#"># informing the variable by URL (or by Query String), on the PHP side, on the server, that same variable can only be accessed via $ _GET, $ _POST is for when you send HTML Form data. Example:

<form method="post" action="seu_script.php">
    <input type="text" name="id" value="123" />
    <input type="submit" value="Enviar"/>
</form>

I hope I have helped.

    
15.10.2018 / 22:54