Delete data mysql php [closed]

0

I have a table made where it shows me records of a database in mysql.In the table for each record is created a button that is called delete and what I want to get is that by clicking this record is deleted.

I have this code made in usuarios.php

<?php
include_once('conexion.php');
$consulta = consultausu();

?>

between the first php code and the following is web design

<?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>
          <input name="btneliminar" type="button" value="Eliminar" type="submit" class="btn btn-primary offset-2" onclick = "consultaeli();" />
      </form>
    </td>
  </tr>
  <?php
}
?>

conexion.php

<?php  
//CONECTAMOS CON LA BBDD

$conexion = new mysqli("localhost", "root", "", "discografica");
if ($conexion->connect_errno) {
    echo "Fallo al conectar a MySQL: (" . $conexion->connect_errno . ") " . $conexion->connect_error;
}
$consultaeliminar='';

  function consultaeli(){
  global $conexion, $consultaeliminar;
  $borrar = "DELETE FROM usuarios where nombre='$variable'";
  return $conexion->query($borrar);
}
}
    
asked by francisco 10.02.2018 в 18:18
source

2 answers

1

You have several things on your page.

The first is that you try to delete a record with a call JAVASCRIPT , when your code is in PHP , so it will not work.

The second is that you have a condition in WEHERE , but in the call, you do not pass that parameter.

You have several solutions, but as you do not comment, if you use JQUERY , I'll give you a somewhat generic solution, which would help you.

The first thing is to change the HTML , to be able to call the PHP , which has to delete the record.

<?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="GET" action="acciones.php?accion=consultaeli&nombre=<?php echo $variable['nombre']; ?>">
          <input name="btneliminar" value="Eliminar" type="submit" class="btn btn-primary offset-2"/>
      </form>
    </td>
  </tr>
  <?php
}
?>

Notice that what I do create the form, and in action , call a file actions, which will be where you do all the operations. From the button of the two types you have, I have left only the submit, which is the one that will launch the form.

The file actions.php , just as you have it would be

<?php
    $conexion = new mysqli("localhost", "root", "", "discografica");
    if ($conexion->connect_errno) {
        echo "Fallo al conectar a MySQL: (" . $conexion->connect_errno . ") " . $conexion->connect_error;
    }
    else{
        $accion=$_GET["accion"];
        switch($accion)
        {
            case "consultaeli":

                $nombre=$_GET["nombre"];

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

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

                mysqli_query($conexion, $borrar);
                mysqli_close($conexion);
            break;
        }
    }
?>

If you use JQUERY , it would be similar, but the FORM , you would not have to create it and the call would be made from the event ONCLICK

    
answered by 11.02.2018 / 10:56
source
0

The problem is that you try to activate a php function. But:

<input name="btneliminar" type="button" value="Eliminar" type="submit" class="btn btn-primary offset-2" onclick = "consultaeli();" />

the onclick event of this button will be looking for a javascript function.

On this page there is an example similar to your way of programming: link

But I recommend you learn to perform CRUD (insert, modify, delete)

    
answered by 11.02.2018 в 06:52