Delete mysql log with php

1

What a good day! I'm working on a project and I see the need to delete files from a list, line by line, the detail is that for this to be effective, the record must also be removed from the database. Any suggestions on how to make that match with a delete button and delete the record at the same time?  Part of the code is as follows:

    <table id="aa">
        <tr class="uno" >
            <td><b>titulo</b></td>
            <td><b>descripcion</b></td>
            <td><b>nombre</b></td>
        </tr>
    <?php
    include 'PDF/baterias/config.inc.php';
    $db=new Conect_MySql();
        $sql = "select*from baterias";
        $query = $db->execute($sql);
        while($datos=$db->fetch_row($query)){?>
        <tr>
            <td><?php echo $datos['titulo']; ?></td>
            <td><?php echo $datos['descripcion']; ?></td>
            <td><a href="PDF/baterias/archivo.php?id=<?php echo $datos['id_documento']?>"><?php echo $datos['nombre_archivo']; ?></a></td>
            <td><form action="delete.php" method="post" name="frm">
                <button onclick="deleteRow(this)">borrar</button></form></td>   
        </tr>

      <?php  } ?>

    </table>

<script>
function deleteRow(r) {
var i = r.parentNode.parentNode.rowIndex;
document.getElementById("aa").deleteRow(i);
}
</script>

Thank you in advance, greetings!

    
asked by Juampa Cisneros Carriedo 16.08.2017 в 19:45
source

2 answers

3

What I would do would be with php and mysql in the following way: Apart from not mixing php html in that way (although that is a matter of taste) Go through the button the idbateria or a unique identifier that you use, and then in the file delete.php you can make a Delete from ... where that identifier is that Variable that you pass through post.

<?php
echo"<table id='aa'>
        <tr class='uno' >
            <td><b>titulo</b></td>
            <td><b>descripcion</b></td>
            <td><b>nombre</b></td>
        </tr>";
include 'PDF/baterias/config.inc.php';
$db=new Conect_MySql();
$sql = "select*from baterias";
$query = $db->execute($sql);

while($datos=$db->fetch_row($query))
    {
    echo "<tr>
             <td>";
    echo        $datos["titulo"];
    echo    "</td>
             <td>";
    echo        $datos['descripcion'];
    echo    "</td>
             <td>
                 <a href='PDF/baterias/archivo.php?id=";
    echo $datos["id_documento"];
    echo        "'>";
    echo          $datos['nombre_archivo'];
    echo        "</a>
             </td>
             <td>
                 <form action='delete.php' method='post' name='frm'>
                    <input type="hidden" name="idbateria" value=" .'$datos["identificadordefila"]'. " />
                     <button type='submit'>borrar</button>
                 </form> 
             </td>
             </tr>";
    }
echo "</table>";

?>

Then in delete.php do the corresponding deletion of row:

//Conectar...

mysql_connect("localhost","tu_user","tu_password");

//Seleccionar base de datos...

mysql_select_db("mi_base_datos");

//Creamos la sentencia SQL y la ejecutamos

$sSQL="Delete From baterias Where titulo='$datos[titulo]'";
mysql_query($sSQL);
...

Greetings.

    
answered by 16.08.2017 в 23:51
0

In these cases I usually send an identifying parameter (in this example I will use the ID) using the same URL to the code that will execute the deletion, leaving the URL something like: delete_perfil.php? id = 1 where 1 would be passed through an echo, that from the code the URL would be structured like this: <a href="eliminar_perfil.php?id=<?php echo $row['id']; ?>"><i class="fa fa-trash"></i></a> and then already in the other code I receive the ID through GET and execute the deletion ... It's easy and fast, also as you can see I give icon look to the link using Font Awesome

    
answered by 02.01.2018 в 07:10