Save radiobuttons enmysql and PHP from a dynamic survey

-1

I have the following question

I have a dynamic survey where the user places his questionnaire and his questions and saves them in the BD and in another page he calls them and shows the questions.

only that I have a question about how to save the answer that the user chose.

PHP Code:

<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="<?php echo $idres; ?>" value="0"></td> 
                <td> NO <input type="radio" name="<?php echo $idres; ?>" value="1"></td> 
                <td><textarea name="<?php echo $idres; ?>" rows="5" cols="20">Escribe aquí tus Hallasgos</textarea></td> 
                <td><textarea name="<?php echo $idres; ?>" rows="5" cols="20">Escribe aquí tus Acciones Correctivas</textarea></td> 
           </tr> 
            <?php } ?>

and this is my insert in the BD

PHP Code:

<?php 
$opcion = $_POST["opcion"]; 
require 'conexion.php'; 

//obtenemos el numero actual de votos para la opcion elegir 
//comprovamos si $opcion no esta vacio 
if(!empty($opcion)){ 
    $consulta = "SELECT votos FROM respuestas WHERE id=$opcion"; 
    $consulta = mysqli_query($conexion, $consulta); 
    while ($row = mysqli_fetch_array($consulta)){ 
        $votos = $row["votos"]; 
    } 
    //Incrementamos en uno los numeros votos totales 
    $votos = $votos +1; 
     
    //actualizamos la tabla respuesta 
    $consulta = "UPDATE respuestas SET votos = $votos WHERE id = $opcion"; 
    mysqli_query($conexion, $consulta); 
} 
header("Location: verEncuesta.php"); 
?>
    
asked by antonio sanchez 08.02.2018 в 19:33
source

1 answer

0

here is an example you can send your form by jquery serialize () or by the same form action = 'insert.php'

      <div class="form-group col-md-4">
        <label class="control-label">Sexo</label><br>
          <label class="radio-inline">
            <input type="radio" name="Datos[sexo]" class="flat-red" checked value="hombre">
            Hombre
          </label>
          <label class="radio-inline">
            <input type="radio" name="Datos[sexo]" class="flat-red" value="mujer">
            Mujer
          </label>
          <label class="radio-inline">
            <input type="radio" name="Datos[sexo]" class="flat-red" value="indistinto">
            Indistinto
          </label>
      </div>

When you receive it escaping the characters

$sexo = $this->conexion -> real_escape_string(strip_tags(stripslashes(trim($datos["sexo"]))));

or haci

$sexo = $datos["sexo"];

then you make your insertion insert into tabla (sexo) values('$sexo')

div class="form-group col-md-4">
                <label class="control-label">Sexo</label><br>
                  <label class="radio-inline">
                    <input type="radio" name="Datos[sexo]" class="flat-red" <?php if($perfil["sexo"] == 'hombre') echo "checked"; ?> value="hombre">
                    Hombre
                  </label>
                  <label class="radio-inline">
                    <input type="radio" name="Datos[sexo]" class="flat-red" <?php if($perfil["sexo"] == 'mujer') echo "checked"; ?> value="mujer">
                    Mujer
                  </label>
                  <label class="radio-inline">
                    <input type="radio" name="Datos[sexo]" class="flat-red" <?php if($perfil["sexo"] == 'indistinto') echo "checked"; ?> value="indistinto">
                    Indistinto
                  </label>
              </div>

haci you must accommodate yours

<div class="form-group col-md-4">
            <label class="control-label">Sexo</label><br>
              <label class="radio-inline">
                <input type="radio" name="Datos[opcion]" class="flat-red" <?php if($perfil["sexo"] == 'Si') echo "checked"; ?> value="Si">
                Si
              </label>
              <label class="radio-inline">
                <input type="radio" name="Datos[opcion]" class="flat-red" <?php if($perfil["sexo"] == 'No') echo "checked"; ?> value="No">
                No
              </label>
          </div>
    
answered by 08.02.2018 в 20:29