Deleting records
Finally, to close the cycle, is the deletion of records. Deletion of records is one of the simplest processes.
In order to indicate what element will be deleted, we have used a link to the page borra.phtml by running the ID_Prueba on every record so the page borra.phtml knows what element of the table to delete.
ejem07e.phtml
<!-- PHP Tutorial WebEstilo.com -->
<html>
<head>
<title>PHP Example</title>
</head>
<body>
<H1>Ejemplo de uso de bases de datos con PHP y MySQL</H1>
<?php
include("conex.phtml");
$link=Conectarse();
$result=mysql_query("select * from prueba",$link);
?>
<TABLE BORDER=1 CELLSPACING=1 CELLPADDING=1>
<TR><TD> <B>Nombre</B></TD> <TD> <B>Apellidos</B> </TD> <TD> <B>Borrar</B> </TD></TR>
<?php
while($row = mysql_fetch_array($result)) {
printf("<tr><td> %s</td><td> %s </td><td><a href=\"borra.phtml?id=%d\">Borra</a></td></tr>", $row["Nombre"],$row["Apellidos"],$row["ID_Prueba"]);
}
mysql_free_result($result);
mysql_close($link);
?>
</table>
</body>
</html>
borra.phtml
<?php
include("conex.phtml");
$link=Conectarse();
$id=$_GET['id'];
mysql_query("delete from prueba where ID_Prueba = $id",$link);
header("Location: ejem07e.phtml");
?>
The page borra.phtml connects to the database and deletes the record indicated in the variable $id that has been sent from the page ejem07e.phtml. Once the record has been deleted, the page ejem07e.phtml is reloaded.