Tema: insercion
Ver Mensaje Individual
  #3 (permalink)  
Antiguo 03/09/2004, 12:51
Avatar de macabro
macabro
 
Fecha de Ingreso: enero-2003
Ubicación: venus >> ((_\
Mensajes: 254
Antigüedad: 21 años, 3 meses
Puntos: 1
agregar.php
Código PHP:
<?php
$nombre
$_POST['nombre'];
$apellido $_POST['apellido'];
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>agregar</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form action="procesar.php" method="post">
Nombre:<input name="nombre" type="text" id="nombre" size="30"><br>
Apellido:<input name="apellido" type="text" id="apellido" size="30">
<input type="submit" name="submit" value="Insertar">
</form>
</body>
</html>
conexion.php
Código PHP:
<?php
// configuaracion para la conexxion a la base de datos
$servidor  'localhost';         // Servidor "localhost"
$usuario   'xxx';              // Usuario para conectarse
$clave     'xxx';             // Contraseña parala coneccion
$base      'xxx';              // Base de datos

//procedimiento a la conexion
$conex = @mysql_connect("$servidor""$usuario""$clave");
    if (!
$conex) {
      echo( 
"<p>No se pudo conectar .</p>".mysql_errno().": ".mysql_error()."" );
      exit();
    }

    
// Seleccinar base de datos
    
if (! @mysql_select_db("$base") ) {
      echo( 
"<p>No se pudo encontrar la base de datos.</p> ".mysql_errno().": ".mysql_error()."" );
      exit();
    }
?>
procesar.php
Código PHP:
<?php
//conectamos a la base de datos
require_once('conexion.php');  
        if (!
$nombre | !$apellido)
        {
die(
'Usted no lleno todos los campos <a href="agregar.php">Regresar</a>');
        }
 
      
$sql "INSERT INTO usuarios (nombre ,apellido, fecha ) 
              VALUES ('$nombre','$apellido',NOW())"
;
              
      if (@
mysql_query($sql)) {
        echo(
"<P>Su insercion ha sido con exito!! <a href='agregar.php'>agregar de nuevo</a> &nbsp; <a href='mostrar.php'>Mostrar</a></P>");
      } else {
        echo(
"<P>error al insertar: ".mysql_error()."</P>");
      }
   
?>
mostrar.php
Código PHP:
<?php 
//conectamos a la base de datos
require_once('conexion.php');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Mostrar</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<a href="agregar.php">Agregar uno nuevo</a>
        <?php
        
echo"<table width='400' border='1'><tr><td>Nombre</td><td>Apellido</td><td>Fecha</td></tr>";
        
            
$result = @mysql_query("SELECT * FROM usuarios ORDER BY fecha DESC");
    if (!
$result) {
      echo(
"<p>Error al seleccionar: " mysql_error() . "</p>");
      exit();
    }
    
    while ( 
$row mysql_fetch_array($result) ) {
    
$id $row["id"];
    
$nombre$row["nombre"];
    
$apellido $row["apellido"];
    
$fecha $row["fecha"];
    
    
//convertimos fecha al español
    
list($anio,$mes,$dia)=explode("-",$row["fecha"]);
        
$date ="$dia-$mes-$anio";
        
//Mostramos los datos
echo "<tr><td>$nombre </td><td>$apellido</td><td> $fecha </td>";
    } 
    echo
"</tr></table>";
    
?>
</body>
</html>
Esto te puede servir como comentarios para tus paginas webs si gustas, haciendo algunos cambios