php delete data

0

Good I'm trying to delete data from php to mysql but I can not get it. It does not give any errors but it does not work.

here I show the data and create a button to delete the data

  <?php
    while($variable = $consulta->fetch_assoc())
    {

      ?>
      <tr>

    <td><?php echo $variable['nombre']; ?></td>
    <td><?php echo $variable['apellido']; ?></td>
    <td><?php echo $variable['correo']; ?></td>
    <td><?php echo $variable['contrasena']; ?></td>
    <td>
      <form method="post" action="acciones.php?accion=consultaeli&nombre=<?php echo $variable['nombre']; ?>">

          <input name="btneliminar" type="button" value="Eliminar" type="submit" class="btn btn-danger" />
      </form>

    </td>
  </tr>
  <?php

}
?>

and here I have the php to delete

<?php
    $conexion = new mysqli("localhost", "root", "", "bbdd");
    if ($conexion->connect_errno) {
        echo "Fallo al conectar a MySQL: (" . $conexion->connect_errno . ") " . $conexion->connect_error;
    }
    else{

        $accion=$_POST["accion"];
        switch($accion)
        {
            case "consultaeli":

                $nombre=$_POST["nombre"];

                header("Location: usuarios.php"); //pagina desde la que se hace la llamada

                $borrar = "DELETE FROM usuarios where nombre='$nombre'"

                mysqli_query($conexion, $borrar);
                mysqli_close($conexion);
            break;
        }
    }
?>
    
asked by francisco 17.02.2018 в 14:26
source

2 answers

1

From what I see to send the data to the file that processes the query, you have something like that

<form method="post" action="acciones.php?accion=consultaeli&nombre=<?php echo $variable['nombre']; ?>">

Why do not you try adding data in inputs type hidden?
And also remove the two types in the submit, it is not necessary, when giving the type submit, the browser will show it as a button and you can even customize it with CSS

<?php
while($variable = $consulta->fetch_assoc())
{

  ?>
  <tr>

<td><?php echo $variable['nombre']; ?></td>
<td><?php echo $variable['apellido']; ?></td>
<td><?php echo $variable['correo']; ?></td>
<td><?php echo $variable['contrasena']; ?></td>
<td>
  <form method="post" action="acciones.php">
       <input type="hidden" name="action" value="consultaeli">
       <input type="hidden" name="nombre" value="<?php echo $variable['nombre'];?>">             
      <input name="btneliminar" type="button" value="Eliminar" type="submit" class="btn btn-danger" />
  </form>

</td>

       
answered by 17.02.2018 / 15:56
source
0

I think what is happening is that you are trying to access the parameters through $ _POST when the parameters of the url are obtained through $ _GET.

If you want to use $ _POST you should save the values in form fields, like.

<input type="hidden" name ="accion" value="consultaeli"/>
<input type="hidden" name ="nombre" value="<?php echo $variable['nombre'];?>"/>

Anyway, there is no point in using the POST method if the parameters are going to happen in the url, I recommend changing to GET.

A detail, I do not know if it's valid to use two types in the submit button

    
answered by 17.02.2018 в 15:13