doudun3910 2015-10-14 22:55
浏览 28

更新随机选择的行

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.

  • 写回答

2条回答 默认 最新

  • duanhu7390 2015-10-14 23:00
    关注

    First I think you need to add a field in your form to identify which question is relevant for the submission:

    <form action="" method="post">
    <input type="hidden" name="pergunta" value="<?= $info['pergunta'] ?>" />
    <input type="submit" class="botao-css" value="Sim" name="sim"/><br/>
    <input type="submit" class="botao-css" value="Não" name="votarnao"/><br/>
    </form>
    

    Then you need to use this instead, but to prevent SQL injections, you should make sure it can only be an integer:

    mysqli_query($connection,"UPDATE enquete SET sim = sim + 1 WHERE pergunta = ".(int) $_POST['pergunta']) ;
    

    $info will be the information about the next question obtained from conexao.php. $_POST will be the data from the last submission. So it makes sense to not use $info at all if you're referring to the last question.

    Note: if pergunta isn't an integer, you should use a prepared statement to protect against SQL injections. (maybe replace pergunta with id in this case)

    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线