Ver Mensaje Individual
  #2 (permalink)  
Antiguo 01/03/2012, 07:05
Avatar de repara2
repara2
 
Fecha de Ingreso: septiembre-2010
Ubicación: München
Mensajes: 2.445
Antigüedad: 13 años, 7 meses
Puntos: 331
Respuesta: Insertar array asociativo con php y mysql ?????

Código PHP:
Ver original
  1. <?php
  2.  
  3. function creaSQL($datos)
  4. {
  5.     $sql = "INSERT INTO db.table (";
  6.     foreach($datos as $key => $value)
  7.     {
  8.         $sql .="'$key',";
  9.     }
  10.     $sql = substr($sql, 0, -1);
  11.     $sql .=") VALUES (";
  12.     foreach($datos as $key => $value)
  13.     {
  14.         $sql .="'$value',";
  15.     }
  16.     $sql = substr($sql, 0, -1);
  17.     $sql .= ");";
  18.    
  19.     return $sql;
  20. }
  21.  
  22. $datos = array('campo1' => 'valor1', 'campo2' => 'valor2');
  23. $sql = creaSQL($datos);
  24. echo $sql;
  25. //Conectar
  26. //Hacer query con $sql como parámetro
  27.  
  28. ?>
__________________
Fere libenter homines, id quod volunt, credunt.