Modelo Php

1,808 views

Published on

Published in: Education, Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,808
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
132
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Modelo Php

  1. 1. Ejemplo de archivos en PHP<br />Módulo Programa<br />
  2. 2. Ejemplo de conexion.php<br />
  3. 3. conexion.php<br />&lt;?PHP<br />$dbhost=&quot;localhost&quot;;<br />$dbusuario=&quot;root&quot;;<br />$dbclave=&quot;&quot;;<br />$db=&quot;censo&quot;;<br />$conexion= mysql_connect($dbhost, $dbusuario, $dbclave);<br />if($conexion==0)<br />{<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Fallo la Conexion al Servidor!!!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;incluir.html&apos;&lt;/script&gt;&quot;;<br />}<br />else<br />{<br />if(mysql_select_db($db)==0)<br /> {<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Fallo la Conexion a la Base de Datos!!!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;incluir.html&apos;&lt;/script&gt;&quot;;<br /> } <br />}<br />?&gt; <br />Se requiere la apertura del código PHP<br />
  4. 4. conexion.php<br />&lt;?PHP<br />$dbhost=&quot;localhost&quot;;<br />$dbusuario=&quot;root&quot;;<br />$dbclave=&quot;&quot;;<br />$db=&quot;censo&quot;;<br />$conexion= mysql_connect($dbhost, $dbusuario, $dbclave);<br />if($conexion==0)<br />{<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Fallo la Conexion al Servidor!!!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;incluir.html&apos;&lt;/script&gt;&quot;;<br />}<br />else<br />{<br />if(mysql_select_db($db)==0)<br /> {<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Fallo la Conexion a la Base de Datos!!!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;incluir.html&apos;&lt;/script&gt;&quot;;<br /> } <br />}<br />?&gt; <br />Se identifica la dirección del servidor, el nombre de usuario y clave con la que se da acceso al mismo. Por ultimo se hace mención al nombre de la base de datos: en este caso “censo”<br />
  5. 5. conexion.php<br />&lt;?PHP<br />$dbhost=&quot;localhost&quot;;<br />$dbusuario=&quot;root&quot;;<br />$dbclave=&quot;&quot;;<br />$db=&quot;censo&quot;;<br />$conexion= mysql_connect($dbhost, $dbusuario, $dbclave);<br />if($conexion==0)<br />{<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Fallo la Conexion al Servidor!!!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;incluir.html&apos;&lt;/script&gt;&quot;;<br />}<br />else<br />{<br />if(mysql_select_db($db)==0)<br /> {<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Fallo la Conexion a la Base de Datos!!!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;incluir.html&apos;&lt;/script&gt;&quot;;<br /> } <br />}<br />?&gt; <br />Mysql_connect permite establecer conexión con el servidor… la variable $conexion permitirá luego cerrar este enlace con dicho servidor<br />
  6. 6. conexion.php<br />&lt;?PHP<br />$dbhost=&quot;localhost&quot;;<br />$dbusuario=&quot;root&quot;;<br />$dbclave=&quot;&quot;;<br />$db=&quot;censo&quot;;<br />$conexion= mysql_connect($dbhost, $dbusuario, $dbclave);<br />if($conexion==0)<br />{<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Fallo la Conexion al Servidor!!!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;incluir.html&apos;&lt;/script&gt;&quot;;<br />}<br />else<br />{<br />if(mysql_select_db($db)==0)<br /> {<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Fallo la Conexion a la Base de Datos!!!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;incluir.html&apos;&lt;/script&gt;&quot;;<br /> }<br />}<br />?&gt; <br />Se valida si la conexión solicitada tuvo éxito. De no ser así, la función retorna 0 y programamos el mensaje correspondiente.<br />
  7. 7. conexion.php<br />&lt;?PHP<br />$dbhost=&quot;localhost&quot;;<br />$dbusuario=&quot;root&quot;;<br />$dbclave=&quot;&quot;;<br />$db=&quot;censo&quot;;<br />$conexion= mysql_connect($dbhost, $dbusuario, $dbclave);<br />if($conexion==0)<br />{<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Fallo la Conexion al Servidor!!!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;incluir.html&apos;&lt;/script&gt;&quot;;<br />}<br />else<br />{<br />if(mysql_select_db($db)==0)<br /> {<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Fallo la Conexion a la Base de Datos!!!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;incluir.html&apos;&lt;/script&gt;&quot;;<br /> }<br />}<br />?&gt; <br />Si se logra establecer la conexión, entonces se utiliza mysql_selec_db para enlazar con la base de datos correspondiente. También es necesario validar dicho enlace para prevenir fallos en el acceso a la data<br />
  8. 8. conexion.php<br />&lt;?PHP<br />$dbhost=&quot;localhost&quot;;<br />$dbusuario=&quot;root&quot;;<br />$dbclave=&quot;&quot;;<br />$db=&quot;censo&quot;;<br />$conexion= mysql_connect($dbhost, $dbusuario, $dbclave);<br />if($conexion==0)<br />{<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Fallo la Conexion al Servidor!!!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;incluir.html&apos;&lt;/script&gt;&quot;;<br />}<br />else<br />{<br />if(mysql_select_db($db)==0)<br /> {<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Fallo la Conexion a la Base de Datos!!!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;incluir.html&apos;&lt;/script&gt;&quot;;<br /> } <br />}<br />?&gt; <br />Código completo del archivo conexion.php<br />
  9. 9. Ejemplo de incluir.html<br />
  10. 10. Incluir.html<br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;title&gt;&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&lt;h1 align=&quot;center&quot;&gt;Incluir Programas&lt;/h1&gt;<br />&lt;formname=&quot;form_prog&quot; method=&quot;post&quot; action=&quot;acciones.php&quot;&gt;<br />C&oacute;digo: &lt;input type=&quot;text&quot; name=&quot;cod_prog&quot;&gt;&lt;br&gt;<br /> Nombre: &lt;input type=&quot;text&quot; name=&quot;nom_prog&quot; size=&quot;30&quot;&gt;&lt;br&gt;<br /> &Aacute;rea: &lt;input name=&quot;area&quot; type=&quot;text&quot; id=&quot;area&quot; size=&quot;30&quot;&gt;<br /> &lt;br&gt;&lt;br&gt;<br /> &lt;input type=&quot;submit&quot; name=&quot;grabar&quot; value=&quot;Grabar&quot;&gt;<br /> &lt;input type=&quot;submit&quot; name=&quot;mostrar&quot; value=&quot;Mostrar&quot;&gt;<br /> &lt;input type=&quot;reset&quot; name=&quot;limpiar&quot; value=&quot;Limpiar&quot;&gt;<br />&lt;/form&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br />Estructura básica de HTML<br />
  11. 11. Incluir.html<br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;title&gt;&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&lt;h1 align=&quot;center&quot;&gt;Incluir Programas&lt;/h1&gt;<br />&lt;formname=&quot;form_prog&quot; method=&quot;post&quot; action=&quot;acciones.php&quot;&gt;<br />C&oacute;digo: &lt;input type=&quot;text&quot; name=&quot;cod_prog&quot;&gt;&lt;br&gt;<br /> Nombre: &lt;input type=&quot;text&quot; name=&quot;nom_prog&quot; size=&quot;30&quot;&gt;&lt;br&gt;<br /> &Aacute;rea: &lt;input name=&quot;area&quot; type=&quot;text&quot; id=&quot;area&quot; size=&quot;30&quot;&gt;<br /> &lt;br&gt;&lt;br&gt;<br /> &lt;input type=&quot;submit&quot; name=&quot;grabar&quot; value=&quot;Grabar&quot;&gt;<br /> &lt;input type=&quot;submit&quot; name=&quot;mostrar&quot; value=&quot;Mostrar&quot;&gt;<br /> &lt;input type=&quot;reset&quot; name=&quot;limpiar&quot; value=&quot;Limpiar&quot;&gt;<br />&lt;/form&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br />Se codifica el título del formulario y se crea la estructura del mismo. El destino de los datos será el archivo acciones.php<br />
  12. 12. Incluir.html<br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;title&gt;&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&lt;h1 align=&quot;center&quot;&gt;Incluir Programas&lt;/h1&gt;<br />&lt;formname=&quot;form_prog&quot; method=&quot;post&quot; action=&quot;acciones.php&quot;&gt;<br />C&oacute;digo: &lt;input type=&quot;text&quot; name=&quot;cod_prog&quot;&gt;&lt;br&gt;<br /> Nombre: &lt;input type=&quot;text&quot; name=&quot;nom_prog&quot; size=&quot;30&quot;&gt;&lt;br&gt;<br /> &Aacute;rea: &lt;input name=&quot;area&quot; type=&quot;text&quot; id=&quot;area&quot; size=&quot;30&quot;&gt;<br /> &lt;br&gt;&lt;br&gt;<br />&lt;input type=&quot;submit&quot; name=&quot;grabar&quot; value=&quot;Grabar&quot;&gt;<br /> &lt;input type=&quot;submit&quot; name=&quot;mostrar&quot; value=&quot;Mostrar&quot;&gt;<br /> &lt;input type=&quot;reset&quot; name=&quot;limpiar&quot; value=&quot;Limpiar&quot;&gt;<br />&lt;/form&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br />Se crean los elementos del formulario que recibirán los datos. Este código puede ser enriquecido con validaciones en javascript. El nombre de cada objeto debería ser el mismo que el de los campos de la tabla.<br />
  13. 13. Incluir.html<br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;title&gt;&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&lt;h1 align=&quot;center&quot;&gt;Incluir Programas&lt;/h1&gt;<br />&lt;formname=&quot;form_prog&quot; method=&quot;post&quot; action=&quot;acciones.php&quot;&gt;<br />C&oacute;digo: &lt;input type=&quot;text&quot; name=&quot;cod_prog&quot;&gt;&lt;br&gt;<br /> Nombre: &lt;input type=&quot;text&quot; name=&quot;nom_prog&quot; size=&quot;30&quot;&gt;&lt;br&gt;<br /> &Aacute;rea: &lt;input name=&quot;area&quot; type=&quot;text&quot; id=&quot;area&quot; size=&quot;30&quot;&gt;<br /> &lt;br&gt;&lt;br&gt;<br /> &lt;input type=&quot;submit&quot; name=&quot;grabar&quot; value=&quot;Grabar&quot;&gt;<br /> &lt;input type=&quot;submit&quot; name=&quot;mostrar&quot; value=&quot;Mostrar&quot;&gt;<br /> &lt;input type=&quot;reset&quot; name=&quot;limpiar&quot; value=&quot;Limpiar&quot;&gt;<br />&lt;/form&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br />Luego se programan los botones , teniendo en cuenta que el nombre asignado a éstos permitirá luego saber que opción activó el usuario.<br />
  14. 14. Incluir.html<br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;title&gt;&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&lt;h1 align=&quot;center&quot;&gt;Incluir Programas&lt;/h1&gt;<br />&lt;formname=&quot;form_prog&quot; method=&quot;post&quot; action=&quot;acciones.php&quot;&gt;<br />C&oacute;digo: &lt;input type=&quot;text&quot; name=&quot;cod_prog&quot;&gt;&lt;br&gt;<br /> Nombre: &lt;input type=&quot;text&quot; name=&quot;nom_prog&quot; size=&quot;30&quot;&gt;&lt;br&gt;<br /> &Aacute;rea: &lt;input name=&quot;area&quot; type=&quot;text&quot; id=&quot;area&quot; size=&quot;30&quot;&gt;<br /> &lt;br&gt;&lt;br&gt;<br /> &lt;input type=&quot;submit&quot; name=&quot;grabar&quot; value=&quot;Grabar&quot;&gt;<br /> &lt;input type=&quot;submit&quot; name=&quot;mostrar&quot; value=&quot;Mostrar&quot;&gt;<br /> &lt;input type=&quot;reset&quot; name=&quot;limpiar&quot; value=&quot;Limpiar&quot;&gt;<br />&lt;/form&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br />Código terminado del archivo incluir.html<br />
  15. 15. Ejemplo de acciones.php<br />Para grabar<br />
  16. 16. &lt;?PHP<br />if($_POST[grabar])<br />{<br />include&quot;../conexion.php&quot;;<br /> $query=&quot;INSERT INTO programa (cod_prog, nom_prog, area) VALUES (&apos;$_POST[cod_prog]&apos;, &apos;$_POST[nom_prog]&apos;, &apos;$_POST[area]&apos;)&quot;;<br />mysql_query($query); <br />mysql_close($conexion);<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Registro Incluido!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;incluir.html&apos;;&lt;/script&gt;&quot;;<br />}<br />if($_POST[eliminar])<br />{<br />include &quot;../conexion.php&quot;; <br /> $query=&quot;DELETE FROM programa WHERE cod_prog=&apos;$_POST[cod_prog]&apos;&quot;;<br />mysql_query($query);<br />mysql_close($conexion);<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Registro Eliminado!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;mostrar.php&apos;&lt;/script&gt;&quot;;<br />}<br />if($_POST[cancelar] || $_POST[mostrar])<br />{<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;mostrar.php&apos;&lt;/script&gt;&quot;;<br />}<br />if($_POST[actualizar])<br />{<br />include&quot;../conexion.php&quot;; <br /> $query=&quot;UPDATE programa SET cod_prog=&apos;$_POST[cod_prog]&apos;, nom_prog=&apos;$_POST[nom_prog]&apos;, area=&apos;$_POST[area]&apos; WHERE cod_prog=&apos;$_POST[cod_prog2]&apos;&quot;;<br />mysql_query($query);<br />mysql_close($conexion);<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Registro Modificado!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;mostrar.php&apos;&lt;/script&gt;&quot;;<br />}<br />?&gt;<br />acciones.php<br />Dentro del código PHP se utiliza una condición para determinar si el usuario activó el botón con el nombre “grabar”<br />
  17. 17. &lt;?PHP<br />if($_POST[grabar])<br />{<br />include&quot;../conexion.php&quot;;<br /> $query=&quot;INSERT INTO programa (cod_prog, nom_prog, area) VALUES (&apos;$_POST[cod_prog]&apos;, &apos;$_POST[nom_prog]&apos;, &apos;$_POST[area]&apos;)&quot;;<br />mysql_query($query); <br />mysql_close($conexion);<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Registro Incluido!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;incluir.html&apos;;&lt;/script&gt;&quot;;<br />}<br />if($_POST[eliminar])<br />{<br />include &quot;../conexion.php&quot;; <br /> $query=&quot;DELETE FROM programa WHERE cod_prog=&apos;$_POST[cod_prog]&apos;&quot;;<br />mysql_query($query);<br />mysql_close($conexion);<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Registro Eliminado!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;mostrar.php&apos;&lt;/script&gt;&quot;;<br />}<br />if($_POST[cancelar] || $_POST[mostrar])<br />{<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;mostrar.php&apos;&lt;/script&gt;&quot;;<br />}<br />if($_POST[actualizar])<br />{<br />include&quot;../conexion.php&quot;; <br /> $query=&quot;UPDATE programa SET cod_prog=&apos;$_POST[cod_prog]&apos;, nom_prog=&apos;$_POST[nom_prog]&apos;, area=&apos;$_POST[area]&apos; WHERE cod_prog=&apos;$_POST[cod_prog2]&apos;&quot;;<br />mysql_query($query);<br />mysql_close($conexion);<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Registro Modificado!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;mostrar.php&apos;&lt;/script&gt;&quot;;<br />}<br />?&gt;<br />acciones.php<br />Usando el include se establecen las conexiones al servidor y a la base de datos. Luego la variable $query almacena la sentencia en DML que será ejecutada con el mysql_query. El comando $_POST permite capturar valores del formulario que invocó este archivo.<br />
  18. 18. &lt;?PHP<br />if($_POST[grabar])<br />{<br />include&quot;../conexion.php&quot;;<br /> $query=&quot;INSERT INTO programa (cod_prog, nom_prog, area) VALUES (&apos;$_POST[cod_prog]&apos;, &apos;$_POST[nom_prog]&apos;, &apos;$_POST[area]&apos;)&quot;;<br />mysql_query($query); <br />mysql_close($conexion);<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Registro Incluido!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;incluir.html&apos;;&lt;/script&gt;&quot;;<br />}<br />if($_POST[eliminar])<br />{<br />include &quot;../conexion.php&quot;; <br /> $query=&quot;DELETE FROM programa WHERE cod_prog=&apos;$_POST[cod_prog]&apos;&quot;;<br />mysql_query($query);<br />mysql_close($conexion);<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Registro Eliminado!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;mostrar.php&apos;&lt;/script&gt;&quot;;<br />}<br />if($_POST[cancelar] || $_POST[mostrar])<br />{<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;mostrar.php&apos;&lt;/script&gt;&quot;;<br />}<br />if($_POST[actualizar])<br />{<br />include&quot;../conexion.php&quot;; <br /> $query=&quot;UPDATE programa SET cod_prog=&apos;$_POST[cod_prog]&apos;, nom_prog=&apos;$_POST[nom_prog]&apos;, area=&apos;$_POST[area]&apos; WHERE cod_prog=&apos;$_POST[cod_prog2]&apos;&quot;;<br />mysql_query($query);<br />mysql_close($conexion);<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Registro Modificado!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;mostrar.php&apos;&lt;/script&gt;&quot;;<br />}<br />?&gt;<br />acciones.php<br />Usando mysql_close se cierra la conexión al servidor y se envía al usuario el mensaje correspondiente<br />
  19. 19. &lt;?PHP<br />if($_POST[grabar])<br />{<br />include&quot;../conexion.php&quot;;<br /> $query=&quot;INSERT INTO programa (cod_prog, nom_prog, area) VALUES (&apos;$_POST[cod_prog]&apos;, &apos;$_POST[nom_prog]&apos;, &apos;$_POST[area]&apos;)&quot;;<br />mysql_query($query); <br />mysql_close($conexion);<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Registro Incluido!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;incluir.html&apos;;&lt;/script&gt;&quot;;<br />}<br />if($_POST[eliminar])<br />{<br />include &quot;../conexion.php&quot;; <br /> $query=&quot;DELETE FROM programa WHERE cod_prog=&apos;$_POST[cod_prog]&apos;&quot;;<br />mysql_query($query);<br />mysql_close($conexion);<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Registro Eliminado!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;mostrar.php&apos;&lt;/script&gt;&quot;;<br />}<br />if($_POST[cancelar] || $_POST[mostrar])<br />{<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;mostrar.php&apos;&lt;/script&gt;&quot;;<br />}<br />if($_POST[actualizar])<br />{<br />include&quot;../conexion.php&quot;; <br /> $query=&quot;UPDATE programa SET cod_prog=&apos;$_POST[cod_prog]&apos;, nom_prog=&apos;$_POST[nom_prog]&apos;, area=&apos;$_POST[area]&apos; WHERE cod_prog=&apos;$_POST[cod_prog2]&apos;&quot;;<br />mysql_query($query);<br />mysql_close($conexion);<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Registro Modificado!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;mostrar.php&apos;&lt;/script&gt;&quot;;<br />}<br />?&gt;<br />acciones.php<br />Esta sección de código permite guardar un registro dentro de la base de datos.<br />
  20. 20. Ejemplo de mostrar.php<br />
  21. 21. &lt;html&gt;<br />&lt;head&gt;&lt;title&gt;&lt;/title&gt;&lt;/head&gt;<br />&lt;body&gt;<br />&lt;h2 align=&quot;center&quot;&gt;Programas Registrados&lt;/h2&gt;&lt;br&gt;<br />&lt;?php<br />include&quot;../conexion.php&quot;; <br />$query=&quot;SELECT * FROM programa ORDER BY cod_prog ASC&quot;;<br />$solicitud=mysql_query($query);<br />mysql_close($conexion); <br />$num_total_registros = mysql_num_rows($solicitud);<br />if($num_total_registros!=0)<br />{<br /> echo&quot;&lt;tablealign=&apos;center&apos; border=&apos;1&apos;&gt;<br /> &lt;tr&gt;&lt;td&gt;C&oacute;digo&lt;/td&gt; &lt;td&gt;Nombre&lt;/td&gt;&lt;td&gt;&Aacute;rea&lt;/td&gt;&lt;td&gt;Opciones&lt;/td&gt;&lt;/tr&gt;&quot;;<br />while ($tupla= mysql_fetch_array($solicitud)) <br /> {<br /> echo&quot;&lt;tr&gt;<br /> &lt;td&gt;$tupla[cod_prog]&lt;/td&gt;&lt;td&gt;$tupla[nom_prog]&lt;/td&gt;&lt;td&gt;$tupla[area]&lt;/td&gt;<br /> &lt;td&gt;&lt;formname=&apos;form_mostrar&apos; method=&apos;post&apos; action=&apos;editar_borrar.php&apos;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;editar&apos; value=&apos;Editar&apos;&gt; <br /> &lt;input type=&apos;submit&apos; name=&apos;eliminar&apos; value=&apos;Eliminar&apos;&gt;<br /> &lt;input type=&apos;hidden&apos; name=&apos;cod_prog&apos; value=&apos;$tupla[cod_prog]&apos;&gt;<br /> &lt;/form&gt;&lt;/td&gt;&lt;/tr&gt;&quot;;<br /> }<br /> echo&quot;&lt;/table&gt;&quot;;<br />}<br />else<br />{<br /> echo&quot;&lt;h2&gt;No se han encontrado registros almacenados&lt;br&gt;en esta categor&iacute;a&lt;/h2&gt;&quot;;<br />}<br />?&gt;<br />&lt;br&gt;<br />&lt;form id=&quot;form1&quot; name=&quot;form1&quot; method=&quot;post&quot; action=&quot;incluir.html&quot;&gt;<br /> &lt;input name=&quot;nuevo&quot; type=&quot;submit&quot; id=&quot;nuevo&quot; value=&quot;Nuevo&quot; /&gt;<br />&lt;/form&gt;<br />&lt;/body&gt;&lt;/html&gt;<br />mostrar.php<br />Se codifica una estructura básica de HTML<br />
  22. 22. &lt;html&gt;<br />&lt;head&gt;&lt;title&gt;&lt;/title&gt;&lt;/head&gt;<br />&lt;body&gt;<br />&lt;h2 align=&quot;center&quot;&gt;Programas Registrados&lt;/h2&gt;&lt;br&gt;<br />&lt;?php<br />include&quot;../conexion.php&quot;; <br />$query=&quot;SELECT * FROM programa ORDER BY cod_prog ASC&quot;;<br />$solicitud=mysql_query($query);<br />mysql_close($conexion); <br />$num_total_registros = mysql_num_rows($solicitud);<br />if($num_total_registros!=0)<br />{<br /> echo&quot;&lt;tablealign=&apos;center&apos; border=&apos;1&apos;&gt;<br /> &lt;tr&gt;&lt;td&gt;C&oacute;digo&lt;/td&gt; &lt;td&gt;Nombre&lt;/td&gt;&lt;td&gt;&Aacute;rea&lt;/td&gt;&lt;td&gt;Opciones&lt;/td&gt;&lt;/tr&gt;&quot;;<br />while ($tupla= mysql_fetch_array($solicitud)) <br /> {<br /> echo&quot;&lt;tr&gt;<br /> &lt;td&gt;$tupla[cod_prog]&lt;/td&gt;&lt;td&gt;$tupla[nom_prog]&lt;/td&gt;&lt;td&gt;$tupla[area]&lt;/td&gt;<br /> &lt;td&gt;&lt;formname=&apos;form_mostrar&apos; method=&apos;post&apos; action=&apos;editar_borrar.php&apos;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;editar&apos; value=&apos;Editar&apos;&gt; <br /> &lt;input type=&apos;submit&apos; name=&apos;eliminar&apos; value=&apos;Eliminar&apos;&gt;<br /> &lt;input type=&apos;hidden&apos; name=&apos;cod_prog&apos; value=&apos;$tupla[cod_prog]&apos;&gt;<br /> &lt;/form&gt;&lt;/td&gt;&lt;/tr&gt;&quot;;<br /> }<br /> echo&quot;&lt;/table&gt;&quot;;<br />}<br />else<br />{<br /> echo&quot;&lt;h2&gt;No se han encontrado registros almacenados&lt;br&gt;en esta categor&iacute;a&lt;/h2&gt;&quot;;<br />}<br />?&gt;<br />&lt;br&gt;<br />&lt;form id=&quot;form1&quot; name=&quot;form1&quot; method=&quot;post&quot; action=&quot;incluir.html&quot;&gt;<br /> &lt;input name=&quot;nuevo&quot; type=&quot;submit&quot; id=&quot;nuevo&quot; value=&quot;Nuevo&quot; /&gt;<br />&lt;/form&gt;<br />&lt;/body&gt;&lt;/html&gt;<br />mostrar.php<br />Luego de titular el documento, se usa msql_query para realizar una consulta de todos los registros. La variable $solicitud almacena el resultado de la consulta y se cierra la conexión.<br />
  23. 23. &lt;html&gt;<br />&lt;head&gt;&lt;title&gt;&lt;/title&gt;&lt;/head&gt;<br />&lt;body&gt;<br />&lt;h2 align=&quot;center&quot;&gt;Programas Registrados&lt;/h2&gt;&lt;br&gt;<br />&lt;?php<br />include&quot;../conexion.php&quot;; <br />$query=&quot;SELECT * FROM programa ORDER BY cod_prog ASC&quot;;<br />$solicitud=mysql_query($query);<br />mysql_close($conexion); <br />$num_total_registros = mysql_num_rows($solicitud);<br />if($num_total_registros!=0)<br />{<br />echo&quot;&lt;tablealign=&apos;center&apos; border=&apos;1&apos;&gt;<br /> &lt;tr&gt;&lt;td&gt;C&oacute;digo&lt;/td&gt; &lt;td&gt;Nombre&lt;/td&gt;&lt;td&gt;&Aacute;rea&lt;/td&gt;&lt;td&gt;Opciones&lt;/td&gt;&lt;/tr&gt;&quot;;<br />while ($tupla= mysql_fetch_array($solicitud)) <br /> {<br /> echo&quot;&lt;tr&gt;<br /> &lt;td&gt;$tupla[cod_prog]&lt;/td&gt;&lt;td&gt;$tupla[nom_prog]&lt;/td&gt;&lt;td&gt;$tupla[area]&lt;/td&gt;<br /> &lt;td&gt;&lt;formname=&apos;form_mostrar&apos; method=&apos;post&apos; action=&apos;editar_borrar.php&apos;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;editar&apos; value=&apos;Editar&apos;&gt; <br /> &lt;input type=&apos;submit&apos; name=&apos;eliminar&apos; value=&apos;Eliminar&apos;&gt;<br /> &lt;input type=&apos;hidden&apos; name=&apos;cod_prog&apos; value=&apos;$tupla[cod_prog]&apos;&gt;<br /> &lt;/form&gt;&lt;/td&gt;&lt;/tr&gt;&quot;;<br /> }<br /> echo&quot;&lt;/table&gt;&quot;;<br />}<br />else<br />{<br /> echo&quot;&lt;h2&gt;No se han encontrado registros almacenados&lt;br&gt;en esta categor&iacute;a&lt;/h2&gt;&quot;;<br />}<br />?&gt;<br />&lt;br&gt;<br />&lt;form id=&quot;form1&quot; name=&quot;form1&quot; method=&quot;post&quot; action=&quot;incluir.html&quot;&gt;<br /> &lt;input name=&quot;nuevo&quot; type=&quot;submit&quot; id=&quot;nuevo&quot; value=&quot;Nuevo&quot; /&gt;<br />&lt;/form&gt;<br />&lt;/body&gt;&lt;/html&gt;<br />mostrar.php<br />Usando mysql_num_rows sabemos cuantos registros fueron encontrados en las consulta. Si la variable $nom_total_registros es igual a cero, se indica al usuario que no hay valores en la tabla<br />
  24. 24. &lt;html&gt;<br />&lt;head&gt;&lt;title&gt;&lt;/title&gt;&lt;/head&gt;<br />&lt;body&gt;<br />&lt;h2 align=&quot;center&quot;&gt;Programas Registrados&lt;/h2&gt;&lt;br&gt;<br />&lt;?php<br />include&quot;../conexion.php&quot;; <br />$query=&quot;SELECT * FROM programa ORDER BY cod_prog ASC&quot;;<br />$solicitud=mysql_query($query);<br />mysql_close($conexion); <br />$num_total_registros = mysql_num_rows($solicitud);<br />if($num_total_registros!=0)<br />{<br /> echo&quot;&lt;tablealign=&apos;center&apos; border=&apos;1&apos;&gt;<br /> &lt;tr&gt;&lt;td&gt;C&oacute;digo&lt;/td&gt; &lt;td&gt;Nombre&lt;/td&gt;&lt;td&gt;&Aacute;rea&lt;/td&gt;&lt;td&gt;Opciones&lt;/td&gt;&lt;/tr&gt;&quot;;<br />while ($tupla= mysql_fetch_array($solicitud)) <br /> {<br />echo&quot;&lt;tr&gt;<br /> &lt;td&gt;$tupla[cod_prog]&lt;/td&gt;&lt;td&gt;$tupla[nom_prog]&lt;/td&gt;&lt;td&gt;$tupla[area]&lt;/td&gt;<br /> &lt;td&gt;&lt;formname=&apos;form_mostrar&apos; method=&apos;post&apos; action=&apos;editar_borrar.php&apos;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;editar&apos; value=&apos;Editar&apos;&gt; <br /> &lt;input type=&apos;submit&apos; name=&apos;eliminar&apos; value=&apos;Eliminar&apos;&gt;<br /> &lt;input type=&apos;hidden&apos; name=&apos;cod_prog&apos; value=&apos;$tupla[cod_prog]&apos;&gt;<br /> &lt;/form&gt;&lt;/td&gt;&lt;/tr&gt;&quot;;<br /> }<br /> echo&quot;&lt;/table&gt;&quot;;<br />}<br />else<br />{<br /> echo&quot;&lt;h2&gt;No se han encontrado registros almacenados&lt;br&gt;en esta categor&iacute;a&lt;/h2&gt;&quot;;<br />}<br />?&gt;<br />&lt;br&gt;<br />&lt;form id=&quot;form1&quot; name=&quot;form1&quot; method=&quot;post&quot; action=&quot;incluir.html&quot;&gt;<br /> &lt;input name=&quot;nuevo&quot; type=&quot;submit&quot; id=&quot;nuevo&quot; value=&quot;Nuevo&quot; /&gt;<br />&lt;/form&gt;<br />&lt;/body&gt;&lt;/html&gt;<br />mostrar.php<br />Si la consulta devuelve registros se dibuja una tabla con los títulos de cada campo en las columnas. El bucle While permitirá que mysql_fetch_array asigne al arreglo $tupla los valores fila a fila en cada repetición.<br />
  25. 25. &lt;html&gt;<br />&lt;head&gt;&lt;title&gt;&lt;/title&gt;&lt;/head&gt;<br />&lt;body&gt;<br />&lt;h2 align=&quot;center&quot;&gt;Programas Registrados&lt;/h2&gt;&lt;br&gt;<br />&lt;?php<br />include&quot;../conexion.php&quot;; <br />$query=&quot;SELECT * FROM programa ORDER BY cod_prog ASC&quot;;<br />$solicitud=mysql_query($query);<br />mysql_close($conexion); <br />$num_total_registros = mysql_num_rows($solicitud);<br />if($num_total_registros!=0)<br />{<br /> echo&quot;&lt;tablealign=&apos;center&apos; border=&apos;1&apos;&gt;<br /> &lt;tr&gt;&lt;td&gt;C&oacute;digo&lt;/td&gt; &lt;td&gt;Nombre&lt;/td&gt;&lt;td&gt;&Aacute;rea&lt;/td&gt;&lt;td&gt;Opciones&lt;/td&gt;&lt;/tr&gt;&quot;;<br />while ($tupla= mysql_fetch_array($solicitud)) <br /> {<br /> echo&quot;&lt;tr&gt;<br /> &lt;td&gt;$tupla[cod_prog]&lt;/td&gt;&lt;td&gt;$tupla[nom_prog]&lt;/td&gt;&lt;td&gt;$tupla[area]&lt;/td&gt;<br /> &lt;td&gt;&lt;formname=&apos;form_mostrar&apos; method=&apos;post&apos; action=&apos;editar_borrar.php&apos;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;editar&apos; value=&apos;Editar&apos;&gt; <br /> &lt;input type=&apos;submit&apos; name=&apos;eliminar&apos; value=&apos;Eliminar&apos;&gt;<br /> &lt;input type=&apos;hidden&apos; name=&apos;cod_prog&apos; value=&apos;$tupla[cod_prog]&apos;&gt;<br /> &lt;/form&gt;&lt;/td&gt;&lt;/tr&gt;&quot;;<br />}<br /> echo&quot;&lt;/table&gt;&quot;;<br />}<br />else<br />{<br /> echo&quot;&lt;h2&gt;No se han encontrado registros almacenados&lt;br&gt;en esta categor&iacute;a&lt;/h2&gt;&quot;;<br />}<br />?&gt;<br />&lt;br&gt;<br />&lt;form id=&quot;form1&quot; name=&quot;form1&quot; method=&quot;post&quot; action=&quot;incluir.html&quot;&gt;<br /> &lt;input name=&quot;nuevo&quot; type=&quot;submit&quot; id=&quot;nuevo&quot; value=&quot;Nuevo&quot; /&gt;<br />&lt;/form&gt;<br />&lt;/body&gt;&lt;/html&gt;<br />mostrar.php<br />En cada repetición del bucle se imprime en pantalla una fila de la tabla con la información obtenida. $tupla usa el nombre del campo como índice. Por cada fila se genera un formulario con las opciones de editar y eliminar, dejando un campo oculto (hidden) con la primaria del registro<br />
  26. 26. &lt;html&gt;<br />&lt;head&gt;&lt;title&gt;&lt;/title&gt;&lt;/head&gt;<br />&lt;body&gt;<br />&lt;h2 align=&quot;center&quot;&gt;Programas Registrados&lt;/h2&gt;&lt;br&gt;<br />&lt;?php<br />include&quot;../conexion.php&quot;; <br />$query=&quot;SELECT * FROM programa ORDER BY cod_prog ASC&quot;;<br />$solicitud=mysql_query($query);<br />mysql_close($conexion); <br />$num_total_registros = mysql_num_rows($solicitud);<br />if($num_total_registros!=0)<br />{<br /> echo&quot;&lt;tablealign=&apos;center&apos; border=&apos;1&apos;&gt;<br /> &lt;tr&gt;&lt;td&gt;C&oacute;digo&lt;/td&gt; &lt;td&gt;Nombre&lt;/td&gt;&lt;td&gt;&Aacute;rea&lt;/td&gt;&lt;td&gt;Opciones&lt;/td&gt;&lt;/tr&gt;&quot;;<br />while ($tupla= mysql_fetch_array($solicitud)) <br /> {<br /> echo&quot;&lt;tr&gt;<br /> &lt;td&gt;$tupla[cod_prog]&lt;/td&gt;&lt;td&gt;$tupla[nom_prog]&lt;/td&gt;&lt;td&gt;$tupla[area]&lt;/td&gt;<br /> &lt;td&gt;&lt;formname=&apos;form_mostrar&apos; method=&apos;post&apos; action=&apos;editar_borrar.php&apos;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;editar&apos; value=&apos;Editar&apos;&gt; <br /> &lt;input type=&apos;submit&apos; name=&apos;eliminar&apos; value=&apos;Eliminar&apos;&gt;<br /> &lt;input type=&apos;hidden&apos; name=&apos;cod_prog&apos; value=&apos;$tupla[cod_prog]&apos;&gt;<br /> &lt;/form&gt;&lt;/td&gt;&lt;/tr&gt;&quot;;<br /> }<br /> echo&quot;&lt;/table&gt;&quot;;<br />}<br />else<br />{<br /> echo&quot;&lt;h2&gt;No se han encontrado registros almacenados&lt;br&gt;en esta categor&iacute;a&lt;/h2&gt;&quot;;<br />}<br />?&gt;<br />&lt;br&gt;<br />&lt;form id=&quot;form1&quot; name=&quot;form1&quot; method=&quot;post&quot; action=&quot;incluir.html&quot;&gt;<br /> &lt;input name=&quot;nuevo&quot; type=&quot;submit&quot; id=&quot;nuevo&quot; value=&quot;Nuevo&quot; /&gt;<br />&lt;/form&gt;<br />&lt;/body&gt;&lt;/html&gt;<br />mostrar.php<br />Adicionalmente se puede agregar un botón extra para regresar de forma automática al formulario incluir.<br />
  27. 27. &lt;html&gt;<br />&lt;head&gt;&lt;title&gt;&lt;/title&gt;&lt;/head&gt;<br />&lt;body&gt;<br />&lt;h2 align=&quot;center&quot;&gt;Programas Registrados&lt;/h2&gt;&lt;br&gt;<br />&lt;?php<br />include&quot;../conexion.php&quot;; <br />$query=&quot;SELECT * FROM programa ORDER BY cod_prog ASC&quot;;<br />$solicitud=mysql_query($query);<br />mysql_close($conexion); <br />$num_total_registros = mysql_num_rows($solicitud);<br />if($num_total_registros!=0)<br />{<br /> echo&quot;&lt;tablealign=&apos;center&apos; border=&apos;1&apos;&gt;<br /> &lt;tr&gt;&lt;td&gt;C&oacute;digo&lt;/td&gt; &lt;td&gt;Nombre&lt;/td&gt;&lt;td&gt;&Aacute;rea&lt;/td&gt;&lt;td&gt;Opciones&lt;/td&gt;&lt;/tr&gt;&quot;;<br />while ($tupla= mysql_fetch_array($solicitud)) <br /> {<br /> echo&quot;&lt;tr&gt;<br /> &lt;td&gt;$tupla[cod_prog]&lt;/td&gt;&lt;td&gt;$tupla[nom_prog]&lt;/td&gt;&lt;td&gt;$tupla[area]&lt;/td&gt;<br /> &lt;td&gt;&lt;formname=&apos;form_mostrar&apos; method=&apos;post&apos; action=&apos;editar_borrar.php&apos;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;editar&apos; value=&apos;Editar&apos;&gt; <br /> &lt;input type=&apos;submit&apos; name=&apos;eliminar&apos; value=&apos;Eliminar&apos;&gt;<br /> &lt;input type=&apos;hidden&apos; name=&apos;cod_prog&apos; value=&apos;$tupla[cod_prog]&apos;&gt;<br /> &lt;/form&gt;&lt;/td&gt;&lt;/tr&gt;&quot;;<br /> }<br /> echo&quot;&lt;/table&gt;&quot;;<br />}<br />else<br />{<br /> echo&quot;&lt;h2&gt;No se han encontrado registros almacenados&lt;br&gt;en esta categor&iacute;a&lt;/h2&gt;&quot;;<br />}<br />?&gt;<br />&lt;br&gt;<br />&lt;form id=&quot;form1&quot; name=&quot;form1&quot; method=&quot;post&quot; action=&quot;incluir.html&quot;&gt;<br /> &lt;input name=&quot;nuevo&quot; type=&quot;submit&quot; id=&quot;nuevo&quot; value=&quot;Nuevo&quot; /&gt;<br />&lt;/form&gt;<br />&lt;/body&gt;&lt;/html&gt;<br />mostrar.php<br />Este es el código completo del archivo mostrar.php<br />
  28. 28. Ejemplo de editar_borrar.php<br />Sección eliminar<br />
  29. 29. &lt;html&gt;&lt;head&gt;&lt;title&gt;&lt;/title&gt;&lt;/head&gt;&lt;body&gt;<br />&lt;?php<br />if($_POST[editar])<br />{<br />include&quot;../conexion.php&quot;; <br /> $query=&quot;SELECT * FROM programa WHERE cod_prog=&apos;$_POST[cod_prog]&apos;&quot;;<br /> $solicitud=mysql_query($query);<br />mysql_close($conexion); <br /> $tupla= mysql_fetch_array($solicitud); <br /> ?&gt;<br /> &lt;h2 align=&apos;center&apos;&gt;Registro a Editar&lt;/h2&gt;<br /> &lt;formname=&quot;form1&quot; method=&quot;post&quot; action=&quot;acciones.php&quot;&gt;<br />C&oacute;digo: &lt;input type=&quot;text&quot; name=&quot;cod_prog&quot; value=&quot;&lt;?PHP echo&quot;$tupla[cod_prog]&quot; ?&gt;&quot;&gt;&lt;br&gt;<br /> Nombre: &lt;input type=&quot;text&quot; name=&quot;nom_prog&quot; size=&quot;30&quot; value=&quot;&lt;?PHP echo&quot;$tupla[nom_prog]&quot; ?&gt;&quot;&gt;&lt;br&gt;<br /> &Aacute;rea: &lt;input name=&quot;area&quot; type=&quot;text&quot; id=&quot;area&quot; size=&quot;30&quot; value=&quot;&lt;?PHP echo&quot;$tupla[area]&quot; ?&gt;&quot;&gt;<br /> &lt;br&gt;&lt;br&gt;<br /> &lt;input type=&apos;hidden&apos; name=&apos;cod_prog2&apos; value=&lt;?PHP echo&quot;$tupla[cod_prog]&quot;; ?&gt;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;actualizar&apos; value=&apos;Actualizar?&apos;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;cancelar&apos; value=&apos;Cancelar&apos;&gt;<br /> &lt;/form&gt;<br /> &lt;?PHP<br />}<br />if($_POST[eliminar])<br />{<br /> echo&quot;&lt;h2 align=&apos;center&apos;&gt;Registro a Eliminar&lt;/h2&gt;&quot;;<br /> echo&quot;&lt;formname=&apos;form_eliminar&apos; method=&apos;post&apos; action=&apos;acciones.php&apos;&gt;&quot;;<br />include&quot;../conexion.php&quot;; <br /> $query=&quot;SELECT * FROM programa WHERE cod_prog=&apos;$_POST[cod_prog]&apos;&quot;;<br /> $solicitud=mysql_query($query);<br />mysql_close($conexion); <br /> $tupla= mysql_fetch_array($solicitud);<br /> echo&quot;<br />Codigo de Programa: $tupla[cod_prog] &lt;br&gt;<br /> Nombre de Programa: $tupla[nom_prog] &lt;br&gt;<br />Area del Programa: $tupla[area] &lt;br&gt;<br /> &lt;input type=&apos;hidden&apos; name=&apos;cod_prog&apos; value=&apos;$tupla[cod_prog]&apos;&gt;<br /> &lt;br&gt;&quot;;<br /> echo&quot;<br /> &lt;input type=&apos;submit&apos; name=&apos;eliminar&apos; value=&apos;Eliminar?&apos;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;cancelar&apos; value=&apos;Cancelar&apos;&gt;<br /> &lt;/form&gt;&quot;;<br />}<br />?&gt;<br />&lt;/body&gt;&lt;/html&gt;<br />editar_borrar.php<br />Se genera la estructura básica de un documento HTML<br />
  30. 30. &lt;html&gt;&lt;head&gt;&lt;title&gt;&lt;/title&gt;&lt;/head&gt;&lt;body&gt;<br />&lt;?php<br />if($_POST[editar])<br />{<br />include&quot;../conexion.php&quot;; <br /> $query=&quot;SELECT * FROM programa WHERE cod_prog=&apos;$_POST[cod_prog]&apos;&quot;;<br /> $solicitud=mysql_query($query);<br />mysql_close($conexion); <br /> $tupla= mysql_fetch_array($solicitud); <br /> ?&gt;<br /> &lt;h2 align=&apos;center&apos;&gt;Registro a Editar&lt;/h2&gt;<br /> &lt;formname=&quot;form1&quot; method=&quot;post&quot; action=&quot;acciones.php&quot;&gt;<br />C&oacute;digo: &lt;input type=&quot;text&quot; name=&quot;cod_prog&quot; value=&quot;&lt;?PHP echo&quot;$tupla[cod_prog]&quot; ?&gt;&quot;&gt;&lt;br&gt;<br /> Nombre: &lt;input type=&quot;text&quot; name=&quot;nom_prog&quot; size=&quot;30&quot; value=&quot;&lt;?PHP echo&quot;$tupla[nom_prog]&quot; ?&gt;&quot;&gt;&lt;br&gt;<br /> &Aacute;rea: &lt;input name=&quot;area&quot; type=&quot;text&quot; id=&quot;area&quot; size=&quot;30&quot; value=&quot;&lt;?PHP echo&quot;$tupla[area]&quot; ?&gt;&quot;&gt;<br /> &lt;br&gt;&lt;br&gt;<br /> &lt;input type=&apos;hidden&apos; name=&apos;cod_prog2&apos; value=&lt;?PHP echo&quot;$tupla[cod_prog]&quot;; ?&gt;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;actualizar&apos; value=&apos;Actualizar?&apos;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;cancelar&apos; value=&apos;Cancelar&apos;&gt;<br /> &lt;/form&gt;<br /> &lt;?PHP<br />}<br />if($_POST[eliminar])<br />{<br /> echo&quot;&lt;h2 align=&apos;center&apos;&gt;Registro a Eliminar&lt;/h2&gt;&quot;;<br /> echo&quot;&lt;formname=&apos;form_eliminar&apos; method=&apos;post&apos; action=&apos;acciones.php&apos;&gt;&quot;;<br />include&quot;../conexion.php&quot;; <br /> $query=&quot;SELECT * FROM programa WHERE cod_prog=&apos;$_POST[cod_prog]&apos;&quot;;<br /> $solicitud=mysql_query($query);<br />mysql_close($conexion); <br /> $tupla= mysql_fetch_array($solicitud);<br />echo&quot;<br />Codigo de Programa: $tupla[cod_prog] &lt;br&gt;<br /> Nombre de Programa: $tupla[nom_prog] &lt;br&gt;<br />Area del Programa: $tupla[area] &lt;br&gt;<br /> &lt;input type=&apos;hidden&apos; name=&apos;cod_prog&apos; value=&apos;$tupla[cod_prog]&apos;&gt;<br /> &lt;br&gt;&quot;;<br /> echo&quot;<br /> &lt;input type=&apos;submit&apos; name=&apos;eliminar&apos; value=&apos;Eliminar?&apos;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;cancelar&apos; value=&apos;Cancelar&apos;&gt;<br /> &lt;/form&gt;&quot;;<br />}<br />?&gt;<br />&lt;/body&gt;&lt;/html&gt;<br />editar_borrar.php<br />Esta vez la consulta se realiza a partir del código primario enviado desde el archivo mostrar. El resto de los pasos son similares.<br />
  31. 31. &lt;html&gt;&lt;head&gt;&lt;title&gt;&lt;/title&gt;&lt;/head&gt;&lt;body&gt;<br />&lt;?php<br />if($_POST[editar])<br />{<br />include&quot;../conexion.php&quot;; <br /> $query=&quot;SELECT * FROM programa WHERE cod_prog=&apos;$_POST[cod_prog]&apos;&quot;;<br /> $solicitud=mysql_query($query);<br />mysql_close($conexion); <br /> $tupla= mysql_fetch_array($solicitud); <br /> ?&gt;<br /> &lt;h2 align=&apos;center&apos;&gt;Registro a Editar&lt;/h2&gt;<br /> &lt;formname=&quot;form1&quot; method=&quot;post&quot; action=&quot;acciones.php&quot;&gt;<br />C&oacute;digo: &lt;input type=&quot;text&quot; name=&quot;cod_prog&quot; value=&quot;&lt;?PHP echo&quot;$tupla[cod_prog]&quot; ?&gt;&quot;&gt;&lt;br&gt;<br /> Nombre: &lt;input type=&quot;text&quot; name=&quot;nom_prog&quot; size=&quot;30&quot; value=&quot;&lt;?PHP echo&quot;$tupla[nom_prog]&quot; ?&gt;&quot;&gt;&lt;br&gt;<br /> &Aacute;rea: &lt;input name=&quot;area&quot; type=&quot;text&quot; id=&quot;area&quot; size=&quot;30&quot; value=&quot;&lt;?PHP echo&quot;$tupla[area]&quot; ?&gt;&quot;&gt;<br /> &lt;br&gt;&lt;br&gt;<br /> &lt;input type=&apos;hidden&apos; name=&apos;cod_prog2&apos; value=&lt;?PHP echo&quot;$tupla[cod_prog]&quot;; ?&gt;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;actualizar&apos; value=&apos;Actualizar?&apos;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;cancelar&apos; value=&apos;Cancelar&apos;&gt;<br /> &lt;/form&gt;<br /> &lt;?PHP<br />}<br />if($_POST[eliminar])<br />{<br />echo&quot;&lt;h2 align=&apos;center&apos;&gt;Registro a Eliminar&lt;/h2&gt;&quot;;<br /> echo&quot;&lt;formname=&apos;form_eliminar&apos; method=&apos;post&apos; action=&apos;acciones.php&apos;&gt;&quot;;<br />include&quot;../conexion.php&quot;; <br /> $query=&quot;SELECT * FROM programa WHERE cod_prog=&apos;$_POST[cod_prog]&apos;&quot;;<br /> $solicitud=mysql_query($query);<br />mysql_close($conexion); <br /> $tupla= mysql_fetch_array($solicitud);<br /> echo&quot;<br />Codigo de Programa: $tupla[cod_prog] &lt;br&gt;<br /> Nombre de Programa: $tupla[nom_prog] &lt;br&gt;<br />Area del Programa: $tupla[area] &lt;br&gt;<br /> &lt;input type=&apos;hidden&apos; name=&apos;cod_prog&apos; value=&apos;$tupla[cod_prog]&apos;&gt;<br /> &lt;br&gt;&quot;;<br /> echo&quot;<br /> &lt;input type=&apos;submit&apos; name=&apos;eliminar&apos; value=&apos;Eliminar?&apos;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;cancelar&apos; value=&apos;Cancelar&apos;&gt;<br /> &lt;/form&gt;&quot;;<br />}<br />?&gt;<br />&lt;/body&gt;&lt;/html&gt;<br />editar_borrar.php<br />Se imprime en pantalla los datos del registro a eliminar. No se necesita While pues es sólo una fila de la tabla. Los botones permitirán borrar el registro o cancelar la petición. El campo oculto nos indicará a quien debemos borrar con el DELETE de acciones.php<br />
  32. 32. &lt;html&gt;&lt;head&gt;&lt;title&gt;&lt;/title&gt;&lt;/head&gt;&lt;body&gt;<br />&lt;?php<br />if($_POST[editar])<br />{<br />include&quot;../conexion.php&quot;; <br /> $query=&quot;SELECT * FROM programa WHERE cod_prog=&apos;$_POST[cod_prog]&apos;&quot;;<br /> $solicitud=mysql_query($query);<br />mysql_close($conexion); <br /> $tupla= mysql_fetch_array($solicitud); <br /> ?&gt;<br /> &lt;h2 align=&apos;center&apos;&gt;Registro a Editar&lt;/h2&gt;<br /> &lt;formname=&quot;form1&quot; method=&quot;post&quot; action=&quot;acciones.php&quot;&gt;<br />C&oacute;digo: &lt;input type=&quot;text&quot; name=&quot;cod_prog&quot; value=&quot;&lt;?PHP echo&quot;$tupla[cod_prog]&quot; ?&gt;&quot;&gt;&lt;br&gt;<br /> Nombre: &lt;input type=&quot;text&quot; name=&quot;nom_prog&quot; size=&quot;30&quot; value=&quot;&lt;?PHP echo&quot;$tupla[nom_prog]&quot; ?&gt;&quot;&gt;&lt;br&gt;<br /> &Aacute;rea: &lt;input name=&quot;area&quot; type=&quot;text&quot; id=&quot;area&quot; size=&quot;30&quot; value=&quot;&lt;?PHP echo&quot;$tupla[area]&quot; ?&gt;&quot;&gt;<br /> &lt;br&gt;&lt;br&gt;<br /> &lt;input type=&apos;hidden&apos; name=&apos;cod_prog2&apos; value=&lt;?PHP echo&quot;$tupla[cod_prog]&quot;; ?&gt;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;actualizar&apos; value=&apos;Actualizar?&apos;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;cancelar&apos; value=&apos;Cancelar&apos;&gt;<br /> &lt;/form&gt;<br /> &lt;?PHP<br />}<br />if($_POST[eliminar])<br />{<br /> echo&quot;&lt;h2 align=&apos;center&apos;&gt;Registro a Eliminar&lt;/h2&gt;&quot;;<br /> echo&quot;&lt;formname=&apos;form_eliminar&apos; method=&apos;post&apos; action=&apos;acciones.php&apos;&gt;&quot;;<br />include&quot;../conexion.php&quot;; <br /> $query=&quot;SELECT * FROM programa WHERE cod_prog=&apos;$_POST[cod_prog]&apos;&quot;;<br /> $solicitud=mysql_query($query);<br />mysql_close($conexion); <br /> $tupla= mysql_fetch_array($solicitud);<br /> echo&quot;<br />Codigo de Programa: $tupla[cod_prog] &lt;br&gt;<br /> Nombre de Programa: $tupla[nom_prog] &lt;br&gt;<br />Area del Programa: $tupla[area] &lt;br&gt;<br /> &lt;input type=&apos;hidden&apos; name=&apos;cod_prog&apos; value=&apos;$tupla[cod_prog]&apos;&gt;<br /> &lt;br&gt;&quot;;<br /> echo&quot;<br /> &lt;input type=&apos;submit&apos; name=&apos;eliminar&apos; value=&apos;Eliminar?&apos;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;cancelar&apos; value=&apos;Cancelar&apos;&gt;<br /> &lt;/form&gt;&quot;;<br />}<br />?&gt;<br />&lt;/body&gt;&lt;/html&gt;<br />editar_borrar.php<br />Esta sección de código permite visualizar el registro que deseamos borrar antes de confirma dicha petición.<br />
  33. 33. Ejemplo de acciones.php<br />Para eliminar<br />
  34. 34. &lt;?PHP<br />if($_POST[grabar])<br />{<br />include&quot;../conexion.php&quot;;<br /> $query=&quot;INSERT INTO programa (cod_prog, nom_prog, area) VALUES (&apos;$_POST[cod_prog]&apos;, &apos;$_POST[nom_prog]&apos;, &apos;$_POST[area]&apos;)&quot;;<br />mysql_query($query); <br />mysql_close($conexion);<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Registro Incluido!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;incluir.html&apos;;&lt;/script&gt;&quot;;<br />}<br />if($_POST[eliminar])<br />{<br />include &quot;../conexion.php&quot;; <br /> $query=&quot;DELETE FROM programa WHERE cod_prog=&apos;$_POST[cod_prog]&apos;&quot;;<br />mysql_query($query);<br />mysql_close($conexion);<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Registro Eliminado!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;mostrar.php&apos;&lt;/script&gt;&quot;;<br />}<br />if($_POST[cancelar] || $_POST[mostrar])<br />{<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;mostrar.php&apos;&lt;/script&gt;&quot;;<br />}<br />if($_POST[actualizar])<br />{<br />include&quot;../conexion.php&quot;; <br /> $query=&quot;UPDATE programa SET cod_prog=&apos;$_POST[cod_prog]&apos;, nom_prog=&apos;$_POST[nom_prog]&apos;, area=&apos;$_POST[area]&apos; WHERE cod_prog=&apos;$_POST[cod_prog2]&apos;&quot;;<br />mysql_query($query);<br />mysql_close($conexion);<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Registro Modificado!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;mostrar.php&apos;&lt;/script&gt;&quot;;<br />}<br />?&gt;<br />acciones.php<br />En esta sección de código se ejecuta la sentencia en DML necesaria para borrar un registro usando el código enviado desde el archivo editar_borrar.php<br />
  35. 35. Ejemplo de editar_borrar.php<br />Sección Editar<br />
  36. 36. &lt;html&gt;&lt;head&gt;&lt;title&gt;&lt;/title&gt;&lt;/head&gt;&lt;body&gt;<br />&lt;?php<br />if($_POST[editar])<br />{<br />include&quot;../conexion.php&quot;; <br /> $query=&quot;SELECT * FROM programa WHERE cod_prog=&apos;$_POST[cod_prog]&apos;&quot;;<br /> $solicitud=mysql_query($query);<br />mysql_close($conexion); <br /> $tupla= mysql_fetch_array($solicitud); <br /> ?&gt;<br />&lt;h2 align=&apos;center&apos;&gt;Registro a Editar&lt;/h2&gt;<br />&lt;formname=&quot;form1&quot; method=&quot;post&quot; action=&quot;acciones.php&quot;&gt;<br />C&oacute;digo: &lt;input type=&quot;text&quot; name=&quot;cod_prog&quot; value=&quot;&lt;?PHP echo&quot;$tupla[cod_prog]&quot; ?&gt;&quot;&gt;&lt;br&gt;<br /> Nombre: &lt;input type=&quot;text&quot; name=&quot;nom_prog&quot; size=&quot;30&quot; value=&quot;&lt;?PHP echo&quot;$tupla[nom_prog]&quot; ?&gt;&quot;&gt;&lt;br&gt;<br /> &Aacute;rea: &lt;input name=&quot;area&quot; type=&quot;text&quot; id=&quot;area&quot; size=&quot;30&quot; value=&quot;&lt;?PHP echo&quot;$tupla[area]&quot; ?&gt;&quot;&gt;<br /> &lt;br&gt;&lt;br&gt;<br /> &lt;input type=&apos;hidden&apos; name=&apos;cod_prog2&apos; value=“&lt;?PHP echo&quot;$tupla[cod_prog]&quot;; ?&gt;”&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;actualizar&apos; value=&apos;Actualizar?&apos;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;cancelar&apos; value=&apos;Cancelar&apos;&gt;<br /> &lt;/form&gt;<br /> &lt;?PHP<br />}<br />if($_POST[eliminar])<br />{<br /> echo&quot;&lt;h2 align=&apos;center&apos;&gt;Registro a Eliminar&lt;/h2&gt;&quot;;<br /> echo&quot;&lt;formname=&apos;form_eliminar&apos; method=&apos;post&apos; action=&apos;acciones.php&apos;&gt;&quot;;<br />include&quot;../conexion.php&quot;; <br /> $query=&quot;SELECT * FROM programa WHERE cod_prog=&apos;$_POST[cod_prog]&apos;&quot;;<br /> $solicitud=mysql_query($query);<br />mysql_close($conexion); <br /> $tupla= mysql_fetch_array($solicitud);<br /> echo&quot;<br />Codigo de Programa: $tupla[cod_prog] &lt;br&gt;<br /> Nombre de Programa: $tupla[nom_prog] &lt;br&gt;<br />Area del Programa: $tupla[area] &lt;br&gt;<br /> &lt;input type=&apos;hidden&apos; name=&apos;cod_prog&apos; value=&apos;$tupla[cod_prog]&apos;&gt;<br /> &lt;br&gt;&quot;;<br /> echo&quot;<br /> &lt;input type=&apos;submit&apos; name=&apos;eliminar&apos; value=&apos;Eliminar?&apos;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;cancelar&apos; value=&apos;Cancelar&apos;&gt;<br /> &lt;/form&gt;&quot;;<br />}<br />?&gt;<br />&lt;/body&gt;&lt;/html&gt;<br />editar_borrar.php<br />Esta vez la consulta se realiza a partir del código primario enviado desde el archivo mostrar. El resto de los pasos son similares.<br />
  37. 37. &lt;html&gt;&lt;head&gt;&lt;title&gt;&lt;/title&gt;&lt;/head&gt;&lt;body&gt;<br />&lt;?php<br />if($_POST[editar])<br />{<br />include&quot;../conexion.php&quot;; <br /> $query=&quot;SELECT * FROM programa WHERE cod_prog=&apos;$_POST[cod_prog]&apos;&quot;;<br /> $solicitud=mysql_query($query);<br />mysql_close($conexion); <br /> $tupla= mysql_fetch_array($solicitud); <br /> ?&gt;<br /> &lt;h2 align=&apos;center&apos;&gt;Registro a Editar&lt;/h2&gt;<br /> &lt;formname=&quot;form1&quot; method=&quot;post&quot; action=&quot;acciones.php&quot;&gt;<br />C&oacute;digo: &lt;input type=&quot;text&quot; name=&quot;cod_prog&quot; value=&quot;&lt;?PHP echo&quot;$tupla[cod_prog]&quot; ?&gt;&quot;&gt;&lt;br&gt;<br /> Nombre: &lt;input type=&quot;text&quot; name=&quot;nom_prog&quot; size=&quot;30&quot; value=&quot;&lt;?PHP echo&quot;$tupla[nom_prog]&quot; ?&gt;&quot;&gt;&lt;br&gt;<br /> &Aacute;rea: &lt;input name=&quot;area&quot; type=&quot;text&quot; id=&quot;area&quot; size=&quot;30&quot; value=&quot;&lt;?PHP echo&quot;$tupla[area]&quot; ?&gt;&quot;&gt;<br /> &lt;br&gt;&lt;br&gt;<br /> &lt;input type=&apos;hidden&apos; name=&apos;cod_prog2&apos; value=“&lt;?PHP echo&quot;$tupla[cod_prog]&quot;; ?&gt;”&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;actualizar&apos; value=&apos;Actualizar?&apos;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;cancelar&apos; value=&apos;Cancelar&apos;&gt;<br /> &lt;/form&gt;<br />&lt;?PHP<br />}<br />if($_POST[eliminar])<br />{<br /> echo&quot;&lt;h2 align=&apos;center&apos;&gt;Registro a Eliminar&lt;/h2&gt;&quot;;<br /> echo&quot;&lt;formname=&apos;form_eliminar&apos; method=&apos;post&apos; action=&apos;acciones.php&apos;&gt;&quot;;<br />include&quot;../conexion.php&quot;; <br /> $query=&quot;SELECT * FROM programa WHERE cod_prog=&apos;$_POST[cod_prog]&apos;&quot;;<br /> $solicitud=mysql_query($query);<br />mysql_close($conexion); <br /> $tupla= mysql_fetch_array($solicitud);<br /> echo&quot;<br />Codigo de Programa: $tupla[cod_prog] &lt;br&gt;<br /> Nombre de Programa: $tupla[nom_prog] &lt;br&gt;<br />Area del Programa: $tupla[area] &lt;br&gt;<br /> &lt;input type=&apos;hidden&apos; name=&apos;cod_prog&apos; value=&apos;$tupla[cod_prog]&apos;&gt;<br /> &lt;br&gt;&quot;;<br /> echo&quot;<br /> &lt;input type=&apos;submit&apos; name=&apos;eliminar&apos; value=&apos;Eliminar?&apos;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;cancelar&apos; value=&apos;Cancelar&apos;&gt;<br /> &lt;/form&gt;&quot;;<br />}<br />?&gt;<br />&lt;/body&gt;&lt;/html&gt;<br />editar_borrar.php<br />Se imprime en pantalla el mismo código que usamos en el formulario incluir, pero cambian los botones por “actualizar “ y “cancelar”. Además se usa un campo oculto con la llave primaria para saber que registro será modificado<br />
  38. 38. &lt;html&gt;&lt;head&gt;&lt;title&gt;&lt;/title&gt;&lt;/head&gt;&lt;body&gt;<br />&lt;?php<br />if($_POST[editar])<br />{<br />include&quot;../conexion.php&quot;; <br /> $query=&quot;SELECT * FROM programa WHERE cod_prog=&apos;$_POST[cod_prog]&apos;&quot;;<br /> $solicitud=mysql_query($query);<br />mysql_close($conexion); <br /> $tupla= mysql_fetch_array($solicitud); <br /> ?&gt;<br /> &lt;h2 align=&apos;center&apos;&gt;Registro a Editar&lt;/h2&gt;<br /> &lt;formname=&quot;form1&quot; method=&quot;post&quot; action=&quot;acciones.php&quot;&gt;<br />C&oacute;digo: &lt;input type=&quot;text&quot; name=&quot;cod_prog&quot; value=&quot;&lt;?PHP echo&quot;$tupla[cod_prog]&quot; ?&gt;&quot;&gt;&lt;br&gt;<br /> Nombre: &lt;input type=&quot;text&quot; name=&quot;nom_prog&quot; size=&quot;30&quot; value=&quot;&lt;?PHP echo&quot;$tupla[nom_prog]&quot; ?&gt;&quot;&gt;&lt;br&gt;<br /> &Aacute;rea: &lt;input name=&quot;area&quot; type=&quot;text&quot; id=&quot;area&quot; size=&quot;30&quot; value=&quot;&lt;?PHP echo&quot;$tupla[area]&quot; ?&gt;&quot;&gt;<br /> &lt;br&gt;&lt;br&gt;<br /> &lt;input type=&apos;hidden&apos; name=&apos;cod_prog2&apos; value=“&lt;?PHP echo&quot;$tupla[cod_prog]&quot;; ?&gt;”&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;actualizar&apos; value=&apos;Actualizar?&apos;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;cancelar&apos; value=&apos;Cancelar&apos;&gt;<br /> &lt;/form&gt;<br /> &lt;?PHP<br />}<br />if($_POST[eliminar])<br />{<br /> echo&quot;&lt;h2 align=&apos;center&apos;&gt;Registro a Eliminar&lt;/h2&gt;&quot;;<br /> echo&quot;&lt;formname=&apos;form_eliminar&apos; method=&apos;post&apos; action=&apos;acciones.php&apos;&gt;&quot;;<br />include&quot;../conexion.php&quot;; <br /> $query=&quot;SELECT * FROM programa WHERE cod_prog=&apos;$_POST[cod_prog]&apos;&quot;;<br /> $solicitud=mysql_query($query);<br />mysql_close($conexion); <br /> $tupla= mysql_fetch_array($solicitud);<br /> echo&quot;<br />Codigo de Programa: $tupla[cod_prog] &lt;br&gt;<br /> Nombre de Programa: $tupla[nom_prog] &lt;br&gt;<br />Area del Programa: $tupla[area] &lt;br&gt;<br /> &lt;input type=&apos;hidden&apos; name=&apos;cod_prog&apos; value=&apos;$tupla[cod_prog]&apos;&gt;<br /> &lt;br&gt;&quot;;<br /> echo&quot;<br /> &lt;input type=&apos;submit&apos; name=&apos;eliminar&apos; value=&apos;Eliminar?&apos;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;cancelar&apos; value=&apos;Cancelar&apos;&gt;<br /> &lt;/form&gt;&quot;;<br />}<br />?&gt;<br />&lt;/body&gt;&lt;/html&gt;<br />editar_borrar.php<br />En el atributo value de los campos se inserta código PHP que facilite la visualización de los valores almacenados para ese registro.<br />
  39. 39. &lt;html&gt;&lt;head&gt;&lt;title&gt;&lt;/title&gt;&lt;/head&gt;&lt;body&gt;<br />&lt;?php<br />if($_POST[editar])<br />{<br />include&quot;../conexion.php&quot;; <br /> $query=&quot;SELECT * FROM programa WHERE cod_prog=&apos;$_POST[cod_prog]&apos;&quot;;<br /> $solicitud=mysql_query($query);<br />mysql_close($conexion); <br /> $tupla= mysql_fetch_array($solicitud); <br /> ?&gt;<br /> &lt;h2 align=&apos;center&apos;&gt;Registro a Editar&lt;/h2&gt;<br /> &lt;formname=&quot;form1&quot; method=&quot;post&quot; action=&quot;acciones.php&quot;&gt;<br />C&oacute;digo: &lt;input type=&quot;text&quot; name=&quot;cod_prog&quot; value=&quot;&lt;?PHP echo&quot;$tupla[cod_prog]&quot; ?&gt;&quot;&gt;&lt;br&gt;<br /> Nombre: &lt;input type=&quot;text&quot; name=&quot;nom_prog&quot; size=&quot;30&quot; value=&quot;&lt;?PHP echo&quot;$tupla[nom_prog]&quot; ?&gt;&quot;&gt;&lt;br&gt;<br /> &Aacute;rea: &lt;input name=&quot;area&quot; type=&quot;text&quot; id=&quot;area&quot; size=&quot;30&quot; value=&quot;&lt;?PHP echo&quot;$tupla[area]&quot; ?&gt;&quot;&gt;<br /> &lt;br&gt;&lt;br&gt;<br /> &lt;input type=&apos;hidden&apos; name=&apos;cod_prog2&apos; value=“&lt;?PHP echo&quot;$tupla[cod_prog]&quot;; ?&gt;”&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;actualizar&apos; value=&apos;Actualizar?&apos;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;cancelar&apos; value=&apos;Cancelar&apos;&gt;<br /> &lt;/form&gt;<br /> &lt;?PHP<br />}<br />if($_POST[eliminar])<br />{<br /> echo&quot;&lt;h2 align=&apos;center&apos;&gt;Registro a Eliminar&lt;/h2&gt;&quot;;<br /> echo&quot;&lt;formname=&apos;form_eliminar&apos; method=&apos;post&apos; action=&apos;acciones.php&apos;&gt;&quot;;<br />include&quot;../conexion.php&quot;; <br /> $query=&quot;SELECT * FROM programa WHERE cod_prog=&apos;$_POST[cod_prog]&apos;&quot;;<br /> $solicitud=mysql_query($query);<br />mysql_close($conexion); <br /> $tupla= mysql_fetch_array($solicitud);<br /> echo&quot;<br />Codigo de Programa: $tupla[cod_prog] &lt;br&gt;<br /> Nombre de Programa: $tupla[nom_prog] &lt;br&gt;<br />Area del Programa: $tupla[area] &lt;br&gt;<br /> &lt;input type=&apos;hidden&apos; name=&apos;cod_prog&apos; value=&apos;$tupla[cod_prog]&apos;&gt;<br /> &lt;br&gt;&quot;;<br /> echo&quot;<br /> &lt;input type=&apos;submit&apos; name=&apos;eliminar&apos; value=&apos;Eliminar?&apos;&gt;<br /> &lt;input type=&apos;submit&apos; name=&apos;cancelar&apos; value=&apos;Cancelar&apos;&gt;<br /> &lt;/form&gt;&quot;;<br />}<br />?&gt;<br />&lt;/body&gt;&lt;/html&gt;<br />editar_borrar.php<br />Esta sección de código permite visualizar el registro que deseamos actualizar antes de confirma dicha petición.<br />
  40. 40. Ejemplo de acciones.php<br />Para actualizar<br />
  41. 41. &lt;?PHP<br />if($_POST[grabar])<br />{<br />include&quot;../conexion.php&quot;;<br /> $query=&quot;INSERT INTO programa (cod_prog, nom_prog, area) VALUES (&apos;$_POST[cod_prog]&apos;, &apos;$_POST[nom_prog]&apos;, &apos;$_POST[area]&apos;)&quot;;<br />mysql_query($query); <br />mysql_close($conexion);<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Registro Incluido!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;incluir.html&apos;;&lt;/script&gt;&quot;;<br />}<br />if($_POST[eliminar])<br />{<br />include &quot;../conexion.php&quot;; <br /> $query=&quot;DELETE FROM programa WHERE cod_prog=&apos;$_POST[cod_prog]&apos;&quot;;<br />mysql_query($query);<br />mysql_close($conexion);<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Registro Eliminado!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;mostrar.php&apos;&lt;/script&gt;&quot;;<br />}<br />if($_POST[cancelar] || $_POST[mostrar])<br />{<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;mostrar.php&apos;&lt;/script&gt;&quot;;<br />}<br />if($_POST[actualizar])<br />{<br />include&quot;../conexion.php&quot;; <br /> $query=&quot;UPDATE programa SET cod_prog=&apos;$_POST[cod_prog]&apos;, nom_prog=&apos;$_POST[nom_prog]&apos;, area=&apos;$_POST[area]&apos; WHERE cod_prog=&apos;$_POST[cod_prog2]&apos;&quot;;<br />mysql_query($query);<br />mysql_close($conexion);<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Registro Modificado!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;mostrar.php&apos;&lt;/script&gt;&quot;;<br />}<br />?&gt;<br />acciones.php<br />En esta sección de código se ejecuta la sentencia en DML necesaria para actualizar un registro usando el código enviado desde el archivo editar_borrar.php<br />
  42. 42. Ejemplo de acciones.php<br />Para cancelar o mostrar<br />
  43. 43. &lt;?PHP<br />if($_POST[grabar])<br />{<br />include&quot;../conexion.php&quot;;<br /> $query=&quot;INSERT INTO programa (cod_prog, nom_prog, area) VALUES (&apos;$_POST[cod_prog]&apos;, &apos;$_POST[nom_prog]&apos;, &apos;$_POST[area]&apos;)&quot;;<br />mysql_query($query); <br />mysql_close($conexion);<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Registro Incluido!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;incluir.html&apos;;&lt;/script&gt;&quot;;<br />}<br />if($_POST[eliminar])<br />{<br />include &quot;../conexion.php&quot;; <br /> $query=&quot;DELETE FROM programa WHERE cod_prog=&apos;$_POST[cod_prog]&apos;&quot;;<br />mysql_query($query);<br />mysql_close($conexion);<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Registro Eliminado!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;mostrar.php&apos;&lt;/script&gt;&quot;;<br />}<br />if($_POST[cancelar] || $_POST[mostrar])<br />{<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;mostrar.php&apos;&lt;/script&gt;&quot;;<br />}<br />if($_POST[actualizar])<br />{<br />include&quot;../conexion.php&quot;; <br /> $query=&quot;UPDATE programa SET cod_prog=&apos;$_POST[cod_prog]&apos;, nom_prog=&apos;$_POST[nom_prog]&apos;, area=&apos;$_POST[area]&apos; WHERE cod_prog=&apos;$_POST[cod_prog2]&apos;&quot;;<br />mysql_query($query);<br />mysql_close($conexion);<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;alert(&apos;Registro Modificado!&apos;)&lt;/script&gt;&quot;;<br /> echo&quot;&lt;script language=&apos;JavaScript&apos;&gt;window.self.location=&apos;mostrar.php&apos;&lt;/script&gt;&quot;;<br />}<br />?&gt;<br />acciones.php<br />En esta sección de código se carga en pantalla el archivo mostrar.php en caso que se presione cancelar durante la edición o eliminación, o que se active el botón mostrar desde el formulario incluir.<br />

×