2016-04-13 98 views
0

我已經在php代碼中設置了一個計數器來增加每次下一次點擊時在mysql中的id值,但是當我刷新或重新加載頁面時,數值會自動增加是否有任何解決方案問題或任何其他替代品。計數器增加刷新和重新加載在PHP

<?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; 

    } 

    ?> 
+0

如果我理解你 - 重載/後你的頁面的刷新」點擊下一個意味着你再次點擊下一個,這增加了計數器當然 –

+0

是的,計數器每增加一次點擊,但也在每次刷新/重新加載@Lashane –

+0

我的意思是刷新/重新加載服務器意味着按鈕被點擊再次,所以計數器必須增加 –

回答

0

當您重新加載網頁時,您將重新加載其POST(以及GET)數據,以及它是否存在。如果您要提交表單,則目標頁面的標題中會包含POST數據。所以如果你重新加載這個頁面,就像你再次點擊了按鈕。

,因爲你已經在使用一個會話有解決方法:

添加一個隱藏字段,在您的形式的微時間戳。這個微時間戳每次加載頁面時都會有所不同(每個用戶) - 但是這個「新」時間戳只有在使用按鈕時纔會發佈。當您只刷新頁面時,您將重新加載舊時間戳。
所以你只需要保存比較上次的時間戳(保存在會話變量中)和當前發佈的時間戳。如果他們是平等的 - 頁剛剛刷新 - 如果他們不相等,那麼你有這是由你的形式發送新的時間戳:

<?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>