Remove database queries with php

-2

Could you tell me how to delete a query from php for my database

  

$conexion = mysqli_connect("127.0.0.1" , "root" , "" , "tiendita") or die ("Error al conectar");/*conexion*/

$resultado = $conexion->query("CALL delCliente();") or die ("Hay problema con la consulta");

while ($row= $resultado->fetch_array(MYSQLI_BOTH)){
    echo "<tr>";
    echo "<td>" . $row["id"] . "</td>";
    echo "<td>" . $row["nombre"] . "</td>";
    echo "<td>" . $row["sexo"] . "</td>";
    /*echo $row[0] . " - " . $row["nombre"] . " - " . $row["sexo"] . "<hr>";*/

}
    
asked by ally_ pretty 30.05.2018 в 04:29
source

2 answers

0

From what I have seen in the response you have left in the comments to what you mean by deleting query is to delete a record from the database.

I recommend that you change the title of your question and be careful with the terms you use in future consultations.

To delete a record with PHP you just have to execute a DELETE FROM query and control the errors.

<?php
$servername = "127.0.0.1";
$username = "root";
$password = "";
$dbname = "tiendita";

// Creación de la conexión
$conexion = new mysqli($servername, $username, $password, $dbname);
// Se comprueba la conexión realizada.
if ($conn->connect_error) {
    die("Error en la conexión: " . $conn->connect_error);
} 

// Consulta para borrar el dato que queramos. 
$sql = "DELETE FROM clientes WHERE id_cliente=1";
//Ejecutamos la consulta y comprobamos que se haya completado correctamente.
if ($conn->query($sql) === TRUE) {
    echo "Registro borrado correctamente.";
} else {
    echo "Error al borrar el registro: " . $conn->error;
}
//Al acabar se cierra la conexión.
$conn->close();
?>

In the query $sql = "DELETE FROM clientes WHERE id_cliente=1"; you would have to create your own query to delete the record or records depending on the value you want. If you want to be able to choose in some way the client id to be deleted from the application, you would have to change the id_cliente=1 for some variable type id_cliente=$idBorrar and recover it from a $ _POST or similar.

    
answered by 30.05.2018 в 09:57
0

First, you must obtain the id you want to remove from your bd:

  • Create a variable and give it the corresponding value:

    $idDelete = $_GET['id'];
    
  • Now that you have the id, just locate it in your sql.

    $borrar = "DELETE * FROM tutabla WHERE id = '$idDelete';";
    
  • answered by 30.05.2018 в 13:19