PHP - encuesta PHP

 
Vista:
Imágen de perfil de Antonio
Val: 61
Ha aumentado 1 puesto en PHP (en relación al último mes)
Gráfica de PHP

encuesta PHP

Publicado por Antonio (42 intervenciones) el 13/02/2018 18:03:13
hola a todos

estoy en un dilema con este proyecto, soy nuevo en PHP y estoy terminando un proyecto de encuestas solo que estoy muy atorado en una modificación que quiero hacer en el final del proyecto.

tengo mi código donde creo la encuesta.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<section id="form">
    <form action="crearPreguntas.php" class="contact_form" method="post">
        <ul>
            <li>
                <h2>Crear Encuesta</h2>
            </li>
            <li>
                <label for="user">Encuesta:</label>
                <input type="text" name="titulo" placeholder="Encuesta" required/>
 
            </li>
 
            <li>
                <label for="preguntas">Preguntas:</label>
                <input type="number" name="preguntas" placeholder="Preguntas" min="0" max="15" required/>
 
            </li>
            <li>
                <input type="submit" class="submit" value="Crear"/>
            </li>
        </ul>
    </form>
</section>

y después donde creo las preguntas

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
include 'header.php';
//Capturamos los datos que vienen por post
$titulo = $_POST["titulo"];
$preguntas = $_POST["preguntas"];
 
 
?>
<section id="form">
    <form action="insertarPregunta.php" class="contact_form" method="post">
        <h3><?php echo $titulo; ?></h3>
        <table>
            <?php
            //Clico for que recorre la cantidad de prguntas ingresadas
            for ($i=1; $i <= $preguntas; $i++){
 
            ?>
            <tr>
                <td>Preguntas <?php echo $i; ?> &nbsp;
                <input name="p<?php echo $i; ?>" type="text" size="100" maxlength="100"></td>
 
            </tr>
            <?php }?>
 
        </table><br><input class="submit" type="submit" value="Insertar">
          <input name="titulo" type="hidden" value="<?php echo $titulo; ?>">
          <input name="preguntas" type="hidden" value="<?php echo $preguntas; ?>">
    </form>
</section>
<style>
    #center{
        margin: -10px;
        padding: 0px 0px 20px;
 
    }
</style>
<center id="center">
    <a id="pagina1" href="javascript:window.history.back();">&laquo;volver Atras</a>
</center>

y guardo mi encuesta y mis preguntas.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
$titulo = $_POST["titulo"];
$preguntas = $_POST["preguntas"];
 
//conexion con Base de datos
require 'conexion.php';
//obtenemos la fecha del sistema
$fecha_actual = date("y-m-d");
 
//insertamos la nueva encuesta
 
$sql = "INSERT INTO encuestas(titulo, fecha) VALUES ('$titulo' ,'$fecha_actual')";
$sql = mysqli_query($conexion,$sql);
 
// Ahora obtenemos la id de la encuesta que acabamos de insertat
$sql = "SELECT id FROM encuestas ORDER BY fecha";
$sql = mysqli_query($conexion, $sql);
while ($row = mysqli_fetch_array($sql)){
    $id = $row["id"];
}
//recorremos las preguntas
for ($i = 1; $i <= $preguntas; $i++){
    //Obtenemos el texto de la pregunta
    $preg = p.$i;
    $pas = $_POST[$preg];
    $texto =  $pas;
 
    //y lo insertamos
    $sql = "INSERT INTO respuestas(texto, idenc) VALUES ('$texto' ,'$id')";
    $sql = mysqli_query($conexion,$sql);
}
header("Location: crearEncuesta.php");
 
?>

y por ultimo muestro mi cuestionario según la selección del usuario

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
 
//capturamos la variable que viene por post del archivo ver encuesta 
 
$opcion = $_POST["opcion"];
require 'conexion.php';
include 'header.php';
 
//seleccionamos la encuesta de acuerdo a la variable $opcion
$consulta = "SELECT * FROM encuestas WHERE id=$opcion";
 
//Ahora recorremos los datos Titulo, fecha, id de la encuesta seleccionada
 
$consulta = mysqli_query($conexion, $consulta);
while ($row = mysqli_fetch_array($consulta)){
    $titulo = $row["titulo"];
    $fecha = $row["fecha"];
    $id = $row["id"];
}
 
?>
<section id="form">
    <form action="inserVotacion.php" class="contact_form" name="form1" method="post">
        <table>
            <tr>
                <!--Mostramos el titulo de la encuesta-->
                <td colspan="2"> <h3><?php echo $titulo; ?></h3></td>
            <input type="hidden" name="id" value="<?php echo $id; ?>">
            </tr>
            <?php
            //consulta que captura el texto , id de la tabla respuestas
            $sql = "SELECT texto,id FROM respuestas WHERE idenc='$id'";
            $sql = mysqli_query($conexion,$sql);
            //ahora recorremos los datos texto, id que estan vinculadas a la cuenta seleccionada
            while ($row = mysqli_fetch_array($sql)){
                $texto = $row["texto"];
                $idres = $row["id"];
 
            ?>
 
 
            <tr>
              <!--  <td width="50"><input type="radio" name="opcion" value="<?php echo $idres; ?>" required</td>
                <td width="470"><?php echo $texto; ?></td>-->
                <td width="50"><?php echo $idres; ?></td>
                <td width="470"><?php echo $texto; ?></td>
                <td> SI <input type="radio" name="radio<?php echo $idres; ?>" value="SI"></td>
                <td> NO <input type="radio" name="radio<?php echo $idres; ?>" value="NO"></td>
                <td><textarea name="comentarios<?php echo $idres; ?>" rows="5" cols="20">Escribe aquí tus Hallazgos</textarea></td>
                <td><textarea name="acciones<?php echo $idres; ?>" rows="5" cols="20">Escribe aquí tus Acciones Correctivas</textarea></td>
            </tr>
            <?php } ?>
            <tr>
                <td>
                    <input class="submit" type="submit" value="Guardar"></input>
                <td>Esta encuesta esta&aacute; del <?php echo $fecha; ?></td>
                </td>
            </tr>
        </table>
    </form>
</section>
<center>
    <div id="paginador">
        <form action="grafico.php" method="post">
            <a id="pagina1" href="javascript:window.history.back();">&laquo; Volver Atras</a>
            <input type="hidden" name="opcion" value="<?php echo $opcion;?>">
            <input class="submit" type="submit" value="Ver"></input>
        </form>
    </div>
</center>
<?php
        include 'foother.php';
?>

sola mente tengo una duda al momento de guardar mi cuestionario.

en la parte de

1
2
3
4
5
6
7
8
9
10
<tr>
  <!--  <td width="50"><input type="radio" name="opcion" value="<?php echo $idres; ?>" required</td>
    <td width="470"><?php echo $texto; ?></td>-->
    <td width="50"><?php echo $idres; ?></td>
    <td width="470"><?php echo $texto; ?></td>
    <td> SI <input type="radio" name="radio<?php echo $idres; ?>" value="SI"></td>
    <td> NO <input type="radio" name="radio<?php echo $idres; ?>" value="NO"></td>
    <td><textarea name="comentarios<?php echo $idres; ?>" rows="5" cols="20">Escribe aquí tus Hallazgos</textarea></td>
    <td><textarea name="acciones<?php echo $idres; ?>" rows="5" cols="20">Escribe aquí tus Acciones Correctivas</textarea></td>
</tr>

como puedo hacer para guardar en mi base de datos, se que tiene que ir guardando pregunta por pregunta y respuesta en un siclo pero no se como realizar esto.:'(

estas son mis bases de datos

BD_encuestas
ID
Fecha
titulo

BD_preguntas
id
idenc
testo

BD_respuestas
id
id_pregunta
idenc
valor
accion
hallazfo
fecha
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder