Tema: foreach
Ver Mensaje Individual
  #1 (permalink)  
Antiguo 21/07/2009, 13:02
Said012784
 
Fecha de Ingreso: julio-2009
Mensajes: 13
Antigüedad: 14 años, 10 meses
Puntos: 0
foreach

Hola a todos, tengo un problema estoy haciendo un sistema para encuestas, donde el usuario introduce el nombre de la encuesta y el numero de preguntas a realizar dentro de esta, despues me manda a otro formulario donde se guarda la encuesta en una bd mysql, y en ese mismo formulario creo dos input text uno para colocar la pregunta y otro para colocar el numero de respuestas de la pregunta

Código PHP:
<?php /* Created on: 20/07/2009 */      
require('conexion_bd.php');    
    
$tit $_POST["tit"];
    
$tot_preg $_POST["tot_preg"];     

    
//Obtenemos la fecha del sistema
    
$fecha date("d-m-Y");
    
    
$sql "INSERT INTO encuestas (titulo, total_preg, fecha) VALUES ('$tit', '$tot_preg', '$fecha') ";
    
$sql mysql_query($sql) or die ("No fue posible agregar la encuesta");    
                         
    
$sqla "SELECT total_preg FROM encuestas WHERE id_titulo=(SELECT max(id_titulo) FROM encuestas)";
    
$sqlb mysql_query($sqla) or die ("No fue posible seleccionar la encuesta");
    
$row=mysql_result($sqlb0);
    
$tot=$row;
    
            
           
?>
<html>
    <head> <title>  </title> </head>
    <body bgcolor="#A6BDD4">
        <br>
        <form action="encuesta3.php" method="post">
                 <table width=35% border="0" align=center cellpadding=0 background="engranes.jpg">
                    <tr> <td colspan="2"> <font color="#0662AD"> <b><div style="text-align: center">Registro de Preguntas</div></b> </font> </td> </tr>
                    <tr> <td colspan="2"> <br> <br></td> </tr> 
                    <?php
                          
for($i=1;$i<=$tot;$i++){ 
                        
?>  
                    
                    <tr> 
                        <td align="center" height="4" width="50%"> <font color="#0662AD"> <b>Pregunta<?php echo $i?></b> </font> </td> 
                        <td align="left" height="4" width="50%"><input type="text" name="p[]" size="20%"></td>
                    </tr>
                    <tr> 
                        <td align="center" height="4" width="50%"> <font color="#0662AD"> <b>Total de Respuestas <?php echo $i;?>:</b> </font> </td> 
                        <td align="left" height="4" width="50%"><input type="text" name="tot_resp[]" size="3%"></td>
                    </tr>
                    <tr> <td colspan="2"> <br> </td> </tr>
                    <?php ?>
                    <tr> <td colspan="2"> <br> </td> </tr>
                 <tr>
                    <td align="center" height="4" colspan="2"> <input type="submit" name="Guardar" value="Enviar"> </td>
                    
                </tr>
            
            </table> 
            

        </form>
            
    </body>
</html>
despues de esto en el siguiente formulario intento guardar los datos del los dos input text en la tabla preguntas
pero al hacer esto me duplica los registros y es por ke en el foreach hace las iteraciones necesarias, para que entiendan mejor:

Código PHP:
<?php /* Created on: 20/07/2009 */
require('conexion_bd.php');    

$sqld "SELECT total_preg FROM encuestas WHERE id_titulo=(SELECT max(id_titulo) FROM encuestas)";
$sqle mysql_query($sqld) or die ("No fue posible seleccionar la encuesta");
$rowf=mysql_result($sqle0);
$totg=$rowf;

$sqla "SELECT max(id_titulo) FROM encuestas";
$sqlb mysql_query($sqla);
$row=mysql_result($sqlb0);
$id=$row;

 
foreach(
$_POST['p'] as $preg)
{
    foreach (
$_POST['tot_resp'] as $tot)
    { 
        
$sql "INSERT INTO preguntas(id_titulo, pregunta, total_resp) VALUES ('$id' , '$preg', '$tot')";
        
$sql mysql_query($sql);
    }
}

?>
Como podria hacerle para resolver esto, agradezco su ayuda de antemano