Hola muy buenos días a todos, llevo años leyendoos en modo incógnito y siempre habíais resuelto mis dudas a partir de las dudas de otros, pero hace una semana he empezado a darle al PHP y puff estoy bastante perdido.
Me gustaría que pudierais ayudarme con mi ejercicio, de él depende si repito curso o no y entre el agobio y demás no tengo ni idea de como hacerlo.
Os cuento:
Pues me dan esta foto:
y después me piden esto
-Navegar por todos los registros de la tabla con los botones:
-Primero:Vaya al primer registro
-Anterior: Vaya al registro anterior
-Siguiene: Vaya al registro siguiente
-Ultimo: Vaya al ultimo registro
-Crear nuevos alumnos: Cuando se pulsa el boton alta, en el mismo formulario anterior se borran los campos, se rellenarán y al pulsar el boton grabar se creara (insert) un nuevo registro con los datos.
-Modificar Datos de un alumno: Utilizaremos los botones para llegar al alumno a modificar, rellenaremos los datos y pulsaremos grabar, que hara que se guarde la informacion.
-Borrar ficha de alumno:Al pulsar el boton borrar se bora al alumno seleccionado y se vuelve a cargar el alumno anterior a ese.
Ahora os pego mi código,
Primero el index.php
Código:
<html>
<head>
<title>Ejemplo de PHP</title>
</head>
<body>
<div align="center">
<H1>Ejercicio IAW</H1>
<?php
include("sql.php");
$link=Conectarse();
if ($link==0)
{
echo "<H1>Error en apertura de bases de datos.</H1>";
exit();
}
$result=mysql_query("select * from alumnos where codigo=1",$link);
$result1=mysql_query("select * from cursos",$link);
$row = mysql_fetch_array($result);
echo "<table>";
echo "<form>";
echo "<tr>";
echo "<td>";
echo "Codigo: ";
echo "</td>";
echo "<td>";
echo $row['codigo'];
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo "<label for='nombre'>Nombre:</label>";
echo "</td>";
echo "<td>";
echo "<input type='text' name=nombre value=" . $row['nombre'] . " size='30' />";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo "<label for='mail'>Mail:</label>";
echo "</td>";
echo "<td>";
echo "<input type='text' name=mail value=" . $row['mail'] . " size='30' />";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo "<label for='codigocursol'>Curso:</label>";
echo "</td>";
echo "<td>";
echo "<select name='codigocurso'>";
while ($row1 = mysql_fetch_array($result1)) {
if ($row1['codigo']==$row['codigocurso']){
echo "<option SELECTED value=" .$row1['codigo'] . "\>" . $row1['nombrecurso'] . "</option>";
} else {
echo "<option value=" .$row1['codigo'] . "\>" . $row1['nombrecurso'] . "</option>";
}
}
echo "</form>";
echo "</table>";
?>
<br>
<input type="button" name="primer" Value="<<" onclick="" >
<input type="button" name="atras" Value="<" onclick="" >
<input type="button" name="adelante" Value=">" onclick="" >
<input type="button" name="ultimo" Value=">>" onclick="" >
<br>
<input type="button" name="alta" Value="Alta de usuario" onclick="" >
<input type="button" name="grabar" Value="Grabar" onclick="" >
<input type="button" name="borrar" Value="Borrar" onclick="" >
<input type="button" name="cerrar" Value="Cerrar" onclick="" >
<?php
//liberamos memoria que ocupa la consulta...
mysql_free_result($result);
//cerramos la conexión con el motor de BD
mysql_close($link);
?>
</div>
</body>
</html>
y ahora el sql.php
Código:
<?php
function Conectarse()
{//inttroducimos los datos de host que son "Server", "usuario" y "contraseña"
if (!($link=mysql_connect("localhost","asir2","rycryc")))//aca hay que introducir los datos que especifique arriba!!!
{
return 0;
}
if (!mysql_select_db("asir2",$link))
{
return 0;
}
return $link;
}?>
Muchas gracias por vuestra ayuda
EDITO: es sobre una tabla alumnos con los campos:
-codigo con auto_increment
-nombre
-mail
-codigocurso = 1, 2, 3
tabla cursos:
-codigo:1, 2, 3
-nombrecurso: PHP, JAVA, HTML