Foros del Web » Programando para Internet » PHP »

Insertar multiples filas con foreach

Estas en el tema de Insertar multiples filas con foreach en el foro de PHP en Foros del Web. Buenas tengo este codigo: Código PHP: //VERIFICAMOS DE NUEVO QUE SE SELECCIONO ALGUN ARCHIVO if (  $archivo  !=  "none"  ){       //CREAMOS NUESTRO INSERT      foreach(  $_POST [ 'tag' ] as  $valor  ) {      $qry  .=  ...
  #1 (permalink)  
Antiguo 06/03/2012, 08:25
Avatar de dvbeaumont  
Fecha de Ingreso: marzo-2011
Ubicación: Caracas
Mensajes: 145
Antigüedad: 13 años
Puntos: 1
Exclamación Insertar multiples filas con foreach

Buenas tengo este codigo:

Código PHP:
//VERIFICAMOS DE NUEVO QUE SE SELECCIONO ALGUN ARCHIVO
if ( $archivo != "none" ){

     
//CREAMOS NUESTRO INSERT
    
foreach( $_POST['tag'] as $valor ) {
    
$qry .= "INSERT INTO t_tag_articulo (id_tag, id_articulo) VALUES
    (  '"
.$valor."', '".$_POST["id_articulo"]."' )";";";  
                                }
                                
    
//NOS CONECAMOS A LA BASE DE DATOS
    //REMPLAZEN SUS VALOS POR LOS MIOS
mysql_connect("localhost"," "," ") or die("No se pudo conectar a la base de datos");
    
    
//SELECCIONAMOS LA BASE DE DATOS CON LA CUAL VAMOS A TRABAJAR CAMBIEN EL VALOR POR LA SUYA
mysql_select_db(" ");
    
    
//EJECUTAMOS LA CONSULTA
    
mysql_query($qry) or die("Query: $qry <br />Error: ".mysql_error());
    
    
//CERRAMOS LA CONEXION
    
mysql_close();
    
//NOTIFICAMOS AL USUARIO QUE EL ARCHVO SE HA ENVIADO O REDIRIGIMOS A OTRO LADO ETC.
    
echo "    <script language=javascript>
    alert('Comentario hecho satisfactoriamente')
    </script>"
;
    include_once (
"lista-articulos.php");
}else{
    echo 
"    <script language=javascript>
    alert('No fue posible cargar el comentario, intente de nuevo')
    </script>"
;
    include_once (
"lista-articulos.php");

Sucede que ingreso mediante un select multiple con name='tag[]'

Varios datos, parece reconocerlos pero arroja este error:

Query: INSERT INTO t_tag_articulo (id_tag, id_articulo) VALUES ( '1', '19' )INSERT INTO t_tag_articulo (id_tag, id_articulo) VALUES ( '2', '19' )INSERT INTO t_tag_articulo (id_tag, id_articulo) VALUES ( '3', '19' )
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 'INSERT INTO t_tag_articulo (id_tag, id_articulo) VALUES ( '2', '19' )INSER' at line 2

Quisiera saber cual es el problema de esa consulta para insertar varios datos
__________________
Sé parte de nuestro mundo creativo.

http://bbcreativos.com
  #2 (permalink)  
Antiguo 06/03/2012, 08:27
Avatar de OsSk4R  
Fecha de Ingreso: octubre-2006
Ubicación: $this->home
Mensajes: 824
Antigüedad: 17 años, 6 meses
Puntos: 74
Respuesta: Insertar multiples filas con foreach

Error de sintaxis:
( '".$valor."', '".$_POST["id_articulo"]."' )";";";
Esos dos sobran

Saludos,
  #3 (permalink)  
Antiguo 06/03/2012, 08:30
Avatar de dvbeaumont  
Fecha de Ingreso: marzo-2011
Ubicación: Caracas
Mensajes: 145
Antigüedad: 13 años
Puntos: 1
Respuesta: Insertar multiples filas con foreach

Ya modifiqué eso e igual sigue saliendo el error:

Query: INSERT INTO t_tag_articulo (id_tag, id_articulo) VALUES ( '2', '19' )INSERT INTO t_tag_articulo (id_tag, id_articulo) VALUES ( '3', '19' )
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 'INSERT INTO t_tag_articulo (id_tag, id_articulo) VALUES ( '3', '19' )' at line 2
__________________
Sé parte de nuestro mundo creativo.

http://bbcreativos.com
  #4 (permalink)  
Antiguo 06/03/2012, 09:21
Avatar de OsSk4R  
Fecha de Ingreso: octubre-2006
Ubicación: $this->home
Mensajes: 824
Antigüedad: 17 años, 6 meses
Puntos: 74
Respuesta: Insertar multiples filas con foreach

Código PHP:
Ver original
  1. mysql_connect("localhost","","") or die("No se pudo conectar a la base de datos");
  2.  
  3. //SELECCIONAMOS LA BASE DE DATOS CON LA CUAL VAMOS A TRABAJAR CAMBIEN EL VALOR POR LA SUYA
  4.  
  5. foreach($_POST['tag'] as $valor) {
  6.        $qry = mysql_query("INSERT INTO t_tag_articulo (id_tag, id_articulo) VALUES ('".$valor."', '".$_POST['id_articulo']."')") or die("Query: $qry <br />Error: ".mysql_error());
  7. }

Saludos,
  #5 (permalink)  
Antiguo 06/03/2012, 09:26
Avatar de dvbeaumont  
Fecha de Ingreso: marzo-2011
Ubicación: Caracas
Mensajes: 145
Antigüedad: 13 años
Puntos: 1
Respuesta: Insertar multiples filas con foreach

SIIIIIIIIIIIIIIIIIIIIII!!!!!! eresss el mejor!! graciass!! jajajaja no me habia percatado de eso!!! Mil gracias!
__________________
Sé parte de nuestro mundo creativo.

http://bbcreativos.com

Etiquetas: filas, foreach, multiples, mysql, sql, usuarios
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 13:01.