Ver Mensaje Individual
  #1 (permalink)  
Antiguo 24/07/2012, 16:44
tony_la09
 
Fecha de Ingreso: julio-2012
Mensajes: 22
Antigüedad: 11 años, 9 meses
Puntos: 3
Insertar varios registros dinamicos generados a partir de una consulta

Este dia les traigo una duda que me ha tenido con insomnio por un buen rato, lo que pasa es que deseo hacer un insert a varios registros traidos desde una consulta, tales insert son dinamicos, a continuacion les pongo el codigo fuente k manejo para que me entiendan mejor.

Código PHP:
Ver original
  1. <?php
  2. include ("conexion.php");
  3. $grupo = $_POST['grupo'];
  4. $consultar = "SELECT matricula,nombre_alumno,nombre_grupo FROM grupos WHERE nombre_grupo = '$grupo'";
  5. $resultado = mysql_query($consultar,$conex);
  6. $num_rows = mysql_num_rows($resultado);
  7. if ($num_rows <= 0){
  8. echo "El grupo ".$grupo." esta vacio<br><br>";
  9. echo "Pulse <a href='frmRegistrar_Calificaciones.php'>aqui</a> si deseas buscar otro Grupo.<br><br>";
  10. echo "Pulse <a href='Inicio.php'>aqui</a> si desea ir a la pagina principal.";
  11. echo "<br><br><br>";
  12. include ("pie.php");
  13. mysql_close($conex);
  14. }elseif ($num_rows > 0){
  15. echo "<form action='Guardar_Calificaciones.php' method='POST'>";
  16. echo "\n<table align='center'>";
  17. echo "\n<tr><th>NOMBRE</th><th>L</th><th>W</th><th>G</th><th>S</th><th>A</th><th>R</th><th>PROM</th><th>NIV</th><th>MOD</th><th>PER</th><th>HRS</th><th>GRUPO</th></tr>";
  18. while ($fila = mysql_fetch_array($resultado))
  19. {
  20. echo "\n<input type='hidden' name='matricula[]' value='" . $fila["matricula"] . "'>";
  21. echo "<tr>";
  22. echo "<td><input type='text' name='nombre_alumno[]' value='" . $fila["nombre_alumno"] ."' readonly></td>";
  23. echo "<td><input type='text' name='L[]' value='' size='3'></td>";
  24. echo "<td><input type='text' name='W[]' value='' size='3'></td>";
  25. echo "<td><input type='text' name='G[]' value='' size='3'></td>";
  26. echo "<td><input type='text' name='S[]' value='' size='3'></td>";
  27. echo "<td><input type='text' name='A[]' value='' size='3'></td>";
  28. echo "<td><input type='text' name='R[]' value='' size='3'></td>";
  29. echo "<td><input type='text' name='promedio[]' value='' size='3'></td>";
  30. echo "<td><input type='text' name='nivel[]' value='' size='3'></td>";
  31. echo "<td><input type='text' name='modulo[]' value='' size='3'></td>";
  32. echo "<td><input type='text' name='periodo[]' value='' size='3'></td>";
  33. echo "<td><input type='text' name='horas[]' value='' size='3'></td>";
  34. echo "<td><input type='text' name='grupo[]' value='" . $fila["nombre_grupo"] . "' size='3'></td>";
  35. echo "</tr>";
  36. }
  37. echo "</table>";
  38. echo "<table align='center'>";
  39. echo "<tr><td><input type='submit' name='btnGuardar' value='GUARDAR'></td></tr>";
  40. echo "</table>";
  41. echo "</form>";
  42. }
  43. ?>

y el codigo que manejo para realizar el insert de los datos es el siguiente

Código PHP:
Ver original
  1. <?php
  2. include ("conexion.php");
  3. for($i=0;$i<$_POST['matricula'];$i++){
  4. $matricula = $_POST['matricula'][$i];
  5. $nombre_alumno = $_POST['nombre_alumno'][$i];
  6. $L = $_POST['L'][$i];
  7. $W = $_POST['W'][$i];
  8. $G = $_POST['G'][$i];
  9. $S = $_POST['S'][$i];
  10. $A = $_POST['A'][$i];
  11. $R = $_POST['R'][$i];
  12. $promedio = $_POST['promedio'][$i];
  13. $nivel = $_POST['nivel'][$i];
  14. $modulo = $_POST['modulo'][$i];
  15. $periodo = $_POST['periodo'][$i];
  16. $horas = $_POST['horas'][$i];
  17. $grupo = $_POST['grupo'][$i];
  18. $consulta = "INSERT INTO Calificaciones (matricula,nombre_alumno,listening,writing,grammar,speaking,attitude,reading,promedio,nivel,modulo,periodo,horas,grupo)
  19.                 VALUES ($matricula,'$nombre_alumno',$L,$W,$G,$S,$A,$R,$promedio,$nivel,$modulo,'$periodo',$horas,'$grupo')";
  20. $resultado = mysql_query($consulta,$conex);
  21. echo $consulta;
  22. echo "SE GUARDO CON EXITO";
  23. mysql_close($conex);}
  24. ?>


El codigo de insercion lo tome de un post anterior, solo que no me funciono, por eso acudo a ustedes..

http://www.forosdelweb.com/f18/insertar-multiples-registros-dinamicos-763571/