mysql - Counter increasing in refresh and in reload in php

720

I have set a counter in php code to increment the id value in mysql on every next click but when I refresh or reload the page the value is increasing automatically is there any solution for this problem or any other substitute.

    <?php
    $db = mysqli_connect('localhost','root','root','rahul');

    $questions ="";
    $msg2 ="";
    $o1  ="" ;
    $o2  ="" ;
    $o3  ="" ;
    $o4  ="" ;
    $disable = "";
    $disable2 = "";

    session_start();

    if(empty($_SESSION['count']))
            $_SESSION['count'] = 0;     

    if(isset($_POST['sub1'])){      
       $ans = $_POST['ans'];
       $email = "[email protected]";          
       $order =  $_SESSION['count']+1;     
       echo $order;
       $_SESSION['count'] =  $order;

       $sql = (" SELECT * FROM qna WHERE id = $order ");

        $query = mysqli_query($db, $sql);
        $row=mysqli_fetch_array($query, MYSQLI_ASSOC);

        $questions = $row['questions'];
        $o1 = $row['o1'];
        $o2 = $row['o2'];
        $o3 = $row['o3'];
        $o4 = $row['o4'];

        $disable="";

    if($_SESSION['count']>5)
     {
    $disable = "disabled";
     }
    $disable2 = "";
        if($_SESSION['count']<=1)
     {
    $disable2 = "disabled";
     }

    //$sql2 =   "INSERT INTO result (id, answer, email) VALUES ('', '$ans', '$email') ".mysqli_error();
    /*
    $sql3 =   mysqli_query($db, "INSERT INTO result (answer, email) VALUES ('$ans', '$email')");

    if(mysqli_affected_rows($sql3)== true)
    {
        echo "inserted";
    }
    else
    {
        echo "not inserted";
    }
    */
    echo $ans. $email;    

     } 

     $sql4 = mysqli_query("select * from result");
     $row = mysqli_fetch_array($db, $sql4);
    // while()
     echo $row['id'];
     for($i=1;$i<=5;$i++)
     {

     }
    ?>

     <?php 

     if(isset($_POST['sub2'])){     
        $result2 = $_SESSION['count']-1;
        $_SESSION['count'] = $result2;    

     $sql = (" SELECT * FROM qna WHERE id = $result2 ");

        $query = mysqli_query($db, $sql);
        $row=mysqli_fetch_array($query, MYSQLI_ASSOC);
        $questions = $row['questions'];
        $o1 = $row['o1'];
        $o2 = $row['o2'];
        $o3 = $row['o3'];
        $o4 = $row['o4'];


    if($_SESSION['count']<=1){
       $disable2 = "disabled";
    }     
 }
 session_write_close();
?>
     <?php 
     if(isset($_POST['start'])){
        $order =  $_SESSION['count']+1;     
        echo $order;
        $_SESSION['count'] =  $order;      
        $sql = (" SELECT * FROM qna WHERE id = 1 ");        
        $query = mysqli_query($db, $sql);

        $row = mysqli_fetch_array($query, MYSQLI_ASSOC);
        $questions = $row['questions'];
        $o1 = $row['o1'];
        $o2 = $row['o2'];
        $o3 = $row['o3'];
        $o4 = $row['o4'];
        $disable="";

    if($_SESSION['count']>=5)
     {
    $disable = "disabled";
     }
    $disable2 = "";
        if($_SESSION['count']<=1){
           $disable2 = "disabled";
       }
    session_write_close();
     } 

     ?>

    <center><br><br><br>
    <form method="post">
    <input type="submit" name="start" value="start">
    </form>

    <a href="logout.php">Log out</a>
    <form action="" method="post" >
    <table border="1" height="300px" width="500px">
    <tr>
    <th colspan="2"><?php echo $questions; ?></th>
    </tr>

    <tr>
    <td><input type="radio"  name="ans" id="ans" value="<?php echo $o1; ?>"><?php echo $o1; ?></td>
    <td><input type="radio"  name="ans" value="<?php echo $o2; ?>"><?php echo $o2; ?></td>
    </tr>

    <tr>
    <td><input type="radio"  name="ans" value="<?php echo $o3; ?>"><?php echo $o3; ?></td>
    <td><input type="radio"  name="ans" value="<?php echo $o4; ?>"><?php echo $o4; ?></td>
    </tr>

    <tr colspan="2">
    <td><center><input type="submit" name="sub1" value="next" <?php echo $disable ?>> </td>
    <td><center><input type="submit" name="sub2" value="previous" <?php echo $disable2 ?>>
    <input type="submit" name="submit3" value="submit" > </td>
    </tr>

    </form>
    </table>

     <?php
     if(isset($_POST['submit3']))
     {
         $ans = $_POST['ans'];
        $email = "dummy";

    //$sql2 =   "INSERT INTO result (id, answer, email) VALUES ('', '$ans', '$email') ".mysqli_error();

    $sql3 =   mysqli_query($db, "INSERT INTO result (answer, email) VALUES ('$ans', '$email')");

    if(mysqli_affected_rows($sql3)== true)
    {
        echo "inserted";
    }
    else
    {
        echo "not inserted";
    }
    echo $ans. $email;

     } 

    ?>
307

Answer

Solution:

when you are reloading a web-page, you are reloading its POST (and also GET) data as well if it's there. if you are submitting a form then the target page contains POST data in its header. so if you reload this page it's like you would have clicked the button again.

since you are already using a session there is a workaround:

add a hidden field with a micro-timestamp in your form. this micro-timestamp will be different every time your page gets loaded (per user) - but this "new" timestamp only get's posted when you use the button. when you just refresh the page, you are reloading with the old timestamp.
so you just need to save compare the last timestamp (saved in a session variable) with the currently posted timestamp. if they are equal - the page just got refreshed - if they are not equal, then you got a new timestamp which was sent by your form:

<?php
    session_start();

    if(!isset($_SESSION["timestamp"]))
        $_SESSION["timestamp"] = 0;

    if(!isset($_POST["timestamp"]))
        $_POST["timestamp"] = 0;

    // previous timestamp - saved in session variable:
    $prev_ts = $_SESSION["timestamp"];

    // currently posted timestamp:
    $post_ts = $_POST["timestamp"];

    if($prev_ts != $post_ts)
    {
        // code to increase your counter goes here.
        $feedback = "button pressed";
    }
    else
    {
        // do nothing when the page just got refreshed
        $feedback = "refreshed";
    }

    $_SESSION["timestamp"] = $post_ts;
?>

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>

        <?php echo $feedback; ?>

        <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST">
            <input type="hidden" name="timestamp" value="<?php echo microtime(); ?>">
            <input type="submit" name="go" value="count">
        </form>
    </body>
</html>

People are also looking for solutions to the problem: Rewrite a function in PHP for exploding an array in which doesn't give me this error:

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.