Translate-Traducir

18/6/14

Base de datos alumnos y su mantenimiento php

Esto es ta hecho en 3 paginas esta es la pagina conex.php
En este ejercicio haremos la edición,alta,y eliminación de alumnos de la base de datos por medio de php.

Haremos una carpeta llamada conexion  dentro crearemos una pagina llamada conex.php
su codigo sera este:
<?php

    // Conectando, seleccionando la base de datos en web
       
       
     $link = mysql_connect('mysql.hostinger.es', 'u414116232_ariel', '123456')
     or die('No se pudo conectar: ' . mysql_error());
    //mysql.hostinger.es=nombre del hosting, u414116232_ariel=usuario                 //123456=contraseña

    mysql_select_db('u414118745_prueba') or die('No se pudo seleccionar la base       de datos');
    // u414118745_prueba=nombre de base de datos
?>

Ahora crearemos otra pagina llamada alumno.php

<?php 
include ("conexion/conex.php");
//declaro mis variables
$id="";
$nombre="";
$apellido="";
$edad="";
$Operacion="";

if(isset($_REQUEST["id"]) && strlen($_REQUEST["id"])>0){
    $Operacion="Edicion de alumnos presiona guardar luego comprobar";
    if(!isset($_REQUEST["guardar"])){
        $query = "select * from alumnoss where id = ".$_REQUEST["id"];
        $result = mysql_query($query) or die('Consulta fallida: ' . mysql_error());
/*tabla alumnoss*/
        if($line =mysql_fetch_array($result,MYSQL_ASSOC)){
            $id=$line["id"];
            $nombre=$line["nombre"];
            $apellido=$line["apellido"];
            $edad=$line["edad"];
        }
    }
}else if(isset($_REQUEST["delete"])){
        $Operacion="Eliminado presiona comprobar";
        $query = "delete from alumnoss where id = ".$_REQUEST["delete"]; /*tabla alumnoss*/
        $result = mysql_query($query) or die('Consulta fallida: ' . mysql_error());
        header("Location:listaalumnos.php");
}else{
    $Operacion="Insertado de alumnos presiona guardar luego comprobar";
    }

    if(isset($_REQUEST["guardar"])){
        $id=$_REQUEST["id"];
        $nombre=$_REQUEST["nombre"];
        $apellido=$_REQUEST["apellido"];
        $edad=$_REQUEST["edad"];
       
        if ($Operacion=="Insertado de alumnos presiona guardar luego comprobar"){
            $query="insert into alumnoss(nombre,apellido,edad)"; //la tabla se llama alumnoss
            $query= $query." values('".$nombre."','".$apellido."','".$edad."')";
            $result = mysql_query($query) or die('Consulta fallida: ' . mysql_error());
             
        }else{
            $query="update alumnos set nombre ='".$nombre."' ";
            $query=$query.", apellido='".$apellido."'";
            $query=$query.", edad='".$edad."'";
            $query=$query." where id=".$id;
            $result = mysql_query($query) or die('Consulta fallida: ' . mysql_error());
           }
            mysql_free_result($result);
            mysql_close($link);
            header("Location:listaalumnos.php");
        
        }

?>
<html xmlns="http://www.w3.org/1999/xhtml"  >

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>listado de alumnos</title>
</head>


<body>

<center><H3>MANTENIMIENTO DE ALUMNOS</H3>
<?php echo $Operacion;?>

<form action="alumno.php" method="get" name="frm" >
<input name="id" type="hidden" value="<?php echo $id;?>"/><br />
NOMBRE:<input name="nombre" type="text" value="<?php echo $nombre;?>"/>
APELLIDO:<input name="apellido" type="text" value="<?php echo $apellido;?>"/>
EDAD:<input name="edad" type="text" value="<?php echo $edad;?>" /> <br /> <br />
<input name="guardar" type="submit" value="GUARDAR" />
<input name="volver" type="button" value="COMPROBAR" onclick="window.location.href='listaalumnos.php'" />
</form>
</center>

</body>
</html>





Esta es la pagina listaalumnos.php


<?php
include("conexion/conex.php");
?>
<html xmlns="http://www.w3.org/1999/xhtml"  >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Lista alumnos</title>

<script language="javascript">
function eliminar(id){
    if(confirm('¿Desea eliminar el usuario con id=' + id  + '?')){
        window.location.href='alumno.php?delete=' + id;
    }
    }
</script>
</head>
<body>
<center>
<h2>LISTADO DE ALUMNOS</h2>
<?php
$query = 'SELECT * FROM alumnoss'; /*tabla alumnoss*/
$result = mysql_query($query) or die('Consulta fallida: ' . mysql_error());
echo "<table border='2' align=center>\n";
echo "<tr>\n";
echo "<th> ID </th>\n";
echo "<th> Nombre </th>\n";
echo "<th> Apellido </th>\n";
echo "<th> Edad </th>\n";
echo "<th colspan=2>&nbsp;</th>\n";
echo "</tr>\n";
//echo "<tr>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "\t<tr>\n";
    echo "\t\t<td>".$line["id"]."</td>\n";
    echo "\t\t<td>".$line["nombre"]."</td>\n";
    echo "\t\t<td>".$line["apellido"]."</td>\n";
    echo "\t\t<td>".$line["edad"]."</td>\n";
    echo "\t\t<td> <a href=\"alumno.php? id=".$line["id"]."\"><img src=\"images/editar.png\" title=\"Editar\"></a></td>\n";
    echo "\t\t<td> <a href=\"javascript:eliminar(".$line["id"].");\"><img src=\"images/eliminar.png\" title=\"Eliminar\"></a></td>\n";
    echo "\t</tr>\n";
}
echo "</table>\n";

// Liberar resultados
mysql_free_result($result);

// Cerrar la conexión
mysql_close($link);
?>

<input name="enviar" type="button" value="NUEVO" onclick="window.location.href='alumno.php'" />
<input name="volver" type="button" value="VOLVER" onclick="window.location.href='index.php'" />
</center>

</body>
</html>

Aqui les dejo otro link de php  crear-escribir-un-programa-algoritmo-en-php

No olviden compartir.

No hay comentarios: