Ver Mensaje Individual
  #10 (permalink)  
Antiguo 20/03/2015, 09:13
mensajeescrito
 
Fecha de Ingreso: mayo-2012
Mensajes: 760
Antigüedad: 11 años, 11 meses
Puntos: 5
Respuesta: Bucle foreach dentro de una sentencia sql

gnzsoloyo, el codigo que has puesto con el implode , lo he probado y no funciona no inserta.

Dice:

Warning: implode(): Invalid arguments passed in on line 43


pateketrueke, este es el codigo que tengo:

archivo 1:

Código HTML:
Ver original
  1. <form method="POST" action="2---insertar_parte_2.php">
  2.        
  3.             <input type="text" name="nombre" placeholder="Nombre"/>
  4.            
  5.             </br></br>
  6.        
  7.             <input type="text" name="caracteristicas" placeholder="Caracteristicas"/>
  8.            
  9.             </br></br>
  10.        
  11.             <input type="text" name="antiguedad" placeholder="antiguedad"/>
  12.            
  13.             </br></br>
  14.            
  15.             <input type="submit" value="Insertar">
  16.                                  
  17.         </form>


archivo 2:

Código PHP:
Ver original
  1. include 'OperacionesMysql.php';
  2.  
  3.     $op = new OperacionesMysql();
  4.        
  5.  
  6.    
  7.     $nombre = $_POST['nombre'];
  8.     $caracteristicas = $_POST['caracteristicas'];  
  9.     $antiguedad = $_POST['antiguedad'];
  10.  
  11.  
  12.    
  13.     //CAMPOS OBLIGATORIOS QUE NO PUEDEN ESTAR VACIOS..................................................
  14.    
  15.     if ($nombre == '' or $caracteristicas == '' or $antiguedad == '')
  16.    
  17.         {
  18.             header('Location:2---insertar_parte_1.php');           
  19.         }
  20.        
  21.         else
  22.        
  23.         {  
  24.             //Lineas para solo poner el nombre de la tabla en el archivo "OperacionesMysql.php"
  25.             $nombre_tabla = $op->nombre_tabla; 
  26.            
  27.             $op->InsertarDatos($_POST['nombre'], $_POST['caracteristicas'], $_POST['antiguedad'], $nombre_tabla);                              
  28.                        
  29.             header('Location:2---insertar_parte_1.php');


archivo 3:

Código PHP:
Ver original
  1. class OperacionesMysql
  2.        
  3.         {
  4.    
  5.             private $servidor = 'localhost';
  6.             private $usuario = 'root';
  7.             private $pass = '';
  8.             private $basedatos = 'vehiculos';  
  9.    
  10.             public $nombre_tabla = 'coches';
  11.    
  12.             //public $nombre, $caracteristicas, $antiguedad;
  13.            
  14.             public $array_datos = array('$nombre', '$caracteristicas', '$antiguedad');
  15.  
  16.  
  17.  
  18.  
  19.  
  20.             public function ConectarBbdd()
  21.            
  22.             {
  23.                 $con = mysqli_connect($this->servidor, $this->usuario, $this->pass, $this->basedatos) or
  24.                 die ('Lo siento pero no conecta a la BBDD');
  25.                
  26.                 return $con;       
  27.             }
  28.  
  29.  
  30.  
  31.  
  32.  
  33.             function InsertarDatos($nombre_tabla, $array_datos)
  34.            
  35.                 {          
  36.                                    
  37.                 echo $sql="INSERT INTO " . $nombre_tabla . " (nombre, caracteristicas, antiguedad) VALUES (";
  38.                       foreach($array_datos as $datos) {
  39.                       $sql.=  $datos . ", "; }
  40.                       $sql.= ")";  
  41.                    
  42.                
  43.                    
  44.                     $con = $this->ConectarBbdd();
  45.                    
  46.                     mysqli_query($con, $sql) or die ('Lo siento pero no se inserta en BBDD' . mysqli_error($con));                                                         
  47.                 }  
  48.  
  49. }