1

My question is that when I copy my array elements between different PHP scripts using session variables, nothing gets printed out. The following are my two PHP files.

file1.php

<?PHP
     session_start();
        $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['answers'] = $survey_Answers;
            echo '<INPUT TYPE = "Submit" Name = "Submit1"  VALUE = "Click here to vote">';

            echo '</form>';
?>

On my Second file (file2.php), I have the following:



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

However, on my file2.php I get the following error: Undefined offset: 0 and nothing gets printed out.

4

5 回答 5

1
echo '<p>';
session_start();

那行不通,必须在任何输出之前调用 session_start !如果你放在session_start()文件的开头,你应该没问题。

于 2013-06-24T18:13:23.933 回答
1

要使用基于 cookie 的会话,必须在向浏览器输出任何内容之前调用 session_start()。

来源: http: //php.net/manual/en/function.session-start.php

您需要session_start()在输出任何内容之前调用。在输出任何内容之前,最好将其放在脚本的开头。

于 2013-06-24T18:14:37.930 回答
0

在 file1.php 中session_start();应该是与 file2.php 中相同的代码的第一行

于 2013-06-24T18:17:24.130 回答
0

PHP手册页上有一条评论session_start(),听起来与您的问题非常相似。http://php.net/manual/en/function.session-start.php#65944

它表示具有整数键的数组分配为直接数组或使用整数将失败,并且数据不会传递到下一页。

修改您的代码以使用字符串作为键。

于 2015-01-02T04:11:14.077 回答
0

请将以下代码添加到您要使用会话数据的每个页面。否则它将返回错误。

<?php
session_start();
?>
于 2013-06-24T18:31:13.767 回答