Error in record deletion

3

I intend to do a record deletion from a table with php and javascript, but at the time of doing the deletion sometimes it does it and other times not

This is the code of my button

 <button title="Eliminar Registro" id="delete-contrato-modal" name="delete-contrato-modal" type="button" class="btn btn-link btn-danger btn-just-icon remove" onclick="deleteCbContrato('<?php echo($row_herramientas['id_tipo_contrato']); ?>');"><i class="material-icons" data-target="#myModalDelete01" data-toggle="modal" >close</i></button> 

This is the function

function deleteCbContrato(id_tipo_contrato){     
        $('#id_tipo_contratoDelete').val(id_tipo_contrato);           

        $('#myModalDelete01').on('shown.bs.modal', function () {
            $('#myInput01').focus()
        }); 
}

This is my modal

<!-- Modal DELETE -->
<div class="modal fade" id="myModalDelete01" tabindex="-1" role="dialog" aria-labelledby="myModalDeleteLabel">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">

                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>


                        <h4 class="modal-title" id="myModalDeleteLabel">Eliminación de Registro</h4>
                    </div>
                    <form role="form" name="formDeleteContrato" method="post">
                        <div class="modal-body">                                    
                                <div class="input-group">
                                <label for="id_tipo_contrato">¿Desea eliminar el registro seleccionado?</label>
                                </div>       
                                <div class="input-group">
                                 <label for="id_tipo_contrato">Registro: </label>
                                 <input type="text" readonly class="form-control" id="id_tipo_contratoDelete" name="id_tipo_contratoDelete" >                                        
                                </div>
                        </div>
                        <div class="modal-footer">
                    <button id="delete-contrato-select" name="delete-contrato-select" type="submit" class="btn btn-primary">Aceptar</button>                                        
                     <button id="cancel"type="button" class="btn btn-default" data-dismiss="modal">Cerrar</button>   



                        </div>
                    </form>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal --> 

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

<script type="text/javascript">
$('#delete-contrato-select').on("click", function(){

var r01=$('#id_tipo_contratoDelete').val();

                "r_delete01" : r01
var parametros = {
        };
        $.ajax({
                data:  parametros,
                url:   'includes/funciones_catalogos.php',
                type:  'post',
                beforeSend: function () {

                },
                success:  function (response) {
location.reload();
                }
        });
 });

</script>

And here is the action of the Delete

<?php
require_once('../conex/conex.php'); 

if(isset($_POST['r_delete01']))
{

$sql01=mysqli_query($conex,"DELETE FROM tipo_contrato WHERE id_tipo_contrato=".$_POST['r_delete01']);
 $conex->query($sql01);
}else{}
    
asked by Tallulah 31.10.2018 в 16:05
source

2 answers

3

There are problems in both the PHP code and the Javascript code

PHP:

Your code runs twice as this:

$sql01=mysqli_query($conex,"DELETE FROM tipo_contrato WHERE id_tipo_contrato=".$_POST['r_delete01']);

And this:

$conex->query($sql01);

It's the same.

We are going to correct it, and we will use the object-oriented style, it is much clearer and understandable. In addition:

  • We will write a secure code using prepared queries
  • We will control the POST variable
  • And we're going to set up an error control

I propose this:

<?php
$dato=(empty($_POST['r_delete01'])) ? NULL: $_POST['r_delete01'];
if ($dato)
{
    require_once('../conex/conex.php');
    $sqlDelete="DELETE FROM tipo_contrato WHERE id_tipo_contrato=?";
    $stmt=$conex->prepare($sqlDelete);
    $stmt->bind_param("i",$dato);
    if($stmt->execute())
    {
        $totalDeleted=$conex->affected_rows;
        $msg="Se borraron $totalDeleted filas";
    }else{
        $msg="Hubo un error: ".$conex->error;
    }
}else{
    $msg="No hay datos en el POST";
}
echo $msg;

Javascript

You are sending the variable parametros empty to the server, when building it like this:

                "r_delete01" : r01  //Esto está fuera
var parametros = {                  //Aquí dentro no hay nada 
        };

For the values to be inside you should put them like this:

var parametros = { 
                   "r_delete01" : r01 
                 };

Just to show what happened, you can put this in the Javascript code:

success:  function (response) {
    /*Agregas esto*/
    alert(response);
    location.reload();

You can also put: console.log(response); , although to see it you will have to check the console. Here as it is a string you will see it in the alert , if it were an object, like a JSON for example, you can not see it with alert , you will have to use the console.

    
answered by 31.10.2018 / 16:26
source
0

Additionally to the comment of A. Cedano , you are not running the PHP code when you click on the button. The function that the click event receives does nothing more than assign a value to the element #id_tipo_contratoDelete , and the shape of the modal screen has not defined the attribute action . To execute the deletion of the record, you must send the form or make an asynchronous call (see

answered by 31.10.2018 в 16:57