Foro de elhacker.net

Programación => PHP => Mensaje iniciado por: phirkan en 28 Marzo 2013, 22:52 pm



Título: mi agenda en php con mysql
Publicado por: phirkan en 28 Marzo 2013, 22:52 pm
Buenas, hice un cursillo de php y nos mandaron hacer una agenda para gestionarlo  mysql y con ficheros. La verdad es que me quedó bastante sencillito de comprender, aquí os dejo mi agenda en mysql, si a alguien le interesa como gestionarlo con ficheros que me lo pida sin problemas. ^_^

Solo tendreis que crear la base de datos en mysql a traves del MyAdmin, o  por comandos sql;

Create database agenda; // y listo xD

<?php
@   $conexion = new mysqli("localhost","root","tucontraseña","agenda");
         if ($ERROR = mysqli_connect_errno()){
        echo "Error de conexión";
        echo "error :$error";
    }
      else {$sql = 'create table if not exists agenda_antonio(nombre varchar(20), telefono int(9), email varchar(40))';
            $conexion->query($sql);
            }
 
   


 echo "<fieldset>
 <legend>AGENDA!</legend><br>

<br>  <form action=\"\" name=\"form\" method=\"POST\">

//En el Onfocus(cuando hacemos clic en el box["input type='text'"]) vemos un poquito de javascript
Nombre: <input type=\"text\" value=\"Introduce el nombre\"    name=\"nombre\"      onFocus=\"if (this.value=='Introduce el nombre') this.value=''\"><br>

 telefono: <input type=\"text\" value=\"Introduce telefono\" name=\"telefono\" onFocus=\"if (this.value=='Introduce telefono') this.value=''\"><br>

 e-mail: <input type=\"text\" value=\"Introduce email\" name=\"email\" onFocus=\"if (this.value=='Introduce email') this.value=''\"><br>

  <input type=\"submit\" value=\"Agregar\" name=\"agregar\" id=\"agregar\">
  <input type=\"submit\" value=\"Listar todo\" name=\"listar\" id=\"listar\"><br>
  <input type=\"submit\" value=\"Borrar registro\" name=\"borrar\" id=\"borrar\">
  <input type=\"submit\" value=\"Borrar todo\" name=\"borrart\" id=\"borrart\">
           </form>
</fieldset>";
 
 if(isset($_POST['agregar'])){
    //Definimos la consulta(insert)
    $meter = "insert into agenda_antonio values('".$_POST['nombre']."','".$_POST['telefono']."','".$_POST['email']."')";
    $conexion->query($meter);
    echo "<br><p align=center>El contacto '".$_POST['nombre']."' se ha agregado.</p><br>";
   

                     //Para que aparezca la tabla cuando agregamos un registro
    $TONTA = "Select * from agenda_antonio";
    $CAMPO=$conexion->query($TONTA);
@        $FILA=$CAMPO->fetch_object();
   
    echo "<table border=1 align=center>";
                    echo "<tr  align=center>";
                    echo "<th width=\"100\">";
                    echo "Nombre";
                    echo "</th>";
                    echo "<th>";
                    echo "Telefono";
                    echo "</th>";
                    echo "<th width=\"200\">";
                    echo "Web";
                    echo"</th>";
   
                     while ($FILA){
                    echo "<tr align=center>";
                    echo "<td>";
                    echo $FILA->nombre;
                    echo "</td>";           
                    echo "<td>";
                    echo $FILA->telefono;
                    echo "</td>";
                    echo "<td>";
                    echo $FILA->email;
                    echo "</td>";
                    echo "</tr>";
                    echo "</table>";
                    $FILA=$CAMPO->fetch_object();


}

    }
   
  if(isset($_POST['listar'])){
   
 
                     //Definimos la consulta(select)
    $TONTA = "Select * from agenda_antonio";
    $CAMPO=$conexion->query($TONTA);
@        $FILA=$CAMPO->fetch_object();
        //si la tabla no tiene ningun contacto
    if(!$FILA)
{echo "<p align=center>No existe ningún contacto todavia.</p>";}
    else{
   
    echo "<table border=1 align=center>";
                    echo "<tr  align=center>";
                    echo "<th width=\"100\">";
                    echo "Nombre";
                    echo "</th>";
                    echo "<th>";
                    echo "Telefono";
                    echo "</th>";
                    echo "<th width=\"200\">";
                    echo "Web";
                    echo"</th>";
   
                     while ($FILA){
                    echo "<tr align=center>";
                    echo "<td>";
                    echo $FILA->nombre;
                    echo "</td>";           
                    echo "<td>";
                    echo $FILA->telefono;
                    echo "</td>";
                    echo "<td>";
                    echo $FILA->email;
                    echo "</td>";
                    echo "</tr>";
                    echo "</table>";
                    $FILA=$CAMPO->fetch_object();}
                }

}


if(isset($_POST['borrar'])){

    echo "<fieldset> <legend>Borrar contacto!</legend><br><br></tittle><form action=\"\" name=\"form\" method=\"POST\">
 Nombre: <input type=\"text\" value=\"Introduce el nombre\" name=\"nom\" onFocus=\"if (this.value=='Introduce el nombre') this.value=''\"><br>";
 
  echo "<input type=\"submit\" value=\"Borrar registro\" name=\"borra\" id=\"borra\"></fieldset>";
      echo " NOTA: Introduce el nombre del contacto completo que quieres borrar:";

}
if(isset($_POST['borra'])){
    //Definimos la consulta(delete)
    $borra = "Delete from agenda_antonio where nombre='".$_POST['nom']."' ";
    $conexion->query($borra);
    echo "Se ha borrado '".$_POST['nom']."' .";}

if(isset($_POST['borrart'])){
    //Definimos la consulta(delete)
    $borrat = "Delete from agenda_antonio;";
    $conexion->query($borrat);
    echo "Se han borrado todos los contactos.";
}
?>

 ;-)