Delete row from a table with php

-1

Hello good afternoon again in the forum and I have the following question.

I have this code that loads everything in the database into a table, the problem is when deleting by ID ...

Thanks in advance.

<form name="frmInsertarFecha" action="" method="POST">
        <table style="width:100%" class="table table-bordered table-responsive">
            <tr>
                <th>Id</th>
                <th>Fecha Inicio</th>
                <th>Fecha Termino</th>
                <th>Estado</th> 
                <th>Accion</th>
                <th>Accion</th>
            </tr>
            <?php

            $consulta = "SELECT * FROM R_FECHA";
            $ejecutar = sqlsrv_query($con, $consulta);

            $i = 0;

            while ($fila = sqlsrv_fetch_array($ejecutar)) {
                $id = $fila['R_FECHA_ID'];
                $fechaIni = $fila['R_FECHA_INICIO'];
                $fechaTer = $fila['R_FECHA_TERMINO'];
                $estado = $fila['R_FECHA_ACTIVO'];
                $i++;
                ?>
                <tr>
                    <td><?php echo $id; ?></td>
                    <td><?php echo $fechaIni; ?></td>
                    <td><?php echo $fechaTer; ?></td>
                    <td><?php echo $estado; ?></td>
                    <td><input type="button" id="btnEstado" name="btnEstado" value="Modificar Estado"></td>
                    <td><input type="button" id="btnBorrar" name="btnBorrar" value="Borrar"></td>
        <!--                    <td><input type="date" name="fInicio" id="fInicio"></td>
                    <td><input type="date" name="fTermino" id="fTermino"></td> 
        </td>-->                <?php
                    if (isset($_GET['btnBorrar'])) {
                        $borrar_id = $_GET['btnBorrar'];
                        $borrar = "DELETE FROM R_FECHA WHERE R_FECHA_ID = '$borrar_id'";

                        $ejecutar = sqlsrv_query($con, $borrar);
                        if ($ejecutar) {
                            echo "<script>alert('Ha sido Borrado')</script>";
                        }
                    }
                    ?>


                </tr>
    
asked by Francisco Nuñez 23.10.2018 в 20:48
source

1 answer

0

Look, you have some errors, first of all your form is sending the data by the POST method and you are trying to capture the id to be deleted through the $_GET fix that is for when the form data is sent by that method, that is, when in your form the attribute method is equal to "get" or is not established and is assumed by default. The other error is that you are trying to get the id of your botón de eliminar which in its value does not have the id of the record that you are going to delete. The solution would be if you are passing the form data by POST , capture the data through the $_POST array and the second is to change your inputs of type botón by an element button with which you can change the value to the value attribute and know what id you want to delete. That would be the last part of your code:

<tr>
    <td><?php echo $id; ?></td>
    <td><?php echo $fechaIni; ?></td>
    <td><?php echo $fechaTer; ?></td>
    <td><?php echo $estado; ?></td>
    <td><button type="submit" id="btnEstado" name="btnEstado" value="<?php echo $id; ?>">Modificar Estado</button></td>
    <td><button type="submit" id="btnBorrar" name="btnBorrar" value="<?php echo $id; ?>">Borrar</button></td>
    <?php
        if (isset($_POST['btnBorrar'])) {
            $borrar_id = $_POST['btnBorrar'];
            $borrar = "DELETE FROM R_FECHA WHERE R_FECHA_ID = '$borrar_id'";

            $ejecutar = sqlsrv_query($con, $borrar);
            if ($ejecutar) {
                echo "<script>alert('Ha sido Borrado')</script>";
            }
        }
    ?>
</tr>
    
answered by 23.10.2018 в 22:27