Data is not being deleted from the database

Asked

Viewed 22 times

0

I made a form, where when the user clicks the delete button, the value entered in the imput 'id' is deleted from the database. Thanks in advance for your help.

Follow my form.

  <form name="Form_CRUD" method="post" action="consulta.php" >
    <div class="row">
    <div class="col-6">
        <div class="form-group">

            <input type="hidden" id="id_input" class="form-control" name="id">
            <label>Nome</label>
            <input type="text" id="nome_input" class="form-control" name="nome">

        </div>
        </div>
        </div>
        <div class="row">
        <div class="col-6">
        <div class="form-group">
            <label>Sobrenome</label>
            <input type="text" class="form-control" id="sobrenome_input" name="sobrenome">
        </div>
        <button type="submit" name="Add"  class="btn btn-primary">Adicionar</button>
        <button type="submit" name="Upd" class="btn btn-primary">Alterar</button>
        <button type="submit" name="Del" class="btn btn-primary">Excluir</button>
        </div>
        </div>
    </form>
    <?php

    $validarenvio = isset($_GET['s']) ? $_GET['s'] : '';

    if ($validarenvio == '1') :
    ?>

        <div class=" alert alert-success" role="alert">
            <strong>Sucesso!</strong>
            Cadastro realizado com sucesso
        </div>
        <?php  endif; ?>
    <?php
    if($validarenvio == '2'):
    ?>

        <div class=" alert alert-success" role="alert">
        <strong>Sucesso!</strong>
            Alterado com sucesso!
        </div>

    <?php  endif; ?>
    <?php
    if($validarenvio == '3'):
    ?>

        <div class=" alert alert-success" role="alert">
        <strong>Sucesso!</strong>
            Excluido com sucesso!
        </div>

    <?php  endif; ?>
    <?php
    if($validarenvio == '0'):
    ?>

        <div class=" alert alert-danger" role="alert">
            <strong>Erro!</strong>
            Erro de envio verifique!
        </div>

    <?php  endif; ?>

Follow my.php query:

                    <?php
                    require_once ('classes/conecta.php'); 
                    $idCli = trim($_POST['id']);
                    $nomeCli = trim($_POST['nome']);
                    $sobrenomeCli = trim($_POST['sobrenome']);
                    $insert = "INSERT INTO clientes_cli";
                    $insert .= " (nome_cli, sobrenome_cli)";
                    $insert .= "VALUES ('$nomeCli', '$sobrenomeCli')";
                    $Obj_Conexao = new CONEXAO();

                    $erro = 0;
                    $up = "UPDATE clientes_cli";
                    $up .= " SET nome_cli = '$nomeCli', sobrenome_cli = '$sobrenomeCli'";
                    $up .= "WHERE cod_cli = $idCli";

                    $del = "DELETE From clientes_cli";
                    $del .= "WHERE cod_cli = $idCli";



                    if (isset($_POST['Add'])){
                            header('Location: index.php?s=1');
                            $inserir_dados = $Obj_Conexao->Consulta($insert);
                            exit;
                    } else if (isset($_POST['Upd'])) {
                            header('Location: index.php?s=2'); 
                            $alterar_dados = $Obj_Conexao->Consulta($up);
                            exit;
                    } else if (isset($_POST['Del'])){
                            header('Location: index.php?s=3'); 
                            $Deletar_dados = $Obj_Conexao->Consulta($del);
                            exit;
                    } else {
                            header('Location: index.php?s=0'); 
                            exit;
                    }
                    ?>

1 answer

1


1- Which return you are getting from this operation?

2- DELETE operations should always be done with the PK field of the table for security reasons. This cod_cli field and the PK of your table? If it is not checked in the configs of your DB if it allows deletion without using PK.

3- The ideal would be to delete BEFORE the Location header!

  • Gabriel, yes my cod_cli is my PK yes.

  • I made the modification of your item three and it did not function!

  • The message to the user is responding comfort the programmed. But in the database no action occurs.

Browser other questions tagged

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