Foros del Web » Programando para Internet » PHP »

Arreglar codigo UPDATE en PHP

Estas en el tema de Arreglar codigo UPDATE en PHP en el foro de PHP en Foros del Web. Pues verán, he creado un sistema de noticias y comentarios a partir del tema de este blog, pero el archivo edit.php me está dando problemas. ...
  #1 (permalink)  
Antiguo 04/07/2011, 15:27
 
Fecha de Ingreso: junio-2011
Mensajes: 92
Antigüedad: 12 años, 10 meses
Puntos: 3
Arreglar codigo UPDATE en PHP

Pues verán, he creado un sistema de noticias y comentarios a partir del tema de este blog, pero el archivo edit.php me está dando problemas.
el codigo que uso es este.

editar.php
Código:
<?php

$id=$_GET["id"];
$connect = mysql_connect("server","user","paswd");

mysql_select_db("database",$connect);

$result=mysql_query("select * from noticias where id_noticia='$id'",$connect);

while($row=mysql_fetch_array($result))
{?>
<style type="text/css">
<!--
.Estilo1 {font-family: Arial, Helvetica, sans-serif}
.Estilo2 {font-size: 24px}
body {
	background-color: #000000;
}
-->
</style>

<form action="edit.php" method="post">
  <table width="800" border="0" align="center" cellpadding="0" cellspacing="10">
  <tr>
    <td width="780" height="488" background="images/dentro1.png"><div align="center" class="Estilo1 Estilo2">Editar Noticia
        <input type="hidden" name="id" value = "<?php echo $row['id_noticia'];?>" />
    </div>
      <div align="center" class="Estilo1">
      <table width="534" border="0" cellspacing="10" cellpadding="0">
        <tr>
          <td width="177">T&iacute;tulo de la noticia </td>
          <td width="327"><input name="titulo" type="text" value="<?php echo $row['titulo']; ?>" size="50" /></td>
        </tr>
        <tr>
          <td height="29">Autor</td>
          <td><label>
            <select name="autor" id="autor">
              <option value="<?php echo $row['autor']; ?>" selected="selected"><?php echo $row['autor']; ?></option>
              <option value="Admin">Admin</option>
            </select>
          </label></td>
        </tr>
        <tr>
          <td>Categor&iacute;a</td>
          <td><label>
            <select name="categoria" id="categoria">
              <option value="<?php echo $row['categoria']; ?>" selected="selected"><?php echo $row['categoria']; ?></option>
              <option value="Foro">Foro</option>
              <option value="Web">Web</option>
              <option value="Blog">Blog</option>
              <option value="Ocio">Ocio</option>
              <option value="Tiempo Libre">Tiempo libre</option>
              <option value="Juegos">Juegos</option>
            </select>
          </label></td>
        </tr>
        <tr>
          <td>Noticia</td>
          <td><textarea name="articulo" cols="40" rows="10"><?php echo $row['noticia']; ?></textarea></td>
        </tr>
        <tr>
          <td>Imagen de la NOTICIA </td>
          <td><input name="imagen" type="text" value="<?php echo $row['imagen']; ?>" size="50" /></td>
        </tr>
      </table>
      <p><br />
            <input name="submit" type="submit" value="Publicar" />
            <br />
        </p>
      </div></td>
  </tr>
</table>
<p>&nbsp;</p></p>
  <p>
    <?php
}
mysql_free_result($result);
mysql_close($connect);

?>
  </p>
</form>  
<p><br>
</p>
</form>
edit.php
Código:
<?php
$id=$_POST['id'];
$titulo=$_POST['titulo'];
$autor=$_POST['autor'];
$categoria=$_POST['categoria'];
$noticia=$_POST['noticia'];
$imagen=$_POST['imagen'];


mysql_connect("server","user","passwd");

mysql_select_db("database);  
$sql = "UPDATE noticias SET id_noticia='$id', titulo='$titulo', autor='$autor', noticia='$noticia', categoria='$categoria', imagen='$imagen' WHERE id_noticia='$id'";

$result = mysql_query($sql, $link) or die("Error en consulta:".mysql_error() );

header("location: index.php");
?>
SEgún me dice, el error está en:
$sql = "UPDATE noticias SET id_noticia='$id', titulo='$titulo', autor='$autor', noticia='$noticia', categoria='$categoria', imagen='$imagen' WHERE id_noticia='$id'";

pero no veeo error ._.
  #2 (permalink)  
Antiguo 04/07/2011, 15:41
 
Fecha de Ingreso: junio-2010
Ubicación: Venezuela, Zulia
Mensajes: 686
Antigüedad: 13 años, 9 meses
Puntos: 55
Respuesta: Arreglar codigo UPDATE en PHP

seria bueno que dijeras cual es el error que te muestra
__________________
<?php echo "No te metas a lo hondo del Mar si no sabes nadar, primero aprende a nadar" ?>
...Error en linea: 1 o.O
  #3 (permalink)  
Antiguo 04/07/2011, 15:42
Avatar de bUllan9ebrio  
Fecha de Ingreso: enero-2011
Ubicación: Chile
Mensajes: 1.128
Antigüedad: 13 años, 2 meses
Puntos: 128
Respuesta: Arreglar codigo UPDATE en PHP

Ese no es el error (update)

el error esta antes, te falta una " mira :

Código PHP:
Ver original
  1. mysql_select_db("database);

y deberia ser :

Código PHP:
Ver original
  1. mysql_select_db("database");

Saludos.
__________________
Si no vivimos como pensamos, pronto empezaremos a pensar como vivimos.
Más vale un Gracias sincero, que un número que aumente "popularidad" ¬¬°

Universidad de chile, campeón
  #4 (permalink)  
Antiguo 04/07/2011, 15:46
 
Fecha de Ingreso: junio-2011
Mensajes: 92
Antigüedad: 12 años, 10 meses
Puntos: 3
Respuesta: Arreglar codigo UPDATE en PHP

Cita:
Iniciado por bUllan9ebrio Ver Mensaje
Ese no es el error (update)

el error esta antes, te falta una " mira :

Código PHP:
Ver original
  1. mysql_select_db("database);

y deberia ser :

Código PHP:
Ver original
  1. mysql_select_db("database");

Saludos.
nono, eso está bien XD, el " lo borrñe yo sin querer cuando borre el nombre de la db para el tema...

Cual puede ser el error?
  #5 (permalink)  
Antiguo 04/07/2011, 15:56
Avatar de bUllan9ebrio  
Fecha de Ingreso: enero-2011
Ubicación: Chile
Mensajes: 1.128
Antigüedad: 13 años, 2 meses
Puntos: 128
Respuesta: Arreglar codigo UPDATE en PHP

Realiza un


Código PHP:
Ver original
  1. echo "$sql <br>";


Copia la query directo al PhpMyAdmin, tal vez tienes un campo vacio y la bd no te deje igresar valores NULL o tienes diferente tipo de campo, int, varchar etc...
__________________
Si no vivimos como pensamos, pronto empezaremos a pensar como vivimos.
Más vale un Gracias sincero, que un número que aumente "popularidad" ¬¬°

Universidad de chile, campeón
  #6 (permalink)  
Antiguo 04/07/2011, 15:57
Avatar de skiper0125  
Fecha de Ingreso: octubre-2010
Ubicación: $this->Mexico('Toluca');
Mensajes: 1.127
Antigüedad: 13 años, 6 meses
Puntos: 511
Respuesta: Arreglar codigo UPDATE en PHP

Hola que tal.

Comprueba si efectivamente recibes todos los valores de tus variables con



Código PHP:
Ver original
  1. <?php
  2. var_dump($_POST);
  3. $id=$_POST['id'];
  4. $titulo=$_POST['titulo'];
  5. $autor=$_POST['autor'];
  6. $categoria=$_POST['categoria'];
  7. $noticia=$_POST['noticia'];
  8. $imagen=$_POST['imagen'];
y comentas
__________________
Recuerda que estamos aquí para orientarte, y no para hacer tu trabajo.
Si mi aporte fue de ayuda, recuerda que agradecer no cuesta nada +1

Skiper0125
  #7 (permalink)  
Antiguo 04/07/2011, 15:59
Avatar de pateketrueke
Modernizr
 
Fecha de Ingreso: abril-2008
Ubicación: Mexihco-Tenochtitlan
Mensajes: 26.399
Antigüedad: 16 años
Puntos: 2534
Respuesta: Arreglar codigo UPDATE en PHP

Cita:
Iniciado por Kilin Ver Mensaje
[...] Cual puede ser el error?
Te voy a preguntar de la manera mas amable, ¿recibes algún mensaje completo de error?
__________________
Y U NO RTFM? щ(ºдºщ)

No atiendo por MP nada que no sea personal.
  #8 (permalink)  
Antiguo 04/07/2011, 16:41
 
Fecha de Ingreso: junio-2011
Mensajes: 92
Antigüedad: 12 años, 10 meses
Puntos: 3
Respuesta: Arreglar codigo UPDATE en PHP

gracias a todos pero ya vi el fallo xD
mysql_select_db("database");

esa parte tendria que haber sido
mysql_select_db("database", $link);
y la anterior

mysql_connect("serv", "user", "pswd");

$link = mysql_connect("serv", "user", "paswd");
  #9 (permalink)  
Antiguo 04/07/2011, 16:46
 
Fecha de Ingreso: junio-2011
Mensajes: 92
Antigüedad: 12 años, 10 meses
Puntos: 3
Respuesta: Arreglar codigo UPDATE en PHP

Tengo otro problema xD
Ahora, aplicando el codigo a otro formulario, me sale este error
Error en consulta UPDATE home SET id_noticia='', noticiauno='', noticiados='', noticiatres='', noticiacuatro='', noticiacinco='' WHERE id_noticia=:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

el codigo que use fue:

Código:
<?php
$id=$_POST['id'];
$noticiauno=$_POST['noticia1'];
$noticiados=$_POST['noticia2'];
$noticiatres=$_POST['noticia3'];
$noticiacuatro=$_POST['noticia4'];
$noticiacinco=$_POST['noticia5'];


$link = mysql_connect("serv", "user", "pswd");

mysql_select_db("db", $link);  
$sql = "UPDATE home SET id_noticia='$id', noticiauno='$noticia1', noticiados='$noticia2', noticiatres='$noticia3', noticiacuatro='$noticia4', noticiacinco='$noticia5' WHERE id_noticia=$id";

$result = mysql_query($sql, $link) or die("Error en consulta $sql:".mysql_error() );

header("location: index.php");
?>
  #10 (permalink)  
Antiguo 04/07/2011, 17:08
Avatar de skiper0125  
Fecha de Ingreso: octubre-2010
Ubicación: $this->Mexico('Toluca');
Mensajes: 1.127
Antigüedad: 13 años, 6 meses
Puntos: 511
Respuesta: Arreglar codigo UPDATE en PHP

Prueba con esto

Código PHP:
Ver original
  1. $id=$_POST['id'];
  2. $noticiauno=$_POST['noticia1'];
  3. $noticiados=$_POST['noticia2'];
  4. $noticiatres=$_POST['noticia3'];
  5. $noticiacuatro=$_POST['noticia4'];
  6. $noticiacinco=$_POST['noticia5'];
  7.  
  8.  
  9. $link = mysql_connect("serv", "user", "pswd") or die("Error server"mysql_error());
  10.  
  11. mysql_select_db("db", $link);or die("Error base de datos"mysql_error());
  12. $sql = "UPDATE home SET id_noticia='$id', noticiauno='$noticia1', noticiados='$noticia2', noticiatres='$noticia3', noticiacuatro='$noticia4', noticiacinco='$noticia5' WHERE id_noticia='$id'";
  13.  
  14. $result = mysql_query($sql) or die("Error en consulta $sql:".mysql_error() );
  15.  
  16. header("location: index.php");
__________________
Recuerda que estamos aquí para orientarte, y no para hacer tu trabajo.
Si mi aporte fue de ayuda, recuerda que agradecer no cuesta nada +1

Skiper0125
  #11 (permalink)  
Antiguo 04/07/2011, 17:27
 
Fecha de Ingreso: junio-2011
Mensajes: 92
Antigüedad: 12 años, 10 meses
Puntos: 3
Respuesta: Arreglar codigo UPDATE en PHP

Cita:
Iniciado por skiper0125 Ver Mensaje
Prueba con esto

Código PHP:
Ver original
  1. $id=$_POST['id'];
  2. $noticiauno=$_POST['noticia1'];
  3. $noticiados=$_POST['noticia2'];
  4. $noticiatres=$_POST['noticia3'];
  5. $noticiacuatro=$_POST['noticia4'];
  6. $noticiacinco=$_POST['noticia5'];
  7.  
  8.  
  9. $link = mysql_connect("serv", "user", "pswd") or die("Error server"mysql_error());
  10.  
  11. mysql_select_db("db", $link);or die("Error base de datos"mysql_error());
  12. $sql = "UPDATE home SET id_noticia='$id', noticiauno='$noticia1', noticiados='$noticia2', noticiatres='$noticia3', noticiacuatro='$noticia4', noticiacinco='$noticia5' WHERE id_noticia='$id'";
  13.  
  14. $result = mysql_query($sql) or die("Error en consulta $sql:".mysql_error() );
  15.  
  16. header("location: index.php");

me da un error de sintaxis

Parse error: syntax error, unexpected T_STRING in /data/multiserv/users/616694/projects/1516211/www/system/noticias2.php on line 10
  #12 (permalink)  
Antiguo 04/07/2011, 17:35
Avatar de skiper0125  
Fecha de Ingreso: octubre-2010
Ubicación: $this->Mexico('Toluca');
Mensajes: 1.127
Antigüedad: 13 años, 6 meses
Puntos: 511
Respuesta: Arreglar codigo UPDATE en PHP

Cita:
Iniciado por Kilin Ver Mensaje
me da un error de sintaxis

Parse error: syntax error, unexpected T_STRING in /data/multiserv/users/616694/projects/1516211/www/system/noticias2.php on line 10
Perdon tiene errores

Código PHP:
Ver original
  1. $id=$_POST['id'];
  2.     $noticiauno=$_POST['noticia1'];
  3.     $noticiados=$_POST['noticia2'];
  4.     $noticiatres=$_POST['noticia3'];
  5.     $noticiacuatro=$_POST['noticia4'];
  6.     $noticiacinco=$_POST['noticia5'];
  7.      
  8.      
  9.     $link = mysql_connect("serv", "user", "pswd") or die("Error server".mysql_error());
  10.      
  11.     mysql_select_db("db", $link);or die("Error base de datos: ".mysql_error());
  12.     $sql = "UPDATE home SET id_noticia='$id', noticiauno='$noticia1', noticiados='$noticia2', noticiatres='$noticia3', noticiacuatro='$noticia4', noticiacinco='$noticia5' WHERE id_noticia='$id'";
  13.      
  14.     $result = mysql_query($sql) or die("Error en consulta $sql: ".mysql_error() );
  15.      
  16.     header("location: index.php");
__________________
Recuerda que estamos aquí para orientarte, y no para hacer tu trabajo.
Si mi aporte fue de ayuda, recuerda que agradecer no cuesta nada +1

Skiper0125
  #13 (permalink)  
Antiguo 04/07/2011, 17:46
 
Fecha de Ingreso: junio-2011
Mensajes: 92
Antigüedad: 12 años, 10 meses
Puntos: 3
Respuesta: Arreglar codigo UPDATE en PHP

Cita:
Iniciado por skiper0125 Ver Mensaje
Perdon tiene errores

Código PHP:
Ver original
  1. $id=$_POST['id'];
  2.     $noticiauno=$_POST['noticia1'];
  3.     $noticiados=$_POST['noticia2'];
  4.     $noticiatres=$_POST['noticia3'];
  5.     $noticiacuatro=$_POST['noticia4'];
  6.     $noticiacinco=$_POST['noticia5'];
  7.      
  8.      
  9.     $link = mysql_connect("serv", "user", "pswd") or die("Error server".mysql_error());
  10.      
  11.     mysql_select_db("db", $link);or die("Error base de datos: ".mysql_error());
  12.     $sql = "UPDATE home SET id_noticia='$id', noticiauno='$noticia1', noticiados='$noticia2', noticiatres='$noticia3', noticiacuatro='$noticia4', noticiacinco='$noticia5' WHERE id_noticia='$id'";
  13.      
  14.     $result = mysql_query($sql) or die("Error en consulta $sql: ".mysql_error() );
  15.      
  16.     header("location: index.php");
Bueno, probando ese codigo me di cuenta de que uno de mis fallos era poner en el update $noticia1 cuando deberia ser $noticiauno

pero con ese codigo y el fallo mio arreglado, no me da ningun fallo, pero tampoco se modifica nada

Etiquetas: arreglar, update
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 14:22.