Академический Документы
Профессиональный Документы
Культура Документы
Ingeniera de Sistemas
Cuando nos referimos a que PHP estn incrustados en los documentos HTML se
afirma que PHP puede escribirse dentro de un documento HTML como se muestra en
el siguiente extracto de cdigo:
Ejemplo.php
<HTML><HEAD><TITLE>Pgina web 1 PHP </TITLE></HEAD>
<BODY>
<?php
echo "Este es mi primer ejemplo en PHP ...";
// Este es un comentario. Podemos incluir mas sentencias PHP en esta seccin
?>
</BODY></HTML>
Este ejemplo solo escribe Este es mi primer ejemplo en PHP en el browser. Note
que los delimitadores para escribir PHP son (<?php) como delimitador de inicio de las
instrucciones PHP y (?>) como delimitador de fin de instrucciones. Las instrucciones
en PHP terminan con un ; (punto y coma).
Importante !! . Cuando una pagina HTML incluye sentencias PHP la extensin de la
pgina cambia de HTML a PHP.
docenteunt@yahoo.es
Lo mismo sucede con el nombre del alumno. Esto permite una validacin el lado del
Cliente. El modelo de base de datos es el siguiente:
docenteunt@yahoo.es
</tr>
<tr>
<td>Carrera</td>
<td><select name="cbocarrera">
<option value="1">SISTEMAS</option>
<option value="2">CONTABILIDAD</option>
<option value="3">ADMINISTRACION</option>
<option value="4">EDUCACION</option>
<option value="5">SICOLOGIA</option>
</select>
</td>
</tr>
<tr>
<td>Sexo</td>
<td><input name="rbsexo" type="radio" value="H" checked="checked" />Hombre
<input name="rbsexo" type="radio" value="M" />Mujer</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="button" onClick="validar(this.form)"
value="Grabar Registro ">
</td>
</tr>
</table>
</form></body></html>
Se puede observar que la pgina contiene secuencias de validacin utilizando
JavaScript. Este cdigo se utiliza para validar las entradas en el formulario. Ahora
escribimos la pgina PHP que interacta con la base de datos MySQL
registraalumno.php
<?php
$con=mysql_connect("127.0.0.1","root","") or die(mysql_error());
mysql_select_db("Universidad") or die(mysql_error());
$res=mysql_query("insert alumnos
values('".$_POST['txtcodigo']."','".$_POST['txtnombres']."','".$_POST['cbocarrera']."','"
.$_POST['rbsexo']."')",$con) or die (mysql_error());
mysql_close($con);
echo '<p align="center"><b> Registro Grabado ! </b></p>';
echo '<hr>'; // Comentario: Imprime una linea horizontal
echo 'Codigo :'.$_POST['txtcodigo'].'<BR>Nombres
:'.$_POST['txtnombres'].'<BR>Carrera :';
switch ($_POST['cbocarrera'])
{
case 1:
echo 'SISTEMAS';
break;
case 2:
echo 'CONTABILIDAD';
break;
docenteunt@yahoo.es
docenteunt@yahoo.es
docenteunt@yahoo.es
consultaalumno.html
<html><head><title>Consulta de Alumnos</title></head><body bgcolor="#99CCFF">
<form method="post" action="consultaalumno.php">
<h3 align="center">Consulta de Alumnos</h3>
<table width="250" border="1" align="center">
<tr>
<td>Carrera</td>
<td><select name="cbocarrera" id="carrera">
<option value="0">- - - TODAS - - -</option>
<option value="1">SISTEMAS</option>
<option value="2">CONTABILIDAD</option>
<option value="3">ADMINISTRACION</option>
<option value="4">EDUCACION</option>
<option value="5">SICOLOGIA</option>
</select>
</td> </tr>
<td colspan="2" align="center"><input type="submit" value="Consultar >>">
</td> </tr>
</table>
</form></body></html>
consultaalumno.php
<?php
$con=mysql_connect("127.0.0.1","root","") or die(mysql_error());
mysql_select_db("Universidad") or die(mysql_error());
if($_POST['cbocarrera']==0)
$consulta="select a.idalumno,a.nombres,c.carrera from alumnos a inner join carreras
c on a.idcarrera=c.idcarrera";
else
$consulta="select a.idalumno,a.nombres,c.carrera from alumnos a inner join carreras
c on a.idcarrera=c.idcarrera where c.idcarrera='".$_POST['cbocarrera']."'";
$res=mysql_query($consulta,$con) or die ('Fall la Operacion de Consulta');
echo '<h2><p align="center">Reporte de Alumnos por Carrera</p></h2>';
echo '<table align="center" border="1"><tr>';
echo '<td>Codigo</td><td>Nombres</td><td>Carrera</td></tr>';
while($row=mysql_fetch_row($res))
{
echo '<tr>';
echo '<td>'.$row[0].'</td>';
echo '<td>'.$row[1].'</td>';
echo '<td>'.$row[2].'</td>';
echo '</tr>';
}
echo '</table>';
mysql_close($con);
echo
'<p
align="center"><br>Volver
a
la
Pagina
Principal
<a
href="consultaalumno.html"> ahora </a></p>';
?>
docenteunt@yahoo.es
docenteunt@yahoo.es
docenteunt@yahoo.es
index.html
<html><head><title></title></head>
<div align="center">
<img src="img/onpe.jpg" width="300"><br>
<a href="registro.html">Registro</a><br>
<a href="consulta.php">Consulta</a>
</div></body></html>
registro.html
<html><head><title></title></head><body>
<div align="center">
<a href="index.html"><img src="img/onpe.jpg" width="300"></a>
<b><p>Registro de Votos</p></b>
<form method="post" action="registro.php">
Departamento:
<select name="cbodepartamentos">
<option value="1">ANCASH</option>
<option value="2">PIURA</option>
<option value="3">LIMA</option>
</select><br>
Partido:
<select name="cbopartidos">
<option value="1">PARTIDO 1</option>
<option value="2">PARTIDO 2</option>
<option value="3">PARTIDO 3</option>
</select><br>
Total Votos:
<input type="text" name="txtvotos" size="2"><br>
<input type="submit" value="Grabar">
<input type="reset" value="Limpiar">
</form></div></body></html>
docenteunt@yahoo.es
10
11
registro.php
<html><head><title></title></head><body>
<div align="center">
<a href="index.html"><img src="img/onpe.jpg" width="300"></a><br>
<?php
$con=mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("onpe");
mysql_query("insert votos
values('".$_POST['cbopartidos']."','".$_POST['cbodepartamentos']."',".$_POST['txtvot
os'].")");
mysql_close($con);
echo 'Los votos se han registrado<br>';
?>
<a href="registro.html">Volver</a></div></body></html>
consulta.php
<html><head><title></title></head><body>
<div align="center">
<a href="index.html"><img src="img/onpe.jpg" width="300"></a><br>
<?php
$con=mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("onpe");
$res=mysql_query("select p.partido,sum(v.votos) from partidos p inner join votos v
on p.idpartido=v.idpartido group by p.idpartido order by 2 desc");
echo '<table border="0" align="center"><tr>';
echo '<tr><td>Partido Politico</td><td>Votos</td></tr>';
while ($row=mysql_fetch_row($res))
{
echo '<tr><td>'.$row[0].'</td><td align="center">'.$row[1].'</td></tr>';
}
$res=mysql_query("select sum(votos) from votos");
$row2=mysql_fetch_row($res);
echo '<tr><td>Votos Totales -></td><td align="center">'.$row2[0].'</td></tr>';
echo '</table>';
mysql_close($con);
?>
<a href="registro.html">Volver</a></div></body></html>
docenteunt@yahoo.es
12
docenteunt@yahoo.es
13
Cuando ingresamos usuarios que no han sido registrados aun en la base de datos
tenemos el siguiente mensaje:
docenteunt@yahoo.es
14
librosenred.html
<html><head><title>Libros en Red</title></head><body>
<script language="javascript">
function validar(form)
{
if (form.txtlogin.value.length<1)
{
alert("Ingrese el Login ");
form.txtlogin.focus()
return false;
}
if (form.txtclave.value.length<1)
{
alert("Ingrese la Clave ");
form.txtclave.focus()
return false;
}
form.submit();
}
</script>
<form method="post" action="login.php">
<div align="center"><img src="img\librosenred.jpg" width="400" height="100"><hr>
<table width="280" border=1>
<tr>
<td width="70">Login :</td>
<td width="164"><input type="text" name="txtlogin" size=25></td>
</tr>
<tr>
<td>Clave : </td>
<td><input type="password" name="txtclave" size=6></td>
</tr>
</table><br><hr>
<input type="button" onclick="validar(this.form)" value="Ingresar"><br>
Si no tienes nombre de usuario haz <br>
<a href="registrausuario.html"> click </a><br><br>
</form></div></body></html>
docenteunt@yahoo.es
15
login.php
<?php
$con=mysql_connect("127.0.0.1","root","") or die("Error de conexin al Servidor");
mysql_select_db("Librosenred") or die(mysql_error());
$res=mysql_query("select * from usuarios where
login='".$_POST['txtlogin']."' and clave='".md5($_POST['txtclave'])." '",$con) or
die("Fallo la Operacion de Consulta ...");
mysql_close($con);
if (mysql_num_rows($res)>0)
{
$row=mysql_fetch_row($res);
echo '<p align="center"> Bienvenido !!<BR><B>'.$row[2].'</B><BR></p>';
}
else
{
echo '<p align="center">Lo sentimos, pero sus credenciales no son validas
!!<br></p>';
}
echo '<p align="center">Volver a la Pgina Principal <a href="librosenred.html">
ahora </a></p>';
?>
registrausuario.html
<html><head><title> Registro de Usuarios </title></head><body>
<script language="javascript">
function validar(form)
{
if (form.txtnombres.value.length<1)
{
alert("Ingrese el nombre ");
form.txtnombres.focus()
return false;
}
if (form.txtlogin.value.length<1)
{
alert("Ingrese el Login ");
form.txtlogin.focus()
return false;
}
if (form.txtclave1.value.length<1)
{
alert("Ingrese la Clave ");
form.txtclave1.focus()
return false;
}
if (form.txtclave2.value.length<1)
{
docenteunt@yahoo.es
16
17
18
<td>Sexo :</td>
<td>
<input type="radio" name="rbsexo" value="M"
checked="checked">Masculino
<input type="radio" name="rbsexo" value="F">Femenino
</td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input type="button" onClick="validar(this.form)" value="Grabar Registro ">
</div></td>
</tr>
</table><hr><a href="librosenred.html"> Regresar </a>
</center></form></body></html>
registrausuario.php
<?php
$con=mysql_connect("127.0.0.1","root","") or die(mysql_error());
mysql_select_db("Librosenred") or die(mysql_error());
$res=mysql_query("select * from usuarios where login='".$_POST['txtlogin']."' and
clave='".md5($_POST['txtclave1'])."'",$con) or die(mysql_error());
if ($row=mysql_fetch_row($res)){
mysql_close($con);
die("Lo sentimos, el usuario ".$_POST['txtlogin']." ya existe ");
}
$nacimiento=$_POST['cbodia']."/".$_POST['cbomes']."/".$_POST['txtperiodo'];
$res=mysql_query("insert into usuarios
values('".$_POST['txtlogin']."','".md5($_POST['txtclave1'])."','".$_POST['txtnombres']."'
,'".$_POST['txtdireccion']."','".$nacimiento."','".$_POST['rbsexo']."')",$con) or
die(mysql_error());
echo 'Login: '.$_POST['txtlogin'].'<br>';
echo 'Nombres: '.$_POST['txtnombres'].'<br>';
echo 'Direccin :'.$_POST['txtdireccion'].'<br>';
echo 'Nacimiento:'.$nacimiento.'<br>';
echo 'Sexo :';
if($_POST['rbsexo']=='M')
echo 'HOMBRE';
else
echo 'MUJER';
mysql_close($con);
?>
<p align="center"><b> ... Se ha registrado el usuario </b></p><hr>
Volver a la Pgina principal <a href="librosenred.html"> ahora </a>
docenteunt@yahoo.es
19
+ EJEMPLOS
Los siguientes ejemplos debern ser desarrollados y estn formulados en base a los
ejemplos anteriores.
docenteunt@yahoo.es