Ver Mensaje Individual
  #9 (permalink)  
Antiguo 10/11/2015, 10:08
Avatar de chronos682
chronos682
 
Fecha de Ingreso: febrero-2004
Ubicación: Tunja - Boyacá - Colombia
Mensajes: 627
Antigüedad: 20 años, 2 meses
Puntos: 69
Respuesta: Añadir valores al insert automaticamente

Prueba este código, tienes que acomodarlo a tus necesidades, a mi me funciona muy bien, genera un archivo por cada tabla llamado insert seguido del nombre de la tabla y dentro del archivo está la conexión a la BD, el insert y el header location. Así sería la idea.

Código PHP:
Ver original
  1. <?php
  2.   $conex = mysqli_connect("$_REQUEST[server]","$_REQUEST[user]","$_REQUEST[pass]","$_REQUEST[database]") or die("Problemas en la conexion"); //Conexion BD
  3.   $db = $_REQUEST['database'];
  4.   $textfile = "";
  5.   $tablas = mysqli_query($conex,"SHOW tables"); //Recuperacion nombre de tablas
  6.   $mostrar = mysqli_num_rows($tablas); //Recuperacion numero total de tablas
  7.   for($f=1;$f <= $mostrar; $f++) //Repetir la creacion para todas las tablas
  8.   {
  9.     $nombre = mysqli_fetch_array($tablas); //Array nombre de tablas
  10.     $insertar = fopen('insert'.$nombre["Tables_in_".$db.""].'.php', "w+");
  11.     fwrite($insertar,"<?php
  12.  
  13.      ");
  14.     $textfile = "\$conexion = mysqli_connect(".$_REQUEST['server'].", ".$_REQUEST['user'].", ".$_REQUEST['pass'].", ".$_REQUEST['database'].") or die('problemas en la conexion')";
  15.     //$textfile = "\$conexion = mysqli_connect(Server, User, Pass, DB) or die('problemas en la conexion');\n\n";
  16.     fwrite($insertar,$textfile);
  17.     $columnas = mysqli_query($conex, "DESCRIBE ".$nombre["Tables_in_".$db.""]."");
  18.     $colu = mysqli_query($conex, "DESCRIBE ".$nombre["Tables_in_".$db.""]."");
  19.     $contar = mysqli_num_rows($columnas);
  20.     $contar2 = mysqli_num_rows($colu);
  21.     $textfile = "INSERT INTO ".$nombre['Tables_in_'.$db]." (";
  22.     while($nombrecol=mysqli_fetch_array($colu))
  23.     {
  24.         $textfile = $textfile.$nombrecol['Field'].", ";
  25.     }
  26.     $textfile = substr($textfile, 0, -2);
  27.     $textfile = "mysqli_query(\$conexion, \"".$textfile.") VALUES ('\$_REQUEST[tituloproyecto]','\$_REQUEST[descripcion]','\$_REQUEST[idproyectos]')\"";
  28.     $textfile .= " or die(\"Problemas en el select\".mysqli_error(\$conexion));\n\n";
  29.     fwrite($insertar,$textfile);
  30.     $textfile = "header('Location: listar".$nombre["Tables_in_".$db.""].".php');
  31.    ?>";
  32.     fwrite($insertar, $textfile);
  33.     fclose($insertar);
  34.   }
  35. ?>
__________________
Si te gustó la respuesta dale +1

HERNÁN G. SIABATO M.
[email protected]