Foros del Web » Programando para Internet » PHP »

Problema con UPDATE

Estas en el tema de Problema con UPDATE en el foro de PHP en Foros del Web. Estoy intentando actualizar una tabla que tiene 8 imagenes con 8 nombres, este es el codigo: @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código PHP: Ver original <?     ...
  #1 (permalink)  
Antiguo 08/04/2010, 04:54
Avatar de moronino  
Fecha de Ingreso: marzo-2010
Mensajes: 55
Antigüedad: 14 años
Puntos: 0
Pregunta Problema con UPDATE

Estoy intentando actualizar una tabla que tiene 8 imagenes con 8 nombres, este es el codigo:

Código PHP:
Ver original
  1. <?
  2.                                         $cambios.=", nb1='" . $_POST["nb1"] . "'";
  3.                                         $cambios.=", nb2='" . $_POST["nb2"] . "'";
  4.                                         $cambios.=", nb3='" . $_POST["nb3"] . "'";
  5.                                         $cambios.=", nb4='" . $_POST["nb4"] . "'";
  6.                                         $cambios.=", nb5='" . $_POST["nb5"] . "'";
  7.                                         $cambios.=", nb6='" . $_POST["nb6"] . "'";
  8.                                         $cambios.=", nb7='" . $_POST["nb7"] . "'";
  9.                                         $cambios.=", nb8='" . $_POST["nb8"] . "'";
  10.                                        
  11.                                         $sql = 'select img1,img2,img3,img4,img5,img6,img7,img8 from slider WHERE id= ' . $_POST["id"];
  12.                                         $resultFotos=mysql_query($sql,$link);
  13.                                         $rowFotos=mysql_fetch_array($resultFotos);
  14.                                         mysql_free_result($resultFotos);
  15.                                        
  16.                                         if ($HTTP_POST_FILES['img1']['name']!="")
  17.                                         {
  18.                                             $nombre_archivo = "../images/botellas/" . $HTTP_POST_FILES['img1']['name'];
  19.                                             $img2 = $HTTP_POST_FILES['img1']['name'];
  20.                                             if ($rowFotos["img1"]!="")
  21.                                             {
  22.                                                 unlink("../images/botellas/" . $rowFotos["img1"]);
  23.                                             }
  24.                                             move_uploaded_file($HTTP_POST_FILES['img1']['tmp_name'], $nombre_archivo);
  25.                                             $cambios.=", img1='" . $img1 . "'";
  26.                                         }
  27.                                        
  28.                                         if ($HTTP_POST_FILES['img2']['name']!="")
  29.                                         {
  30.                                             $nombre_archivo = "../images/botellas/" . $HTTP_POST_FILES['img2']['name'];
  31.                                             $img2 = $HTTP_POST_FILES['img2']['name'];
  32.                                             if ($rowFotos["img2"]!="")
  33.                                             {
  34.                                                 unlink("../images/botellas/" . $rowFotos["img2"]);
  35.                                             }
  36.                                             move_uploaded_file($HTTP_POST_FILES['img2']['tmp_name'], $nombre_archivo);
  37.                                             $cambios.=", img2='" . $img2 . "'";
  38.                                         }
  39.                                        
  40.                                         if ($HTTP_POST_FILES['img3']['name']!="")
  41.                                         {
  42.                                             $nombre_archivo = "../images/botellas/" . $HTTP_POST_FILES['img3']['name'];
  43.                                             $img2 = $HTTP_POST_FILES['img3']['name'];
  44.                                             if ($rowFotos["img3"]!="")
  45.                                             {
  46.                                                 unlink("../images/botellas/" . $rowFotos["img2"]);
  47.                                             }
  48.                                             move_uploaded_file($HTTP_POST_FILES['img3']['tmp_name'], $nombre_archivo);
  49.                                             $cambios.=", img3='" . $img3 . "'";
  50.                                         }
  51.                                        
  52.                                         if ($HTTP_POST_FILES['img4']['name']!="")
  53.                                         {
  54.                                             $nombre_archivo = "../images/botellas/" . $HTTP_POST_FILES['img4']['name'];
  55.                                             $img2 = $HTTP_POST_FILES['img4']['name'];
  56.                                             if ($rowFotos["img4"]!="")
  57.                                             {
  58.                                                 unlink("../images/botellas/" . $rowFotos["img4"]);
  59.                                             }
  60.                                             move_uploaded_file($HTTP_POST_FILES['img4']['tmp_name'], $nombre_archivo);
  61.                                             $cambios.=", img4='" . $img4 . "'";
  62.                                         }
  63.                                        
  64.                                         if ($HTTP_POST_FILES['img5']['name']!="")
  65.                                         {
  66.                                             $nombre_archivo = "../images/botellas/" . $HTTP_POST_FILES['img5']['name'];
  67.                                             $img2 = $HTTP_POST_FILES['img5']['name'];
  68.                                             if ($rowFotos["img5"]!="")
  69.                                             {
  70.                                                 unlink("../images/botellas/" . $rowFotos["img5"]);
  71.                                             }
  72.                                             move_uploaded_file($HTTP_POST_FILES['img5']['tmp_name'], $nombre_archivo);
  73.                                             $cambios.=", img5='" . $img5 . "'";
  74.                                         }
  75.                                        
  76.                                         if ($HTTP_POST_FILES['img6']['name']!="")
  77.                                         {
  78.                                             $nombre_archivo = "../images/botellas/" . $HTTP_POST_FILES['img6']['name'];
  79.                                             $img2 = $HTTP_POST_FILES['img6']['name'];
  80.                                             if ($rowFotos["img6"]!="")
  81.                                             {
  82.                                                 unlink("../images/botellas/" . $rowFotos["img6"]);
  83.                                             }
  84.                                             move_uploaded_file($HTTP_POST_FILES['img6']['tmp_name'], $nombre_archivo);
  85.                                             $cambios.=", img6='" . $img6 . "'";
  86.                                         }
  87.                                        
  88.                                         if ($HTTP_POST_FILES['img7']['name']!="")
  89.                                         {
  90.                                             $nombre_archivo = "../images/botellas/" . $HTTP_POST_FILES['img7']['name'];
  91.                                             $img2 = $HTTP_POST_FILES['img7']['name'];
  92.                                             if ($rowFotos["img7"]!="")
  93.                                             {
  94.                                                 unlink("../images/botellas/" . $rowFotos["img7"]);
  95.                                             }
  96.                                             move_uploaded_file($HTTP_POST_FILES['img7']['tmp_name'], $nombre_archivo);
  97.                                             $cambios.=", img7='" . $img7 . "'";
  98.                                         }
  99.                                        
  100.                                         if ($HTTP_POST_FILES['img8']['name']!="")
  101.                                         {
  102.                                             $nombre_archivo = "../images/botellas/" . $HTTP_POST_FILES['img8']['name'];
  103.                                             $img2 = $HTTP_POST_FILES['img8']['name'];
  104.                                             if ($rowFotos["img8"]!="")
  105.                                             {
  106.                                                 unlink("../images/botellas/" . $rowFotos["img8"]);
  107.                                             }
  108.                                             move_uploaded_file($HTTP_POST_FILES['img8']['tmp_name'], $nombre_archivo);
  109.                                             $cambios.=", img8='" . $img8 . "'";
  110.                                         }
  111.                                        
  112.                                        
  113.                                         $sql = 'Update slider SET ' . $cambios . ' WHERE id= ' . $_POST["id"];
  114.                                         mysql_query($sql,$link) or die (mysql_error());
  115.                             ?>

al pulsar en actualizar me salta el mysql_error:

"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 ' nb1='jb', nb2='bombay', nb3='beefeater', nb4='cacique', nb5='smirnoff', nb6='pa' at line 1"

ayuda please...
  #2 (permalink)  
Antiguo 08/04/2010, 05:03
 
Fecha de Ingreso: marzo-2010
Ubicación: Barcelona
Mensajes: 657
Antigüedad: 14 años
Puntos: 26
Respuesta: Problema con UPDATE

en la primera linea $cambios contiene ya algo mas antes de que le incluyas todo eso? porque si no es asi sobra la primera ",".

tambien canvia:

Código PHP:
$sql 'select img1,img2,img3,img4,img5,img6,img7,img8 from slider WHERE id= ' $_POST["id"]; 
por:
Código PHP:
$sql "select img1,img2,img3,img4,img5,img6,img7,img8 from slider WHERE id= '" $_POST["id"]."'"
  #3 (permalink)  
Antiguo 08/04/2010, 05:12
Avatar de moronino  
Fecha de Ingreso: marzo-2010
Mensajes: 55
Antigüedad: 14 años
Puntos: 0
Respuesta: Problema con UPDATE

lo he puesto asi...

Código PHP:
Ver original
  1. $cambios="nb1=" . $_POST["nb1"] . "";
  2.                                         $cambios.=", nb2='" . $_POST["nb2"] . "'";
  3.                                         $cambios.=", nb3='" . $_POST["nb3"] . "'";
  4.                                         $cambios.=", nb4='" . $_POST["nb4"] . "'";
  5.                                         $cambios.=", nb5='" . $_POST["nb5"] . "'";
  6.                                         $cambios.=", nb6='" . $_POST["nb6"] . "'";
  7.                                         $cambios.=", nb7='" . $_POST["nb7"] . "'";
  8.                                         $cambios.=", nb8='" . $_POST["nb8"] . "'";
  9.                                        
  10.                                         $sql = "select img1,img2,img3,img4,img5,img6,img7,img8 from slider WHERE id= '" . $_POST["id"]."'";

Y ahora no me reconoce la columna de nb1 (Unknown column 'jb' in 'field list')
  #4 (permalink)  
Antiguo 08/04/2010, 05:40
Avatar de Eleazan  
Fecha de Ingreso: abril-2008
Ubicación: Ibiza
Mensajes: 1.879
Antigüedad: 16 años
Puntos: 326
Respuesta: Problema con UPDATE

Código PHP:
$cambios="nb1= '" $_POST["nb1"] . "'" 
Te faltan las comillas simples, no? º.º
  #5 (permalink)  
Antiguo 08/04/2010, 05:41
 
Fecha de Ingreso: marzo-2010
Ubicación: Barcelona
Mensajes: 657
Antigüedad: 14 años
Puntos: 26
Respuesta: Problema con UPDATE

Código PHP:
$cambios="nb1='" $_POST["nb1"]."'" 
Supongo que los nombres de las columnas de la tabla son nb1,nb2,nb3,etc.
  #6 (permalink)  
Antiguo 08/04/2010, 06:33
Avatar de moronino  
Fecha de Ingreso: marzo-2010
Mensajes: 55
Antigüedad: 14 años
Puntos: 0
Respuesta: Problema con UPDATE

Solucionado.

Me faltaban las comillas simples...

GRACIAS SOIS LOS MEJORES!!

Etiquetas: 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 20:19.