会话变量值不在页面之间传递

时间:2010-04-25 11:40:32

标签: php

我对php有点新,虽然我已经设法在这段代码让我困惑之前传递了会话变量的值

<form action="team_reg2.php" method="post" name="form1" class="cent" id="form1">
<p><strong>Team Registration</strong></p>
<?php
    $con=mysql_connect("localhost","root","");
    mysql_select_db("databolism",$con);
    $result=mysql_query("select * from events where id=$_POST[event]",$con);
    $row=mysql_fetch_assoc($result);
    $num=$row['max_team'];
    $_SESSION['max_team']=$num;
    $_SESSION['event_id']=$_POST['event'];
    for($i=1;$i<=$num;$i++)
    {
        print "<p><span id=\"sprytextfield$i\"\n>";
        print "<label>member$i";
        print "<input type=\"text\" name=\"mem$i\" id=\"mem$i\" />\n";
        print "</label>\n";
        print "<span class=\"textfieldInvalidFormatMsg\">Please enter only id</span></span></p>\n";
    }
?>

现在这将通过post传递给team_reg2.php

<?php
    $con=mysql_connect("localhost","root","");
    if(!$con)
        die("could not connect to db");
    $db=mysql_select_db("databolism",$con);
    $num=6;
    $event=14;
    $result=mysql_query("select * from events where id=$event",$con) or die(mysql_error());
    $row=mysql_fetch_assoc($result);
    $query='fantasia1_'.$row['name'];
    $query2="select max(t_id) from $query";
    $que=mysql_query($query2,$con);
    $result=mysql_fetch_array($que) or die(mysql_error());
    $chk3=$result['max(t_id)']; 
    if($chk3==NULL)
    {
        for($i=1;$i<=$num;$i++)
        {
            $name='mem'.$i;
            if($_POST["$name"]!="")
            {
                $query2="insert into $query values(1,'$_POST[$name]')";
                $que=mysql_query($query2,$con);
            }
        }

        echo " please note your team id is 1 <br>";
        echo " your team members are : <br>";
        for($i=1;$i<=$num;$i++)
        {
            $name='mem'.$i;
            echo "$_POST[$name]<br>";
        }

    }
    else
    {
        $str="select * from $query where (";
        for($i=1;$i<=$num;$i++)
        {
            $name='mem'.$i;
            $text="p_id='$_POST[$name]'";
            if($i==1)
                $str=$str.$text;
            else
                $str=$str.' or '.$text;
        }
        $str=$str.')'; 
        $query2=$str;
        echo "$str<br>";
        // echo "$query2</br>";
        $que=mysql_query($query2,$con) or die(mysql_error());
        $num=mysql_num_rows($que);
        if($num!=0)
        {
            while($result=mysql_fetch_array($que))
            {
                echo "$result[p_id] is already registered in team $result[t_id]<br>";    
            }
            //include("reg_team.html");
        } 
        else if($num==0)
        {
            //echo $query;
            $query2="select max(t_id) from $query";
            $que=mysql_query($query2,$con) or die(mysql_error());
            //echo "$que<br>";
            $result=mysql_fetch_array($que);
            $max=$result['max(t_id)'];
            $max++;
            $num=$_SESSION['max_team'];
            for($i=1;$i<=$num;$i++)
            {
                $name='mem'.$i;
                if($_POST[$name]!="")
                {
                    $query2="insert into $query values($max,'$_POST[$name]')";
                    $que=mysql_query($query2,$con);
                }
            }
            echo " please note your team id is $max <br>";
            echo " your team members are : <br>";
            for($i=1;$i<=$num;$i++)
            {
                $name='mem'.$i;
                echo "$_POST[$name]<br>";
            }
        }
    }
?>

我做了session_start();在页面本身的开头。问题是在第二个文件中回显$ _SESSION变量不会打印任何内容。

有人请解释我发生了什么事。 谢谢

1 个答案:

答案 0 :(得分:3)

第二个文件中也需要session_start()