Php不能正常工作


Php not working in form

更新由于某种原因,我的页面没有从表单中提取信息,它希望保持所有字段为空,即使选中它也应该更改QuestionOptionId的值。我想让它做的是,当你选中单选按钮时,它会将值保存到会话中,这样以后就可以像测验一样提交了。出于某种原因,我的代码不会更改值,甚至不会获得QuestionOptionId 的值

下面的代码

<?php
session_start();
//check if the user is already logged in.
if (!isset($_SESSION['username'])) {
    header('Location: login.php');
}
$QuestionOptionId = "";
//get value from post data and store into session
if (isset($_POST['QuestionOptionId'])){
    $_SESSION['QuestionOptionId'] = $_POST['QuestionOptionId'];
}
//get back from session
if (isset($_SESSION['QuestionOptionId'])){
    $QuestionOptionId = $_SESSION['QuestionOptionId'];
}
print $_SESSION['username'];
?>
<?php
$con=mysqli_connect("localhost","root","root","root") or die();
                //execute query
                $query ="SELECT UserId from user where username = '".$_SESSION['username']."'";     
                $result=mysqli_query($con, $query);
                $UserId=$result->num_rows;
while ($row = $result->fetch_assoc()) {  
   echo $row['UserId']; 
   $UserId=$row['UserId'];//etc...
 }
?>
<body>
    <header>
        <p class="text-center">
            Welcome :<?php echo $_SESSION['username'];
                            echo $UserId;
                    ?>
                </p>
                <?php 
                        $query="SELECT * from testview";
                        $res=mysqli_query($con, $query);
                        $rows=$res->num_rows;
                            $i=1;
                        while($result=mysqli_fetch_array($res))
                {?>
                   <?php if($i==1){?>
                   <div id='question<?php   echo $i;?>' class='cont'>
                   <form name="QuestionTime"Method="post" action="push.php">
                  <p class='questions' id="qtext<?php echo $i;?>"> <?php echo $i?>.<?php echo $result['QuestionText'];?></p>
                  <input <?php if ($QuestionOptionId=='3'){ echo 'checked="checked"';} ?>type="radio" value="1" id='radio1_<?php echo $result['QuestionOptionId'];?>'name="1"/>
                  <?php
                  $query="SELECT OptionChoiceName FROM testview WHERE OptionChoiceId=3";
                        $res=mysqli_query($con, $query);
                        $rows=$res;
                            $i=1;
                          while($result=mysqli_fetch_array($res))
                 echo  $result['OptionChoiceName'];?>
                  <input <?php if ($QuestionOptionId=='4'){ echo 'checked="checked"';} ?> type="radio" value="2"id='radio1_<?php echo $result['QuestionId'];?>'name="1"/>
                  <?php
                  $query="SELECT OptionChoiceName FROM testview WHERE OptionChoiceId=4";
                        $res=mysqli_query($con, $query);
                        $rows=$res;
                            $i=1;
                          while($result=mysqli_fetch_array($res))
                 echo  $result['OptionChoiceName'];
                 ?>
<br><br>Selected Value: <?php echo $QuestionOptionId; ?>
                  <br/>
                  <?php  
                    $_SESSION['UserId']=$UserId;
                  ?>
                 <button id='next<?php echo $i;?>' class='next btn btn-success' type='submit'>Finish</button>
                 </div> 
                    <?php } $i++;} ?>
                    </form>
                    </div>
                    </div>
                    </header></header>

任何帮助都将非常感谢

您没有为QuestionOptionId&CCD_ 2。尝试使用此查询-

SELECT OptionChoiceName, QuestionOptionId, QuestionId FROM testview WHERE OptionChoiceId=3

这应该是-

if(isset($_SESSION['QuesitonOptionId']))
{
    $QuestionOptionId= $_SESSION['QuesitonOptionId'];
}