ayuda, como insertar varios registros en una sola tabla?

gissmo

Bovino adicto
#1
buenas noches, hola estoy estudiando ingenieria en sistemas y necesito ayuda al guardar registros en una bd, estoy usando html, php, mysql, y xampp.

tengo que insertar una pregunta en una tabla, con varias posibles respuestas, la pregunta ya la guardo sin problemas, el problema es a la hora de guardar varias respuestas en una tabla.

esto lo e intentado con un ciclo for pero no encuentro la salida, si alguien me puede echar la mano, muchas gracias.

a continuacion les pongo mis codigos, el problema en si lo tengo de la tercer pagina a la cuarta pagina.

primer pagina -->preguntas_alta_formu.php
<?php
$conectar=mysql_connect("localhost","root","") or die ("Problemas en el servidor".mysql_error());
mysql_select_db ("sistemas",$conectar) or die ("Problemas en la BD".mysql_error());
$registros=mysql_query("select * from indice",$conectar) or die ("Problemas en el Select".mysql_error());
$numregistros=mysql_num_rows($registros);//numero de registros existentes
$indice=$numregistros + 1;//siguiente registro
?>
<html>
<head>
<title>Formulario de nuevas preguntas</title>
<style type="text/css">
.oro
{
color: #ff3;
}
</style>
</head>
<body bgcolor="#2C7127">
<h2 class="oro">PREGUNTA NUEVA</h2>
<form name="alta" method="post" OnSubmit="return validar(this)" action="preguntas_alta_codigo.php">
<div>
<table border="1">
<tr>
<td class="oro">PREGUNTA #</td><td><input type="text" name="nopreg" size="1" readonly="readonly" value="<?php echo $indice ?>"></td>
</tr>
<tr>
<td class="oro">PREGUNTA</td><td><input type="text" name="preg" size="60"></td>
</tr>
</table>
<table width="200" border="1">
<tr>
<td><div align="center" class="blue"><input name="alta" type="submit" class="blue" value="enviar"></div></td>
<td><div align="center" class="blue"><input name="borrar" type="reset" class="blue" value="borrar"></div></td>
</tr>
</table>
</div>
</form>
</body>
</html>
<script language="javaScript">
function validar(alta)
{
if(alta.preg.value.length==0)
{
alert("Campo pregunta vacio");
alta.preg.focus();
return(false);
}
if (confirm("¿Desea guardar el registro?"))
{
enviar();
}
else
{
alta.reset();
return(false);
}
}
</script>
segunda pagina -->preguntas_alta_codigo.php
<?php
$conexion=mysql_connect("localhost","root","") or die ("problemas en la conexion");
mysql_select_db("sistemas", $conexion) or die ("problemas en la seleccion de la BD");
$nopreg = $_POST['nopreg'];
$preg = $_POST['preg'];
mysql_query("insert into indice(clavein, nombrein) values ('$_REQUEST[nopreg]', '$_REQUEST[preg]')", $conexion) or die ("Problemas en el select1".mysql_error());
?>
<html>
<head>
<title>Coneccion de php con mysql</title>
<style type="text/css">
.oro
{
color: #ff3;
}
.blanco
{
color: #fff;
}
</style>
</head>
<body bgcolor="#2C7127">
<h5 class="oro">PREGUNTA INSERTADA CORRECTAMENTE</h5>
<form name="alta" method="post" OnSubmit="return validar(this)" action="respuestas_alta_formu.php">
<table class="oro" border="1">
<tr>
<td>PREGUNTA #</td><td><input type="text" name="nopreg" size="1" value="<?php echo $nopreg; ?>" readonly="true">
<input type="hidden" name="nopreg" value="<?php echo $nopreg; ?>"></td>
</tr>
<tr>
<td>PREGUNTA</td><td><input type="text" name="preg" size="60" value="<?php echo $preg; ?>" readonly="true"></td>
</tr>
<tr>
<td># DE RESPUESTAS</td><td><input type="text" name="terminobusqueda">
<input type="hidden" value="<?php echo $terminobusqueda; ?>"></td>
</tr>
</table>
<table width="200" border="1">
<tr>
<td><div align="center" class="blue"><input name="alta" type="submit" class="blue" value="INSERTAR RESPUESTA"></div></td>
</tr>
</table>
</form>
</body>
</html>
tercer pagina -->rspuestas_alta_formu.php
<?php
$nopreg=$_POST['nopreg'];
$terminobusqueda=$_POST['terminobusqueda'];
?>
<html>
<head>
<title>Nuevas respuestas</title>
<style type="text/css">
.oro
{
color: #ff3;
}
.blanco
{
color: #fff;
}
</style>
</head>
<body bgcolor="#2C7127">
<h2 class="oro">NUEVAS RESPUESTAS</h2>
<form name="alta" method="post" OnSubmit="return validar(this)" action="respuestas_alta_codigo.php">
<table class="oro" border="1">
<tr>
<td>PREGUNTA #</td><td><input type="text" name="nopreg" size="1" value="<?php echo $nopreg; ?>" readonly="true">
<input type="hidden" name="nopreg" value="<?php echo $nopreg; ?>"></td>
</tr>
<tr>
<td>PREGUNTA</td><td><input type="text" name="preg" size="60" value="<?php echo $preg; ?>" readonly="true"></td>
</tr>
<tr>
<td># RESPUESTAS</td><td><input type="text" name="terminobusqueda" size="1" value="<?php echo $terminobusqueda; ?>" readonly="true">
<input type="hidden" value="<?php echo $terminobusqueda; ?>"></td>
</tr>
</table>
<table class="oro" border="1">
<tr><th>RESPUESTA #</th><th>RESPUESTA</th><th>ESTATUS</th><th>COMENTARIOS</th></tr>
<?php
for($i=1; $i<=$terminobusqueda; $i ++)
{
?>
<tr>
<td align="center">
<input type="text" name="numero<?php echo $i; ?>" value="<?php echo $i; ?>" size="1"></td>
<td><input type="text" name="respuestare<?php echo $i; ?>" size="70"></td>
<td><select name="estatusre<?php echo $i; ?>">
<option value=""></option>
<?php
$conexion=mysql_connect("localhost","root","");
mysql_select_db("sistemas", $conexion) or die ("problemas en la bd" .mysql_error());
$registro=mysql_query("select * from estatusrespuesta", $conexion) or die ("problemas en el select" .mysql_error());
while ($reg=mysql_fetch_array($registro))
{
echo "<option value=\"$reg[claveesresp]\">$reg[nombreestaresp]</option>";
}
?>
</select></td>
<td><input type="text" name="comentariore<?php echo $i; ?>" value="NINGUNO"></td>
</tr>
<?php
}
?>
</table>
<table width="200" border="0">
<tr>
<td><div align="center" class="blue"><input name="alta" type="submit" class="blue" value="enviar"></div></td>
<td><div align="center" class="blue"><input name="borrar" type="reset" class="blue" value="borrar"></div></td>
</tr>
</table>
</form>
</body>
</html>
cuarta pagina -->respuestas_alta_codigo.php
<?php
$conexion=mysql_connect("localhost","root","") or die ("problemas en la conexion");
mysql_select_db("sistemas", $conexion) or die ("problemas en la seleccion de la base de datos");
$nopreg=$_POST['nopreg'];
$terminobusqueda=$_POST['terminobusqueda'];
for($i=1; $i<=$terminobusqueda; $i ++)//obtener valores del arreglo anterior
{
$respuestare=$_POST['respuestare'.$i];
$estatusre=$_POST['estatusre'.$i];
$comentariore=$_POST['comentariore'.$i];
}
for($i=1; $i<=$terminobusqueda; $i ++)//mostrar arreglo
{
echo "respuesta # ".$i."<br>";
echo "respuesta ".$respuestare.$i."<br>";
echo "estatus ".$estatusre.$i."<br>";
echo "comentarios ".$comentariore.$i."<br>";
}
//insertar arreglo aqui
?>
<html>
<head>
<title>insertar respuestas</title>
<style type="text/css">
.oro
{
color: #ff3;
}
.blanco
{
color: #fff;
}
</style>
</head>
<body bgcolor="#2C7127">
<form name="alta" method="post" OnSubmit="return validar(this)" action="respuestas_alta_formu.php">
<p class="oro">NUMERO DE PREGUNTA <input type="text" name="nopreg" value="<?php echo $nopreg; ?>" readonly="true">
<input type="hidden" name="nopreg" value="<?php echo $nopreg; ?>">
</p>
<p class="oro">La respuesta se inserto exitosamente</p>
<p><a href="http://localhost/sistemas/respuestas_alta_formu.php" target="mainFrame" class="oro">OTRA RESPUESTA</a></p>
<p><a href="http://localhost/sistemas/cuerpo.html" target="mainFrame" class="oro">FINALIZADO</a></p>
<table width="200" border="0">
<tr>
<td><div align="center" class="blue"><input name="alta" type="submit" class="blue" value="enviar"></div></td>
<td><div align="center" class="blue"><input name="borrar" type="reset" class="blue" value="borrar"></div></td>
</tr>
</table>
</form>
</body>
</html>
 

dgr182

Bovino maduro
#2
Yo lo que te recomiendo en tu caso es que envies por el metodo POST todas las variables de las respuestas, las guardas en un Arreglo en la siguiente pagina donde las recibes, claro solo las que no estén vacias, y utilizas la funcion "foreach"
http://php.net/manual/en/control-structures.foreach.php
Para arreglos, y dentro de la funcion guardas la respuesta para el ID de pregunta que se esta manejando. Claro en tu tabla de respuestas que se relacione con ese ID de pregunta.

Espero te sirva la idea.
 
Arriba