0

我的问题是,当我使用会话变量在不同的 php 脚本之间复制我的数组元素时,什么都没有打印出来。以下是我的两个 php 文件。

文件1.php

   $SQL = "SELECT * FROM tblquestions";
if ($db_found) {
        $result = mysql_query($SQL);
        $numRows = mysql_num_rows($result); //return number of rows in the table
        echo '<FORM NAME ="form1" METHOD ="POST" ACTION ="file2.php">';
        for ($i = 1; $i <= 2; $i++)
        {
            $db_field = mysql_fetch_assoc($result);
            $qID[$i] = $db_field['QID'];
            $question[$i] = $db_field['Question'];
            $A[$i] = $db_field['qA'];
            $B[$i] = $db_field['qB'];
            $C[$i] = $db_field['qC'];
            echo '<P>';
            print $question[$i];
            echo '<P>';
            echo "<INPUT TYPE = 'Radio' Name = '".$qNum."'  value= 'A'>"; 
            print $A[$i];
            echo '<P>';
            echo  "<INPUT TYPE = 'Radio' Name = '".$qNum."'   value= 'B'>"; 
            print $B[$i];
            echo '<P>';
            echo  "<INPUT TYPE = 'Radio' Name = '".$qNum."'   value= 'C'>"; 
            print $C[$i];
            //if (isset($_POST[$name_Value]))
            $survey_Answers[$i-1] = $_POST[$qNum];
            print '</BR>'.$survey_Answers[$i-1]."</BR>";
            $question_Number = ltrim($qNum,'q');
            $question_Number++;
            $qNum ='q'.$question_Number;
        }

        echo '<p>';
    session_start();
    $_SESSION['answers'] = $survey_Answers;
        echo '<INPUT TYPE = "Submit" Name = "Submit1"  VALUE = "Click here to vote">';

        echo '</form>';

在我的第二个文件(file2.php)上,我有以下内容:

<?PHP
session_start();
if (isset($_POST['Submit1'])) {
        $results = $_SESSION['answers'];
        print $results[0];
?>

执行上述代码时没有打印任何内容。

提前致谢

4

1 回答 1

1

我很确定存储的会话值需要是字符串。

在 file1.php 更改:

$_SESSION['answers'] = $survey_Answers;

对此:

$_SESSION['answers'] = json_encode($survey_Answers);

然后在 file2.php 中更改:

$results = $_SESSION['answers'];

对此:

$results = json_decode($_SESSION['answers']);
于 2013-06-24T18:04:36.983 回答