Professional Documents
Culture Documents
personas
Campo
Tipo
id
Int (8)
Nombre
Varchar(35)
Telefono
Int (11)
Direccion
Varchar(55)
Foto
Varchar(280)
Extra
Campo Clave
Auto_Increment PrimaryKey
La base de datos que usare se llama AGENDA. Como se observa nicamente es una sola tabla,
adems, debes llenar un registro por lo minimo a travs de tu servidor xampp en la subcategora
phpmyadmin / base de datos (agenda) / tabla (personas).
index.php
Este archivo permite mostrar la ventana principal del sistema agenda, en esta como es de suponerse
el usuario tiene para seleccionar dos opciones ya sea agregar o buscar y le da click al botn
correspondiente, para que esta pantalla funcione correctamente es indispensable que se encuentre
ubicado en la carpeta raz del servidor que en este caso seria C:\xampp\htdocs\xampp\Agenda\ ,
tambin hay que crear otra carpeta dentro de la carpeta agenda llamada imgenes quedando esta
ruta de trabajo asi C:\xampp\htdocs\xampp\agenda\imagenes\ , dentro de esta carpeta vas a
insertar tres imgenes relacionada con agregar, buscar y una ultima que sea un cuaderno en blanco
abierto reconociendo su extesion; cabe destacar que los nombres de estas imgenes son agenda.jpg
que corresponde al cuaderno abierto, agregar.png que corresponde al botn agregar y buscar.png
que corresponde al botn bucar, bueno muestro el cdigo:
<html>
<head>
<title>Agenda</title>
</head>
<style type="text/css">
PG.2*PROF.JEANCARLOSBLANCOPROGRAMACINAVANZADA*
.agenda {
margin:100px auto 0 auto;
width:701px;
height:468px;
background-image:url(imagenes/agenda.jpg);
}
.agenda #contenidor {
padding:25px;
width:276px;
height:428px;
}
</style>
<body>
<div class="agenda">
<div id="contenidor">
<table width="100%" height="404" border="0">
<tr>
<td
height="38"
colspan="3"
align="center"
valign="middle"><h1>Agenda
en
PHP</h1></td>
</tr>
<tr>
PG.3*PROF.JEANCARLOSBLANCOPROGRAMACINAVANZADA*
<td colspan="3" valign="top">Esta agenda fue creada para las clases de PHP a los
estudiantes de programacin avanzada como modo de ejemplo en el cual vamos a mostrar formas
para agregar, modificar y buscar datos en una Base de Datos.<br /><br /><br /><center>
<a href="agregar.php"><img src="imagenes/agregar.png" width="128" height="128" /></a>
<a href="buscar.php"><img src="imagenes/buscar.png" width="128" height="128" /></a>
</center>
</td>
</tr>
</table>
</div>
</div>
</body>
</html>
config.php
<?php
/*
Instituto Universitario Pedaggico Monseor Arias Blanco
Descripcion: Conecta con el Manejador de Base de Datos (DBMS) y deja disponible las variables
globales para ser utilizada posteriormente.
Author:
PG.4*PROF.JEANCARLOSBLANCOPROGRAMACINAVANZADA*
Archivo: config.php
*/
// Configuracion de la base de datos.
$dbhost = "localhost"; // Servidor
$dbuser = "root"; // Usuario
$dbpass = ""; // Contrasea
$dbname = "agenda"; // Base de datos
// Creando conexion.
$link = mysql_connect($dbhost,$dbuser,$dbpass); // Conectamos a la base de datos
mysql_select_db($dbname,$link); // Seleccionamos la base de datos
?>
agregar.php
En la pantalla de agregar si observas aparece los datos bsicos que se necesitan para registrar una
persona, el codigo que muestro a continuacion es del archivo que se muestra en el navegador al dar
click en dicho enlace. Este archivo, en resumen, muestra un formulario creado con PHP para que el
usuario capture sus datos personales envie dichos datos al script agregar.php que guarda los datos en
la base de datos. El codigo es este
<?php
/*
PG.5*PROF.JEANCARLOSBLANCOPROGRAMACINAVANZADA*
Archivo: agregar.php
*/
// Incluimos la configuracion y conexion a la MySQL.
include('config.php');
// Definimos la variable $msg por seguridad.
$msg = "";
// Si se apreta el boton Agendar, da la condicion como true.
if($_POST['agendar'])
{
// Verificamos que no alla ningun dato sin rellenar.
if(!empty($_POST['nombre']) || !empty($_POST['telefono']) || !empty($_POST['direccion']))
{
// Pasamos los datos de los POST a Variables, y le ponemos seguridad.
// "htmlentities = Convierte todos los caracteres aplicables a entidades HTML"
$nombre = htmlentities($_POST['nombre']);
$telefono = htmlentities($_POST['telefono']);
$direccion = htmlentities($_POST['direccion']);
$foto = htmlentities($_POST['foto']);
PG.6*PROF.JEANCARLOSBLANCOPROGRAMACINAVANZADA*
}
.agenda #contenidor {
padding:25px; <!-- Establece la anchura de algunas o todas las zonas de relleno de los
elementos -->
width:276px;
height:428px;
}
</style>
<body>
<div class="agenda">
<div id="contenidor">
<table width="100%" height="404" border="0">
<tr>
<!-- combina las tres columnas en una celda alineado al medio -->
<td height="38" colspan="3" align="center" valign="middle"><h1>Agregar persona</h1></td>
</tr>
<tr>
<td colspan="3" valign="top"><center><em><span
style="color:red;"><?=$msg;?></span></em></center>
<form action="agregar.php" method="post">
<!-- strong es el apropiado para marcar con especial nfasis las partes ms
importantes de un texto -->
PG.8*PROF.JEANCARLOSBLANCOPROGRAMACINAVANZADA*
<strong>Nombre</strong><br />
<input type="text" name="nombre" id="nombre" />
<br />
<br />
<strong>Telfono</strong>
<br />
<input type="text" name="telefono" id="telefono" />
<br />
<br />
<strong>Direccin</strong><br />
<input type="text" name="direccion" id="direccion" />
<br />
<br />
<strong>Link de la Foto</strong><br />
<input type="text" name="foto" id="foto" />
<br />
<br />
<input type="submit" name="agendar" value="Agendar" />
</form>
</td>
</tr>
</table>
PG.9*PROF.JEANCARLOSBLANCOPROGRAMACINAVANZADA*
</div>
</div>
</body>
</html>
buscar.php
<?php
/*
Instituto Universitario Pedaggico Monseor Arias Blanco
Descripcion: Este archivo muestra un formulario que permite al usuario
buscar los datos de una persona registrada en la base de datos.
Author:
Archivo: buscar.php
*/
// Incluimos la configuracion y conexion a la MySQL.
include('config.php');
// Definimos la variable $msg por seguridad.
$msg = "";
// Si se apreta el boton Agendar, da la condicion como true.
if($_GET['agendar'])
{
PG.10*PROF.JEANCARLOSBLANCOPROGRAMACINAVANZADA*
height:468px;
background-image:url(imagenes/agenda.jpg);
}
.agenda #contenidor {
padding:25px;
width:276px;
height:428px;
}
</style>
<body>
<div class="agenda">
<div id="contenidor">
<table width="100%" height="404" border="0">
<tr>
<td
height="38"
colspan="3"
align="center"
valign="middle"><h1>Buscar
Personas</h1></td>
</tr>
<tr>
<td
colspan="3"
valign="top"><center><em><span
style="color:red;"><?=$msg;?></span></em></center><br />
<center><form action="buscar.php" method="get">
<input type="text" name="q" id="q" />
PG.12*PROF.JEANCARLOSBLANCOPROGRAMACINAVANZADA*
PG.13*PROF.JEANCARLOSBLANCOPROGRAMACINAVANZADA*
ver.php
<?php
/*
Instituto Universitario Pedaggico Monseor Arias Blanco
Descripcion: Este archivo muestra un formulario que permite al usuario
Visualizar los datos de una persona registrada en la base de datos.
Author:
Archivo: ver.php
*/
// Incluimos la configuracion y conexion a la MySQL.
include('config.php');
// Definimos el ID de la persona a editar.
$id = htmlentities($_GET['id']);
// Mostramos los datos
$sql = "SELECT * FROM personas WHERE id='".$id."' LIMIT 1";
$query = mysql_query($sql,$link);
$row = mysql_fetch_assoc($query);
?>
<html>
<head>
<title>Agenda - Ver personas</title>
PG.14*PROF.JEANCARLOSBLANCOPROGRAMACINAVANZADA*
</head>
<style type="text/css">
.agenda {
margin:100px auto 0 auto;
width:701px;
height:468px;
background-image:url(imagenes/agenda.jpg);
}
.agenda #contenidor {
padding:25px;
width:276px;
height:428px;
}
</style>
<body>
<div class="agenda">
<div id="contenidor">
<table width="100%" height="404" border="0">
<tr>
<td
height="38"
colspan="3"
align="center"
valign="middle"><h1><?=$row['nombre']?></h1></td>
</tr>
PG.15*PROF.JEANCARLOSBLANCOPROGRAMACINAVANZADA*
<tr>
<td colspan="3" valign="top"><strong>Nombre:</strong>
<?=$row['nombre']?><br />
<br />
<strong>Telefono:</strong>
<?=$row['telefono']?><br />
<br />
<strong>Direccion: </strong><?=$row['direccion']?>
<br />
<br /><br />
<center><img src="<?php if(!empty($row['foto'])){ echo $row['foto']; } else { echo
"imagenes/noavatar.png"; } ?>" width="128" height="128" /></center><br />
<center>[ <a href="editar.php?id=<?=$id?>">Editar Datos</a> ]
</center>
</td>
</tr>
</table>
</div>
</div>
</body>
</html>
PG.16*PROF.JEANCARLOSBLANCOPROGRAMACINAVANZADA*
editar.php
<?php
/*
Instituto Universitario Pedaggico Monseor Arias Blanco
Descripcion: Este archivo muestra un formulario que permite al usuario
modificar los datos de una persona y actualizarla en la base de datos.
Author:
Archivo: editar.php
*/
// Incluimos la configuracion y conexion a la MySQL.
include('config.php');
// Definimos la variable $msg por seguridad.
$msg = "";
// Definimos el ID de la persona a editar.
$id = htmlentities($_GET['id']);
// Si se apreta el boton Agendar, da la condicion como true.
if($_POST['agendar'])
{
// Verificamos que no alla ningun dato sin rellenar.
if(!empty($_POST['nombre']) || !empty($_POST['telefono']) || !empty($_POST['direccion']))
{
PG.17*PROF.JEANCARLOSBLANCOPROGRAMACINAVANZADA*
<head>
<title>Agenda - Editar personas</title>
</head>
<style type="text/css">
.agenda {
margin:100px auto 0 auto;
width:701px;
height:468px;
background-image:url(imagenes/agenda.jpg);
}
.agenda #contenidor {
padding:25px;
width:276px;
height:428px;
}
</style>
<body>
<div class="agenda">
<div id="contenidor">
<table width="100%" height="404" border="0">
<tr>
<td height="38" colspan="3" align="center" valign="middle"><h1>Editar Persona</h1></td>
PG.19*PROF.JEANCARLOSBLANCOPROGRAMACINAVANZADA*
</tr>
<tr>
<td colspan="3" valign="top"><center><em><span style="color:red;"><?=$msg;?></span></em></center>
</form>
</td>
</tr>
</table>
</div>
</div>
</body>
</html>
PG.21*PROF.JEANCARLOSBLANCOPROGRAMACINAVANZADA*