我创建了一个页面,其中有两个按钮接下来,前一个接下来是增加会话值,之前是减少会话值,但是当我刷新或重新加载页面时会话值增加和减少这是问题,我需要会话值增加和减少只有当我按下一个/上一个按钮才有办法分开两个会话。
<?php
$db = mysqli_connect('localhost','root','root','rahul');
session_start();
if(empty($_SESSION['count']))
$_SESSION['count'] = 0;
if(isset($_POST['sub1'])) {
$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);
$disable="";
$disable2="";
if($_SESSION['count'] >= 5) {
$disable = "disabled";
}
if($_SESSION['count']<=1) {
$disable2 = "disabled";
}
echo '<form action="" method="post">
<table border="1" height="300px" width="500px">
<tr>
<th colspan="2">Q'. $row['id'].": ".$row['questions'].'</th>
</tr>
<tr>
<td><input type="radio" value="" name="ans">'.$row["o1"].'</td>
<td><input type="radio" value="" name="ans">'.$row['o2'].'</td>
</tr>
<tr>
<td><input type="radio" value="" name="ans">'.$row['o3'].'</td>
<td><input type="radio" value="" name="ans">'.$row['o4'].'</td>
</tr>
<tr colspan="2">
<td><center><input type="submit" name="sub1" value="next" ' .$disable.'> </td>
<td><center><input type="submit" name="sub2" value="previous" ' .$disable2.'></td>
</tr>
</form>
</table>
';
}
?>
<?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);
$disable2 = "";
if($_SESSION['count'] <= 1) {
$disable2 = "disabled";
}
if($_SESSION['count'] >= 5) {
$disable = "disabled";
}
echo '<form action="" method="post" >
<table border="1" height="300px" width="500px">
<tr>
<th colspan="2">Q'. $row['id'].": ".$row['questions'].'</th>
</tr>
<tr>
<td><input type="radio" value="" name="ans">'.$row["o1"].'</td>
<td><input type="radio" value="" name="ans">'.$row['o2'].'</td>
</tr>
<tr>
<td><input type="radio" value="" name="ans">'.$row['o3'].'</td>
<td><input type="radio" value="" name="ans">'.$row['o4'].'</td>
</tr>
<tr colspan="2">
<td><center><input type="submit" name="sub1" value="next" > </td>
<td><center><input type="submit" name="sub2" value="previous" ' .$disable2.'></td>
</tr>
</form>
</table>
';
}
?>
<center><br><br><br>
<form method="post">
<input type="submit" name="sub1" value="start">
</form>
<a href="logout.php">Reset</a>