I am building a code that shows up a random question everytime you refresh the page or answer the question on screen. This is the code I have on my conexao.php file to select a random question:
$research = mysqli_query ($connection, "SELECT * FROM enquete ORDER BY RAND() LIMIT 1");
$result = mysqli_fetch_array($research);
$info = $result;
The code above is working fine to select a random question from my "enquete" table.
The problem I have is when the question is answered. This is the code I have that displays the question and presents 2 submit buttons (YES/NO):
<?php
include 'conexao.php';
$contador = 0;
do{
echo $info['pergunta'];
if(isset($_POST['sim'])){
mysqli_query($connection,"UPDATE enquete SET sim = sim + 1 WHERE pergunta = '".$info['pergunta']."'") ;
echo "<br/>".$info['sim']."people who answered YES <br/>";
echo $info['nao']." people who answered NO <br/>";
}
$contador++;
} while ($contador < 1);
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="estilo.css">
<link async href="http://fonts.googleapis.com/css?family=Aclonica"
rel="stylesheet" type="text/css"/>
</head>
<div class="wrapper">
<form action="" method="post">
<input type="submit" class="botao-css" value="Sim" name="sim"/><br/>
<input type="submit" class="botao-css" value="Não" name="votarnao"/><br/>
</form>
</div>
</html>
The problem I have is the following: The code above seems to be ok to add 1 to an answered question (YES in the example above), however instead of adding 1 to the current random question shown on screen it adds 1 to the next question which will be shown.
For example:
- Question 1: The sky is blue? (if YES is hit then it adds 1 to the next question "sim" row instead of adding to this one. )
- Question 2: Do you drink water? (it has already gotten 1 because of the previous question and if I think yes to this one the it adds 1 to the next question "yes" row)
Of course what I intend to do is to have the system to add 1 to "yes" row of the current question and not to the one that is next.
If someone could help me giving tips on this I would be really glad.