How to display message after updating a record using PHP?

Asked

Viewed 190 times

0

Well, inside my main page(index.php) I check if there was any request to the server.

// Atualizar
if (isset($_GET['update'])) {
    $id = $_GET['update'];
    $cliente = array(
        $nome = $_POST['nome_cliente'],
        $endereco = $_POST['endereco_cliente'],
        $feeds = $_POST['feeds_cliente'],
        $entregues = $_POST['entregues_cliente'],
        $observacao = $_POST['observacao_cliente'],
        $telefone = $_POST['telefone_cliente'],
        $valor = $_POST['valor_cliente'],
        $data_de_pg = $_POST['dataPg'],
        $designer = $_POST['designer_cliente']
   );
   $Cliente->update($id, $cliente);
}

I selected in DB to display all registered clients and save these values in the modal date attribute.

<a href="#atualizar" class="btn btn-primary text- 
light ml-1 mt-1" data-toggle="modal" title="Editar" 
data-id="<?php echo $row['id'];?>" data-nome="<?php 
echo $row['nome']; ?>" data-endereco="<?php echo 
$row['endereco']; ?>" data-feeds="<?php echo 
$row['feeds']; ?>" data-entregues="<?php echo 
$row['entregues']; ?>" data-observacao="<?php echo 
$row['observacao']; ?>" data-telefone="<?php echo 
$row['telefone']; ?>" data-valor="<?php echo 
$row['valor']; ?>" data-designer="<?php echo 
$row['designer']; ?>" data-data_de_pg="<?php echo 
$row['data_de_pg']; ?>">
    <i class="fas fa-edit fa-1x"></i>
</a>

If the user performs the update action, I capture all these values saved in the data attribute and display the modal and load the url with the client id (index.php in the form action attribute? update=client.id).

// Escuta o evento do modal com id #atualizar
$('#atualizar').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget);
    var modal = $(this);

    var cliente = {
        id: button.data('id'),
        nome: button.data('nome'),
        endereco: button.data('endereco'), 
        feeds: button.data('feeds'),
        entregues: button.data('entregues'),
        observacao: button.data('observacao'),
        telefone: button.data('telefone'),
        valor: button.data('valor'),
        designer: button.data('designer'),
        data_de_pg: formatDate(button.data('data_de_pg'))};

    modal.find('#nome').val(cliente.nome);  
    modal.find('#endereco').val(cliente.endereco);
    modal.find('#feeds').val(cliente.feeds);
    modal.find('#entregues').val(cliente.entregues); 
   modal.find('#observacao').val(cliente.observacao); 
    modal.find('#telefone').val(cliente.telefone);
    modal.find('#valor').val(cliente.valor); 
    modal.find('#designer').val(cliente.designer); 
   modal.find('#data_de_pg').val(cliente.data_de_pg); 
    modal.find('#action').attr('action', 'index.php?update=' + cliente.id);});

In the request check to the server, after having captured all the data, I execute the method of updating the data.

<?php 
require_once 'connection.php';

class Cliente extends Connection{
      private $connection;

      function __construct(){
           $this->connection = parent::connection();
      }

      function update($id, $cliente = array()){
           try {
               $pgd = $cliente[6] * 0.33;

               $query = "UPDATE {$this->table_clients} SET nome = '$cliente[0]',  endereco = '$cliente[1]', feeds = $cliente[2], entregues = $cliente[3], observacao = '$cliente[4]', telefone = '$cliente[5]', valor = $cliente[6], data_de_pg = '$cliente[7]', designer = '$cliente[8]', pgd = $pgd WHERE id = $id";

               $atualizar = mysqli_query($this->connection, $query);

               if ($atualizar) {?>
                   <div class="alert alert-success alert-dismissible fade show  col-md-4 col-sm-4 col-lg-4" style ="margin-left: auto; margin-right: 10px;" role="alert" id="Alerta">
                   Dados atualizados com sucesso
                   <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                         <span aria-hidden="true">&times;</span>
                   </button></div>
                <?php
                }else{?>
                   <div class="alert alert-danger alert-dismissible fade show  col-md-4 col-sm-4 col-lg-4" style ="margin-left: auto; margin-right: 10px;" role="alert" id="Alerta">
                   Falha na atualização dos dados!
                   <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                         <span aria-hidden="true">&times;</span>
                   </button></div>
                <?php
                }

        }catch (Exception $error){
              echo $error->getMessage();
              return null;
        }

        parent::disconnect($this->connection);
   }

When I do the action of updating the SQL query works well, but it does not return the message I want. I did this same procedure with the registration and it returns the desired message, only that the request I used in the registration was POST. I do not know if the problem of the message appearing has to do with the type of request. I hope that you can help me and remove this doubt.

Thanks this already! :)

  • of a var_dump in $update

  • Well, I did what you sent and the return was "bool(true)", but to see this return I had to redirect the data to another page where I did the same check in my index.php.

1 answer

0

There is a typo, missing a hyphen. In $atualizar = mysqli_query($this>connection, $query); change to $atualizar = mysqli_query($this->connection, $query); and see if you solved the problem.

  • All right, I’ll fix it.

  • But it still didn’t solve.

  • I purposely made a mistake in the update method of the Client class. And that was the outworking the message appears, I don’t know why, if you know and can explain to me why...

Browser other questions tagged

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