Anda di halaman 1dari 35

2011

PROYECTO AGENDA
LEER ANTES, IMPORTANTE: Proyecto con solo la primera parte terminada en la que se ha creado una agenda con todos los datos necesarios en una tabla. Falta la segunda parte en la que se crea la misma agenda pero con los datos en diferentes tablas y con integridad referencial entre ellas, y una posible tercera sin integridad referencial, haciendo el borrado, el editado de datos de usuario y de las notas mediante scripts

Javier Garca Cambronel SEGUNDO DE ASIR 12/12/2011

[PROYECTO AGENDA] 12 de diciembre de 2011

CREACIN BASE DE DATOS AGENDA PRIMERA: UNA TABLA CON TODOS LOS CAMPOS
INDEX.HTML CONFIG.PHP AGREGAR.PHP BUSCAR.PHP EDITAR.PHP BORRAR.PHP

EDITARNOTAS.PHP

FUNCIONAMIENTO DE LA AGENDA EN EL NAVEGADOR


PAGINA PRINCIPAL

COMPROBANDO AGREGAR CONTACTOS


CUANDO FALTA EL NOMBRE CUANDO FALTA EL APELLIDO CUANDO FALTA EL CORREO CUANDO NO FALTA NADA

COMPROBANDO EL BUSCADOR (buscar.php)

COMPROBANDO LA SELECCIN DE USUARIOS, SU VISTA (ver.php)

COMPROBANDO LA FUNCIN DE EDITAR DATOS (editar.php)

COMPROBANDO EL BORRADO DE USUARIOS (borrar.php)

COMPROBANDO EL EDITADO DE NOTAS (editarnotas.php)

SEGUNDO DE ASIR

Pgina 1

[PROYECTO AGENDA] 12 de diciembre de 2011


CREACIN BASE DE DATOS AGENDA PRIMERA: UNA TABLA CON TODOS LOS CAMPOS Lo primero que tenemos que hacer es crear un usuario para ello he optado hacerlo en modo grfico

Despues nos conectamos como ese usuario con el nombre y la contrasea como vemos en la imagen y procedemos a crear la base de datos.

Creamos la base de datos

Y entramos en ella

Ahora creamos la tabla sobre la que vamos a trabajar CREATE TABLE `personas` ( `id` int(8) NOT NULL auto_increment, `nombre` varchar(180) default NULL, `apellidos` varchar(180) default NULL, `correo` varchar(180) default NULL, `telefonofijo` int(12) default NULL, `telefonomovil` int(12) default NULL, `fax` varchar(180) default NULL, `pais` varchar(15) default NULL, `codigopostal` varchar(5) default NULL, `direccion` varchar(180) default NULL, `notas` TEXT default NULL, `foto` varchar(280) default NULL, PRIMARY KEY (`id`) );

SEGUNDO DE ASIR

Pgina 2

[PROYECTO AGENDA] 12 de diciembre de 2011


INDEX.HTML Es el que va a mostrar las opciones de agregar usuario y buscar los que ya tenemos en la base de datos. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Agenda proyecto 2</title> </head> <style type="text/css"> .agenda { margin:100px auto 0 auto; width:841px; height:561px; 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>

SEGUNDO DE ASIR

Pgina 3

[PROYECTO AGENDA] 12 de diciembre de 2011


<td height="38" colspan="3" align="center" valign="middle"><h1>Agenda Proyecto 2</h1></td> </tr> <tr> <td colspan="3" valign="top">SEGUNDO DE ASIR<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>

SEGUNDO DE ASIR

Pgina 4

[PROYECTO AGENDA] 12 de diciembre de 2011


CONFIG.PHP Es el que se va a encargar de hacer la conexin a la base de datos y si en algn futuro tuviramos que hacer algn cambio de base de datos bastara con editar este archivo. <?php // Configuracion de la base de datos. $dbhost = "localhost"; // Servidor $dbuser = "javier"; // Usuario $dbpass = "asir2012"; // Contrasea $dbname = "agenda"; // Tabla

// Creando conexion. $link = mysql_connect($dbhost,$dbuser,$dbpass); // Conectamos a la base de datos mysql_select_db($dbname,$link); // Seleccionamos la base de datos ?>

SEGUNDO DE ASIR

Pgina 5

[PROYECTO AGENDA] 12 de diciembre de 2011


AGREGAR.PHP Es el cdigo que se va a encargar de registrar a los usuarios si cumplen una serie de condiciones. <?php // Incluimos la configuracion y conexion a la MySQL. include('config.php'); // Definimos la variable $msg por seguridad. $msg = "";

// Si se aprieta el boton Registrar, da la condicion como true.


if ($_POST['registrar']) { // Verificamos que no tenga ningun dato considerado importante sin rellenar. if(!empty($_POST['nombre']) AND !empty($_POST['apellidos']) AND !empty($_POST['correo'])) { // Pasamos los datos de los POST a Variables, y le ponemos seguridad. $nombre = htmlentities($_POST['nombre']); $apellidos = htmlentities($_POST['apellidos']); $correo = htmlentities($_POST['correo']); $telefonofijo = htmlentities($_POST['telefonofijo']); $telefonomovil = htmlentities($_POST['telefonomovil']); $fax = htmlentities($_POST['fax']); $pais = htmlentities($_POST['pais']); $codigopostal = htmlentities($_POST['codigopostal']); $direccion = htmlentities($_POST['direccion']); $foto = htmlentities($_POST['foto']); $notas = htmlentities($_POST['notas']); // Insertamos los datos en la base de datos, si da algun error lo muestra.

SEGUNDO DE ASIR

Pgina 6

[PROYECTO AGENDA] 12 de diciembre de 2011


$sql = "INSERT INTO personas (nombre, apellidos, correo, telefonofijo, telefonomovil, fax, pais, codigopostal, direccion, foto, notas) VALUES ('".$nombre."','".$apellidos."','".$correo."','".$telefonofijo."', '".$telefonomovil."','".$fax."','".$pais."','".$codigopostal."','".$direccion."','".$foto."','".$not as."')"; mysql_query($sql,$link) or die(mysql_error()); // Mostramos un mensaje diciendo que todo salio como lo esperado $msg = "Persona registrada en la agenda correctamente"; } else {

// Si hay un dato sin rellenar mostramos el siguiente texto.


$msg = "Falta rellenar algun dato importante. Recuerda que nombre apellidos y correo son campos obligatorios"; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Agenda - Agregar personas</title> </head> <style type="text/css"> .agenda { margin:100px auto 0 auto; width:841px; height:561px; background-image:url(imagenes/agenda.jpg); } .agenda #contenidor { SEGUNDO DE ASIR Pgina 7

[PROYECTO AGENDA] 12 de diciembre de 2011


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"><h2>Agregar contacto</h2></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>Nombre</strong><br /> <input type="text" name="nombre" id="nombre" /> <br /> <strong>Apellidos</strong> <br /> <input type="text" name="apellidos" id="apellidos" /> <br /> <strong>Correo electrnico</strong> <br /> <input type="text" name="correo" id="correo" /> <br />

SEGUNDO DE ASIR

Pgina 8

[PROYECTO AGENDA] 12 de diciembre de 2011


<strong>Telfono fijo</strong> <br /> <input type="text" name="telefonofijo" id="telefonofijo" /> <br /> <strong>Telfono mvil</strong> <br /> <input type="text" name="telefonomovil" id="telefonomovil" /> <br /> <strong>Fax</strong> <br /> <input type="text" name="fax" id="fax" /> <br /> <strong>Pas</strong> <br /> <input type="text" name="pais" id="pais" /> <br /> <strong>Cdigo Postal</strong> <br /> <input type="text" name="codigopostal" id="codigopostal" /> <br /> <strong>Direccin</strong><br /> <input type="text" name="direccion" id="direccion" /> <br /> <strong>Link de la Foto</strong><br /> <input type="text" name="foto" id="foto" /> <br /> <strong>NOTAS</strong><br />

SEGUNDO DE ASIR

Pgina 9

[PROYECTO AGENDA] 12 de diciembre de 2011


<input type="text" name="notas" id="notas" /> <br /> <input type="submit" name="registrar" value="registrar" /> </form> </td> </tr> </table> </div> </div> </body> </html>

SEGUNDO DE ASIR

Pgina 10

[PROYECTO AGENDA] 12 de diciembre de 2011


BUSCAR.PHP Es el cdigo que se va a encargar de buscar un usuario en nuestra base de datos, mostrndonos como resultado el nombre y los apellidos del mismo. <?php // Incluimos la configuracion y conexion a la MySQL. include('config.php'); // Definimos la variable $msg por seguridad. $msg = ""; // Si se apreta el boton Buscar, da la condicion como true. if($_GET['buscar']) { // Verificamos que no tengamos ningun dato sin rellenar. if(!empty($_GET['q'])) { $nombre = htmlentities($_GET['q']); $sql = "SELECT * FROM personas WHERE nombre LIKE '%".$nombre."%'"; $query = mysql_query($sql,$link); // Mostramos un mensaje diciendo que todo sali como lo esperado $msg = "Resultados para el nombre ".$nombre; } else { // Si hay un dato sin rellenar mostramos el siguiente texto. $msg = "Falta rellenar algun dato"; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">

SEGUNDO DE ASIR

Pgina 11

[PROYECTO AGENDA] 12 de diciembre de 2011


<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Agenda - Buscar personas</title> </head> <style type="text/css"> .agenda { margin:100px auto 0 auto; width:841px; height:561px; 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 />

SEGUNDO DE ASIR

Pgina 12

[PROYECTO AGENDA] 12 de diciembre de 2011


<center><form action="buscar.php" method="get"> <input type="text" name="q" id="q" /> <input type="submit" name="buscar" value="Buscar" /> </form></center><br /> <?php if($_GET['buscar'] && !empty($_GET['q'])){ ?> <table width="100%" border="1"> <?php while($row = mysql_fetch_assoc($query)){ ?> <tr> <td> <a href="ver.php?id=<?=$row['id']?>"><?=$row['nombre']?> <?=$row['apellidos']?></a> </td> </tr> <?php } ?> </table> <?php } ?> </td> </tr> </table> </div> </div> </body> </html>

SEGUNDO DE ASIR

Pgina 13

[PROYECTO AGENDA] 12 de diciembre de 2011


EDITAR.PHP Es el cdigo que se va a encargar de editar los datos de un usuario ya creado, modificando solo los valores que cambiemos. <?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['editar']) { // Verificamos que no tengamos ningun dato sin rellenar. if(!empty($_POST['nombre']) AND !empty($_POST['apellidos']) AND !empty($_POST['correo'])) { // Pasamos los datos de los POST a Variables, y le ponemos seguridad. $nombre = htmlentities($_POST['nombre']); $apellidos = htmlentities($_POST['apellidos']); $correo = htmlentities($_POST['correo']); $telefonofijo = htmlentities($_POST['telefonofijo']); $telefonomovil = htmlentities($_POST['telefonomovil']); $fax = htmlentities($_POST['fax']); $pais = htmlentities($_POST['pais']); $codigopostal = htmlentities($_POST['codigopostal']); $direccion = htmlentities($_POST['direccion']); $foto = htmlentities($_POST['foto']);

SEGUNDO DE ASIR

Pgina 14

[PROYECTO AGENDA] 12 de diciembre de 2011


$notas = htmlentities($_POST['notas']); // Insertamos los datos en la base de datos, si da algun error lo muestra. $sql = "UPDATE personas SET nombre='".$nombre."', apellidos='".$apellidos."',correo='".$correo."',telefonofijo='".$telefonofijo."',telefonomovil= '".$telefonomovil."', fax='".$fax."',pais='".$pais."',codigopostal='".$codigopostal."',direccion='".$direccion."', foto='".$foto."', notas='".$notas."' WHERE id='".$id."'"; mysql_query($sql,$link) or die(mysql_error()); // Mostramos un mensaje diciendo que todo salio como lo esperado $msg = "Persona editada correctamente"; } else { // Si hay un dato sin rellenar mostramos el siguiente texto. $msg = "Falta rellenar algun dato importante. Recuerda que nombre apellidos y correo son campos obligatorios"; } } // Mostramos los datos $sql = "SELECT * FROM personas WHERE id='".$id."' LIMIT 1"; $query = mysql_query($sql,$link); $row = mysql_fetch_assoc($query); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Agenda - Editar personas</title> </head> <style type="text/css"> .agenda { SEGUNDO DE ASIR Pgina 15

[PROYECTO AGENDA] 12 de diciembre de 2011


margin:100px auto 0 auto; width:841px; height:561px; 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> </tr> <tr> <td colspan="3" valign="top"><center><em><span style="color:red;"><?=$msg;?></span></em></center> <form action="editar.php?id=<?=$id?>" method="post" > <strong>Nombre</strong><br /> <input type="text" name="nombre" id="nombre" /> <br /> <strong>Apellidos</strong> <br />

SEGUNDO DE ASIR

Pgina 16

[PROYECTO AGENDA] 12 de diciembre de 2011


<input type="text" name="apellidos" id="apellidos" /> <br /> <strong>Correo electrnico</strong> <br /> <input type="text" name="correo" id="correo" /> <br /> <strong>Telfono fijo</strong> <br /> <input type="text" name="telefonofijo" id="telefonofijo" /> <br /> <strong>Telfono mvil</strong> <br /> <input type="text" name="telefonomovil" id="telefonomovil" /> <br /> <strong>Fax</strong> <br /> <input type="text" name="fax" id="fax" /> <br /> <strong>Pas</strong> <br /> <input type="text" name="pais" id="pais" /> <br /> <strong>Cdigo Postal</strong> <br /> <input type="text" name="codigopostal" id="codigopostal" /> <br /> <strong>Direccin</strong><br />

SEGUNDO DE ASIR

Pgina 17

[PROYECTO AGENDA] 12 de diciembre de 2011


<input type="text" name="direccion" id="direccion" /> <br /> <strong>Link de la Foto</strong><br /> <input type="text" name="foto" id="foto" /> <br /> <strong>NOTAS</strong><br /> <input type="text" name="notas" id="notas" /> <br /> <input type="submit" name="editar" value="editar" /> </form> </td> </tr> </table> </div> </div> </body> </html>

SEGUNDO DE ASIR

Pgina 18

[PROYECTO AGENDA] 12 de diciembre de 2011


BORRAR.PHP Es el cdigo que se va a encargar de borrar un usuario, requiriendo para ello, hacer una confirmacin. <?php // Incluimos la configuracion y conexion a la MySQL. include('config.php'); // Definimos el ID de la persona a editar. $id = htmlentities($_GET['id']);

// Si se apreta el boton Borrar, da la condicion como true.


if($_POST['borrar']){ $sql = "DELETE FROM personas WHERE id='".$id."'"; mysql_query($sql,$link) or die(mysql_error()); // Mostramos un mensaje diciendo que todo salio como lo esperado printf ("Persona Borrada correctamente"); } else { printf ("Tiene que confirmar que quiere borrar el USUARIO"); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Agenda - Borrar Usuario</title> </head> <style type="text/css"> .agenda { margin:100px auto 0 auto;

SEGUNDO DE ASIR

Pgina 19

[PROYECTO AGENDA] 12 de diciembre de 2011


width:841px; height:561px; 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>Borrar usuario</h1></td> </tr> <tr> <center> <form action="borrar.php?id=<?=$id?>" method="post" > <input type="submit" name="borrar" value="borrar" /> </form> </center> </tr> </table> </div> </div> SEGUNDO DE ASIR Pgina 20

[PROYECTO AGENDA] 12 de diciembre de 2011


</body> </html>

SEGUNDO DE ASIR

Pgina 21

[PROYECTO AGENDA] 12 de diciembre de 2011


EDITARNOTAS.PHP Es el cdigo encargado de modificar las notas de un usuario. <?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 editarnotas, da la condicion como true. if($_POST['editarnotas']) { // Verificamos hemos escrito algo en la nota. if(!empty($_POST['notas'])) {

// Pasamos los datos de los POST a Variables, y le ponemos seguridad.


$notas = htmlentities($_POST['notas']); // Insertamos los datos en la base de datos, si da algun error lo muestra. $sql = "UPDATE personas SET notas='".$notas."' WHERE id='".$id."'"; mysql_query($sql,$link) or die(mysql_error()); // Mostramos un mensaje diciendo que todo salio como lo esperado $msg = "nota editada correctamente"; } else { // Si hay un dato sin rellenar mostramos el siguiente texto. $msg = "Si quieres editar una nota, tienes que escribir algo obligatoriamente"; } } SEGUNDO DE ASIR Pgina 22

[PROYECTO AGENDA] 12 de diciembre de 2011


// Mostramos los datos
$sql = "SELECT * FROM personas WHERE id='".$id."' LIMIT 1"; $query = mysql_query($sql,$link); $row = mysql_fetch_assoc($query); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Agenda - Editar notas</title> </head> <style type="text/css"> .agenda { margin:100px auto 0 auto; width:841px; height:561px; background-image:url(imagenes/agenda.jpg); } .agenda #contenidor { padding:25px; width:276px; height:428px; } </style> <body> <div class="agenda"> <div id="contenidor"> SEGUNDO DE ASIR Pgina 23

[PROYECTO AGENDA] 12 de diciembre de 2011


<table width="100%" height="404" border="0"> <tr> <td height="38" colspan="3" align="center" valign="middle"><h1>Editar notas</h1></td> </tr> <tr> <td colspan="3" valign="top"><center><em><span style="color:red;"><?=$msg;?></span></em></center> <form action="editarnotas.php?id=<?=$id?>" method="post" > <strong>NOTAS</strong><br /> <input type="text" name="notas" id="notas" /> <br /> <input type="submit" name="editarnotas" value="editarnotas" /> </form> </td> </tr> </table> </div> </div> </body> </html>

SEGUNDO DE ASIR

Pgina 24

[PROYECTO AGENDA] 12 de diciembre de 2011

FUNCIONAMIENTO DE LA AGENDA EN EL NAVEGADOR


PAGINA PRINCIPAL Entramos en la agenda y vemos esto

SEGUNDO DE ASIR

Pgina 25

[PROYECTO AGENDA] 12 de diciembre de 2011


COMPROBANDO AGREGAR CONTACTOS Cuando agregamos un contacto (agregar.php) se debe de comprobar que tanto el nombre como los apellidos y el correos estn rellenados. CUANDO FALTA EL NOMBRE

SEGUNDO DE ASIR

Pgina 26

[PROYECTO AGENDA] 12 de diciembre de 2011


CUANDO FALTA EL APELLIDO

SEGUNDO DE ASIR

Pgina 27

[PROYECTO AGENDA] 12 de diciembre de 2011


CUANDO FALTA EL CORREO

SEGUNDO DE ASIR

Pgina 28

[PROYECTO AGENDA] 12 de diciembre de 2011


CUANDO NO FALTA NADA

SEGUNDO DE ASIR

Pgina 29

[PROYECTO AGENDA] 12 de diciembre de 2011


COMPROBANDO EL BUSCADOR (buscar.php)

SEGUNDO DE ASIR

Pgina 30

[PROYECTO AGENDA] 12 de diciembre de 2011


COMPROBANDO LA SELECCIN DE USUARIOS, SU VISTA (ver.php)

SEGUNDO DE ASIR

Pgina 31

[PROYECTO AGENDA] 12 de diciembre de 2011


COMPROBANDO LA FUNCIN DE EDITAR DATOS (editar.php)

SEGUNDO DE ASIR

Pgina 32

[PROYECTO AGENDA] 12 de diciembre de 2011


COMPROBANDO EL BORRADO DE USUARIOS (borrar.php)

SEGUNDO DE ASIR

Pgina 33

[PROYECTO AGENDA] 12 de diciembre de 2011


COMPROBANDO EL EDITADO DE NOTAS (editarnotas.php)

SEGUNDO DE ASIR

Pgina 34

Anda mungkin juga menyukai