CRUD in PHP and MYSQL - Delete

Asked

Viewed 789 times

0

I’m having trouble at a crud in php and myqsl. My code doesn’t seem to be getting the id to perform the actions. The site has an administrative part in which I view registered users and can edit, delete and detail about each user.

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

<?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 code of exclui_user.php and gives error on 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(); 

?>
  • 1

    Here is the code of exclui_user.php and gives error on line 11 ($id = $_POST["id_user"];) <? php&#xA;&#xA;// exibir error&#xA;ini_set('display_errors',1);&#xA;ini_set('display_startup_erros',1);&#xA;error_reporting(E_ALL);&#xA;&#xA;&#xA;include 'conecta.php';&#xA;&#xA;$id = $_POST["id_user"];&#xA;&#xA;// monta a query&#xA;$sql = "DELETE FROM usuario WHERE id_user = '$id'"; // executes the query $result = $Conn->query($sql); if ($result) { echo "Deleted user!" ; } Else { echo "Error: ". $sql . " <br>" . $Conn->error; } // closes connection point $Conn->close(); ;?>

  • If possible click on [Edit] and put the new code. In the comments it gets disorganized. Ps.: In the question you can press Ctrl + K to format it.

1 answer

1

Hello,

The line where you have the delete button is currently so:

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

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="excluir_user.php?**edit**= and the href="excluir_user.php?**id**=

REPLY UPDATE 1:

On line 11 of the file change from:

$id = $_POST["id_user"];

for

$id = $_GET["id"];

Explanation

When you create the link this way: href="excluir_user.php?id=123 informing the variable by the URL (or by Query String), on the PHP side, on the server, this same variable can only be accessed via $_GET, the $_POST is for when you send data from the HTML Form. Example:

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

I hope I’ve helped.

  • There really was this mistake. But it was because I picked it up from a site like this to try another way. Only before it was the way you suggested but it didn’t work

  • @amandacostam9 already edited your question and edited the answer, any questions just ask... hug.

Browser other questions tagged

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