Problem when deleting with php in a mysqli database

4

I am making a form in which you can already save perfectly in the mysqli database, and I already get the data in an html. Now what I want is to erase with a button.

The thing is that it deletes, but always the last record, and not which one I select from the list independently. If I select the first or the second one, I always delete the last one, and I want to see what's wrong.

Here is the code from where I get the list, and also has the delete button.

<?php


        $consultabla = mysqli_query($conexion, "SELECT * from 001_agendacentral") or die("Error al leer Agenda");
            while ($extraido = mysqli_fetch_assoc($consultabla)) {



          echo '<tr>
                <td>'.$extraido['nombre'].'</td>
                <td>'.$extraido['departamento'].'</td>
                <td>'.$extraido['puesto'].'</td>
                <td>'.$extraido['correo'].'</td>
                <td>'.$extraido['telefono'].'</td>
                <td>
                  <form action="admdelete.php?id='.$extraido['id'].'" method="POST">
                    <input name="id" type="hidden" value="' . $extraido['id'].'"/>
                <input type="submit" class="btn btn-warning" value="Borrar"/></td>
                </tr>';
                }

                mysqli_close($conexion);
                 ?>

Here is the file that performs the deletion procedure.

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

   $conexion = mysqli_connect($host,$user,$pass,$db) or die ("Error al conectar");
     $conexion->set_charset("utf8");

    $id = $_REQUEST['id'];
    echo $id;
       //Borrado de tabla con el Campo Nombre
      $borrar = "DELETE from 001_agendacentral where nombre='$id'" or die("Error al Borrar");

       $resultado = mysqli_query($conexion, $borrar) or die ("Error al Borrar");

     if ($resultado) {

mysqli_close($conexion);
echo "Borrado Correctamente";
# code...
}


?>

Already the id column exists in the database. In case you wonder why I'm not showing it, it's not necessary.

    
asked by Yunier Garcia 25.10.2016 в 23:49
source

2 answers

0

Dear, you have to close the form:

    while ($extraido = mysqli_fetch_assoc($consultabla)) {



      echo '<tr>
            <td>'.$extraido['nombre'].'</td>
            <td>'.$extraido['departamento'].'</td>
            <td>'.$extraido['puesto'].'</td>
            <td>'.$extraido['correo'].'</td>
            <td>'.$extraido['telefono'].'</td>
            <td>
              <form action="admdelete.php?id='.$extraido['id'].'" method="POST">

                <input name="id" type="hidden" value="' . $extraido['id'].'"/>
                <input type="submit" class="btn btn-warning" value="Borrar"/>    

              </form>
            </td>
            </tr>';
    }
    
answered by 26.10.2016 / 02:58
source
2

This is because all the input in which you save the id have the same name .

I recommend that you give them a dynamic name (for example with the number of the id or iterations of the loop):

<input name="id' . $extraido['id']. '" type="hidden" value="' . $extraido['id'].'"/>

So it would look like this:

echo '<tr>
         <td>'.$extraido['nombre'].'</td>
         <td>'.$extraido['departamento'].'</td>
         <td>'.$extraido['puesto'].'</td>
         <td>'.$extraido['correo'].'</td>
         <td>'.$extraido['telefono'].'</td>
         <td>
         <form action="admdelete.php?id='.$extraido['id'].'" method="POST">
             <input name="id' . $extraido['id']. '" type="hidden" value="' . $extraido['id'].'"/>
             <input type="submit" class="btn btn-warning" value="Borrar"/></td>
          </tr>';

By having all the input the same name when you make $id = $_REQUEST['id']; you only get the last value because it overwrites all the previous ones.

    
answered by 25.10.2016 в 23:58