image


Valora:  3/5
Inicio » PHP » PHP Tutorial » Database access




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>&nbsp;<B>Nombre</B></TD> <TD>&nbsp;<B>Apellidos</B>&nbsp;</TD> <TD>&nbsp;<B>Borrar</B>&nbsp;</TD></TR>
<?php      

   while($row = mysql_fetch_array($result)) {
      printf("<tr><td>&nbsp;%s</td><td>&nbsp;%s&nbsp;</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>

Execute View code


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.






WebEstilo.com - Introduzca su e-mail y conozca las novedades. No hacemos Spam.
Google
  Web WebEstilo.com   
 
Valid HTML 4.01!
Última modificación:25 de Diciembre de 2004. Spain - España.
© 1998-2004 por Joaquin Gracia. Todos los derechos reservados.