Delete BD record with PHP

0

Hi, I have a little query to see if anyone can help me.

I have a table that connects to my database and brings the records, and now I need that in my table, in each record, there is a delete record button. I have the button created as shown in the following image.

I'm starting in PHP and I'm not sure how to do it

Thanks

Edit: I update the information with the code of the php page where the table is displayed

<?php
 require_once 'conexion.php'; //Esto me conecta con la BD
 
 	$result;

 	$conn = dbConnect();

 	$sql = 'SELECT * FROM avisos';

 	$result = $conn->query($sql);

 	$rows = $result->fetchAll();
 
  ?>

 	<table border="1">
 <!--Comprobar si se ha iniciado sesion-->
		<?php
		session_start();
		if (isset($_SESSION['username'])) {
			echo "Has iniciado sesion como ".$_SESSION['username'];
		}else{
			header("location: index.php");
		}
		 ?>
 		<thead>
 			<tr>
         <th>Averia / Pedido</th>
         <th>Estado</th>
         <th>Observaciones</th>
         <th>Borrar/modificar</th>
 			</tr>
 		</thead>
 		<tbody>
 		<?php
 			foreach ($rows as $row) {
 		?>
 			<tr>
         <td><?php echo $row['averia']; ?></td>
         <td><?php echo $row['estado']; ?></td>
         <td><?php echo $row['observaciones']; ?></td>
         <td>
           <a href="#" class="btn" onlick=""></a>
         </td>
 			</tr>
    
asked by Alejandro 30.12.2017 в 12:12
source

2 answers

1

I will explain it in a simple way since I see that you are learning.

One way you can do it is by creating a link that contains the registration id. So you only have to get the id using $_GET['id'] and you search for the record by that id and delete it.

On your button edit and add the link and specifying the url:

<td>
  <a href="<?php echo $_SERVER['SERVER_NAME'] . '/index.php?eliminar='. echo $row['id'] ?> " class="btn" onlick=""></a>
</td>

The final url would be like this: http://localhost/nombreApp/index.php?eliminar=numeroIdRegistro .

So now, you just have to find out if the eliminar parameter was sent to your page through the url in the following way:

require_once 'conexion.php'; //Esto me conecta con la BD

$result;
$conn = dbConnect();
$sql = 'SELECT * FROM avisos';


    if(isset($_GET["eliminar"]))
    {
        // eliminas el registro
        $conexion->query("DELETE FROM tabla WHERE id = ". $_GET["eliminar]);
    }

    //...

All this happens on the same page. The only thing that was added was the link and the condition to eliminate.

The right thing to do to send the record by POST, and thus prevent it from intentionally deleting record but as only an example, I think you can start with that.

Here is the documentation for $_SERVER["SEVER_NAME"] and $_GET in case you need more information.

    
answered by 30.12.2017 в 15:16
0

In the same way that you are associating the fields of your query with the HTML table, you must associate the ID of the table of your BD with the Delete button: <a href="ruta/archivo.php?id=<?php echo $row['ID']; ?>" class="btn" ><?php echo $row['ID']; ?></a>

One option that you can use is to create a separate PHP file with the definition of the function that will delete the record in BD; after that, in the href property of the HTML '', you will define the path where the file you created is located and you will send the parameter by the GET method (this is achieved with the concatenation of the path of the file, plus the sign '?' , plus the variable pair = value).

In this way you only have to validate the variable 'id' is defined and proceed with the deletion of the record.

    
answered by 30.12.2017 в 15:25