将变量从MYSQL发送到另一个PHP页面

时间:2014-08-22 09:56:22

标签: php mysql

我正在尝试使用PHP和MYSQL进行调查,但无法弄清楚如何将值从MYSQL值发送到另一个PHP页面。

我正在使用填充了来自相应MYSQL数据库的数据的单选按钮,当用户按下提交按钮时,我想将答案发送到另一个页面,以便我可以将其与其他答案进行比较。我已经使用了我自己填充的单选按钮并将它们发送到另一个页面,但是看起来似乎可以解决这个问题。

所以我的主要问题是你会怎么做?我曾尝试使用isset函数和查询字符串,但我再次使用php变量,但不能使用mysql数据填充变量。

以下代码是我用来向用户展示问题的代码:

 <?php
 require_once ('mysqli_connect.php');

$q = "SELECT question_id, question, option_1, option_2, option_3, option_4 FROM survey_db LIMIT         1";     
 $result = @mysqli_query ($dbcon, $q); // Run the query
if ($result) { // If it ran OK, display the records
// Table header
 echo '<table>
<tr>
<td><b> Please Answer All Questions In The Survey </b> </td>
<br>
</tr>';
// Fetch and print all the records
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo '<form action="answersurvey.php" method="GET">';
echo '<tr>
<tr>
<td>
<br>
<br>

<ol><li> '.$row['question'].'</li></ol>

<input type="radio"   name = "'.$row['question_id'].'" value= "1" />'.$row['option_1'].' 
<br>
<input type="radio"   name = "'.$row['question_id'].'" value= "2" />'.$row['option_2'].' 
<br>
<input type="radio"   name = "'.$row['question_id'].'" value= "3" />'.$row['option_3'].' 
<br>
<input type="radio"   name = "'.$row['question_id'].'" value= "4" />'.$row['option_4'].' 
<br>

</td>
</tr>';
}
echo '</table>

<input type ="submit" value="submit">

</form>'; // Close the table

?>

1 个答案:

答案 0 :(得分:0)

<?php
 require_once ('mysqli_connect.php');

$q = "SELECT question_id, question, option_1, option_2, option_3, option_4 FROM survey_db LIMIT         1";     
$result = @mysqli_query ($dbcon, $q); // Run the query
if ($result) {
?>
<form action="answersurvey.php" method="POST">
<table>
    <tr>
        <td><b> Please Answer All Questions In The Survey </b> </td>
    </tr>
<?php   
// Fetch and print all the records
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$question = $row['question'];
$question_id = $row['question_id'];
$option_1 = $row['option_1'];
$option_2 = $row['option_2'];
$option_3 = $row['option_3'];
$option_4 = $row['option_4'];
?>
<tr>
    <td>
        <br>
        <br>

        <ol><li><?php echo $question;?></li></ol>

        <input type="radio"   name = "question[<?php echo $question_id;?>]" value= "1" /><?php echo $option_1;?>
        <br>
        <input type="radio"   name = "question[<?php echo $question_id;?>]" value= "2" /><?php echo $option_2;?>
        <br>
        <input type="radio"   name = "question[<?php echo $question_id;?>]" value= "3" /><?php echo $option_3;?>
        <br>
        <input type="radio"   name = "question[<?php echo $question_id;?>]" value= "4" /><?php echo $option_4;?>
    <br>

    </td>
</tr>
<?php
}
?></table>

<input type ="submit" value="submit">

</form>

<?php
}
?>

<强> answersurvey.php

if(isset($_REQUEST['question']))
{
    $arr_question = $_REQUEST['question'];  
    print_r($arr_question);

    foreach($arr_question as $question_id=>$option_value)
    {
         // do your task
    }
}