Ver Mensaje Individual
  #1 (permalink)  
Antiguo 16/05/2011, 17:02
gmzatos
 
Fecha de Ingreso: mayo-2011
Mensajes: 18
Antigüedad: 13 años
Puntos: 0
Que opinan de este codigo?

Hola a todos.

Luego de estudiar bastante el dia de hoy php y mysql pues por fin hice mi sistema de noticias y me gustaría opiniones que puedan mejorar el código.

Configuración:

Código PHP:
Ver original
  1. <?php
  2. //Conexion a la db
  3. $connect=mysql_connect('localhost','root','ragnarok') or die ('Error al conectar');
  4. //Seleccion de base de datos
  5. mysql_select_db('ragnarok') or die ('Por favor verifique su base de datos.');
  6. // Consultas
  7. $sql = "SELECT * FROM noticias";
  8. $consulta = mysql_query ($sql, $connect) or die (mysql_error());
  9. $error = "Imposible procesar formulario, los campos estan vacios";
  10. $done = "Datos enviados correctamente";
  11. ?>

Formulario para añadir la noticia:

Código PHP:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Agrega tu noticia</title>
  6. <link href='http://fonts.googleapis.com/css?family=Droid+Sans' rel='stylesheet' type='text/css'>
  7. </head>
  8. <style>
  9. #form {
  10.     position: absolute;
  11.     top: 222px;
  12.     height: 45%;
  13.     left: 260px;
  14.     width: 30%;
  15.     text-align: center;
  16.     border-radius:3px;
  17.     -webkit-border-radius:3px;
  18.     -moz-border-radius:3px;
  19.     background-color:#0581E4;
  20.     opacity:0.8;
  21.     border: 1px solid #09F;
  22.     font-family:droid sans;
  23.     font-size: 18px;
  24.     color: #FFF;
  25.     text-shadow: 1px 0px 1px #000;
  26.     }
  27. body {
  28.     background-image:url(ragnarok%20-%20004.jpg);
  29.     background-attachment:fixed;
  30.     background-size:100%;
  31.     -moz-background-size:100%;
  32.     }
  33. </style>
  34. <body background="ragnarok - 004.jpg">
  35. <div id="form"><form id="form1" name="form1" method="post" action="noticia_p.php">
  36.   <p>&nbsp;</p>
  37.   <p>
  38.     <label for="titulo">Titulo:<br />
  39.       <br />
  40.     </label>
  41.     <input type="text" name="titulo" id="titulo" />
  42.   </p>
  43.   <p>
  44.     <label for="contenido">Noticia:<br />
  45.       <br />
  46.     </label>
  47.     <textarea name="contenido" id="contenido" cols="45" rows="5"></textarea>
  48.   </p>
  49.   <p>
  50.     <input type="submit" name="button" id="button" value="Añadir noticia" />
  51.   </p>
  52. </form></div>
  53. <div style="position:absolute; left: 339px; top: 40px; height: 163px; width: 320px;"><img src="logo.png" width="320" height="163" /></div>
  54. </body>
  55. </html>

Proceso para añadir la noticia:

Código PHP:
Ver original
  1. <?php
  2.  
  3. include('config.php');
  4.  
  5. $titulo = $_POST['titulo'];
  6. $contenido = $_POST['contenido'];
  7.  
  8. if($titulo == "" || $contenido == ""){
  9.     echo $error;
  10. } else {
  11. $sql = "INSERT INTO noticias (titulo,contenido) values('$titulo','$contenido')";
  12. $cnt = mysql_query($sql,$connect) or die (mysql_error());
  13. echo "¡Tu noticia a sido añadida!";
  14. }
  15. ?>

Saludos