Noviembre 8 del 2017

0 comentarios
Hoy se hizo la última clase del periodo, y también la última evaluación.

A continuación, un ejercicio de base de datos en PHP.

INDEX:

<!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>Untitled Document</title>
</head>
<?php
$mensaje = isset($_GET['mensaje'])?$_GET['mensaje']:"";
if ($mensaje != ""){
?>
<script type="text/javascript">alert("<?=$mensaje;?>");</script>
<?php }?>
<body>
<form id="form1" name="form1" method="post" action="guardar.php">
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td width="16%">&nbsp;</td>
      <td width="74%">&nbsp;</td>
      <td width="10%">&nbsp;</td>
    </tr>
    <tr>
      <td>Nombre : </td>
      <td><input type="text" name="nombre" inputmode="" d="nombre" /></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>Apellido : </td>
      <td><input type="text" name="apellido" id="apellido" /></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="button" id="button" value="Guardar" /></td>
      <td>&nbsp;</td>
    </tr>
  </table>
</form>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td colspan="3"><hr /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td><a href="ver_pantalla.php" target="_blank">Ver Resultados en Pantalla</a></td>
    <td>&nbsp;</td>
    <td><a href="ver_pdf.php" target="_blank">Ver Resultados en Pdf</a></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td colspan="3"><hr /></td>
  </tr>
</table>
</body>
</html>

GUARDAR:

<?php 
$servidor = 'localhost';
$usr_sistema = 'root';
$pass_sistema = 'alumnos';
$base_datos = 'ejemplo_001';

$conexion=mysqli_connect($servidor, $usr_sistema, $pass_sistema);
if (!$conexion){
header ("Location: index.php?mensaje=error de conexion.");
exit();    
}
//-- ------------------------------------------------------------------------------------------------
$nombre = isset($_POST['nombre'])?$_POST['nombre']:"";
$apellido = isset($_POST['apellido'])?$_POST['apellido']:"";
//-- ------------------------------------------------------------------------------------------------
$mensaje = 'Registro Incluido con Exito.';
if (strlen($nombre)==0) $mensaje = "El nombre no puede ser vacio. Intentelo de nuevo.";
elseif (strlen($apellido)==0) $mensaje = "El apellido no puede ser vacio. Intentelo de nuevo.";
else{
//-- ------------------------------------------------------------------------------------------------
$sql = "INSERT INTO ".$base_datos.".usuarios (id, nombre, apellido) "."VALUES(NULL, '"
.$nombre."', '".$apellido."')";
echo $sql;

$sql = mysqli_query($conexion,$sql);
mysqli_free_result($sql);
//-- ------------------------------------------------------------------------------------------------
}
header("Location: index.php?mensaje=".$mensaje);
exit();
?>

VER PANTALLA:

<?php 
$servidor = 'localhost';
$usr_sistema = 'root';
$pass_sistema = 'alumnos';
$base_datos = 'ejemplo_001';
$conexion=mysqli_connect($servidor, $usr_sistema, $pass_sistema);
if (!$conexion){
header ("Location: index.php?mensaje=error de conexion.");
exit();    
}
//-- ------------------------------------------------------------------------------------------------
// -- Cambia dependiendo del ejercicio
$sql= "SELECT id, apellido, nombre, activo "
  ."FROM ".$base_datos.".usuarios  "
  ."ORDER by apellido, nombre";   
$sql = mysqli_query($conexion,$sql); // No cambia
//-- ------------------------------------------------------------------------------------------------
?>
<!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>Untitled Document</title>
</head>

<body><table width="100%" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center" valign="middle"><strong>I.d.</strong></td>
    <td align="center" valign="middle"><strong>Apellido</strong></td>
    <td align="center" valign="middle"><strong>Nombre</strong></td>
    <td align="center" valign="middle"><strong>Activo</strong></td>
  </tr>
<?php
while($rs = mysqli_fetch_array($sql)) { 
$id = $rs[0];
$apellido = $rs[1];
$nombre = $rs[2];
$activo = $rs[3];
?>   
  <tr>
    <td align="center" valign="middle"><?=$id;?></td>
    <td align="center" valign="middle"><?=$apellido;?></td>
    <td align="center" valign="middle"><?=$nombre;?></td>
    <td align="center" valign="middle"><?=$activo;?></td>
  </tr>
<?php }?>  
  </table>
</body>
</html>






Publicar un comentario