How can I send more than one value of multiple Buttons radios to the database?

Asked

Viewed 57 times

0

The questions are all taken from the database, but I can’t send more than one, and when I send them, I don’t even enter them into the database.

Sending Code:

   <?php

    if (!isset($_SESSION)){session_start();}

    echo    $resposta=$_POST['resposta-1'];
    echo "<br>";
    echo   $user=$_POST['user']; echo "<br>";

    $inst0="Select Resposta from aluno_teste ";  

    $result0=mysqli_query($conn,$inst0);
    echo $numlinhas1=mysqli_num_rows($result0);
    //echo var_dump($_POST);
    if ($numlinhas1 == 0)
    {                   

        $resposta="Insert INTO aluno_teste(Resposta,) VALUES(' ','".$resposta."')";   

        if(!mysqli_query($conn,$inst0)){ 
            echo 'erro :'. mysqli_error($conn);
        }else{
            echo "<br>";
            echo 'Linhas alteradas: '. mysqli_affected_rows($conn);
        }

        $_SESSION['mensagem'] ="Respostas enviadas";
        //header("Location:IndexTestes.php");
    }

?>

Code of radio Buttons:

$instS='Select * from perguntas';
$query = mysqli_query($conn,$instS);

while ($row = mysqli_fetch_assoc ($query))
{
    echo"<br>";

    echo"<center><fieldset style='border:solid; width:500px;'>";

    echo" <legend style='background: #FF9; width:150px; border: solid 1px black; 
    -webkit-border-radius: 8px; -moz-border-radius: 8px; 
    border-radius: 8px; padding: 6px;'> Questão: ".$row['idPergunta']."";
    echo"</legend>";

    echo "<center> <h4><b> Pergunta: </h4></center></b>";

    echo"<center>"
        .$row['textoPerguntas'].""; //Buscar a pergunta a base de dados
    echo"<center>";

    echo " <h4><b> Resposta: </h4></b>";

    $radioname = "resposta-".$row['idPergunta'];
        //$items = Array($row['RespostaCorrecta'],$row['RespostaErrada1'],$row['RespostaErrada2'],$row['RespostaErrada3']);

    ?>              

    <fieldset name="pergunta-<?php echo $radioname; ?>" id="pergunta-<?php echo $radioname; ?>" >
        <?php
    //  echo " <input type='radio' required name='pergunta-".$radioname."' id='pergunta-".$radioname."' > ".$items[array_rand($items)].""; 
        echo " <input type='radio' required name='".$radioname."' id='".$radioname."' value='".$row['RespostaErrada3']."' > ".$row['RespostaCorrecta'].""; 
        echo "<br>";
        echo " <input type='radio' required name='".$radioname."' id='".$radioname."' value='".$row['RespostaErrada3']."' > ".$row['RespostaErrada1'].""; 
        echo "<br>";
        echo " <input type='radio' required name='".$radioname."' id='".$radioname."' value='".$row['RespostaErrada3']."' > ".$row['RespostaErrada2'].""; 
        echo "<br>";
        echo " <input type='radio' required name='".$radioname."' id='".$radioname."' value='".$row['RespostaErrada3']."' > ".$row['RespostaErrada3'].""; 
        echo "<br>";
        ?>
    </fieldset>
  <br>


    <?php 
        echo "</fieldset></center>"; 
}

    ?>
<center> 
    <button onClick='return confirm("Deseja enviar?");'  class='btn btn-default glyphicon glyphicon-ok' type='submit'> Enviar</button>
</center>                               
</form>

1 answer

0

Regarding the fact that you are not entering in the database, you should check the line where the query is built, the syntax is wrong.

The right would be something like:

$resposta = "Insert INTO aluno_teste(Resposta) VALUES('".$resposta."')";

Regarding the fact that "can’t send more than one" do you refer to the form Ubmit? That is, when you give Ubmit $_POST is not filled with the answers to all the questions?

  • Exactly. Because I have my radio Buttons with random names, and it gets complicated for me to be able to send a value ...

  • Paulo, you see, the random name of the radio Buttons has nothing to do with the fact that the values are not going via $_POST post post Ubmit, you know? What would justify this malfunction would be a syntax error for example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.