0

所以我试图将一些会话变量输入到数据库中,并且我成功地插入了所有行,除了 $_SESSION['organisationId']。一些上下文:一个用户登陆一个网址,如下面的片段中给出的,我得到了organizationId,然后希望该用户在创建帐户时被分配该organizationId - 作为一种“邀请系统”工作.

// 这是我使用的 URL:http ://thisapp.com/login.php?competitionId=51da7ed4d686a&organisationId=51d81cab92709

   <?php
      session_start();
      ob_start();
      ini_set('display_errors',1);
      ini_set('display_startup_errors',1);
      error_reporting(-1);

       include('db.php');

    $_SESSION['competitionId'] = $_GET['competitionId'];
    $_SESSION['organisationId'] = $_GET['organisationId'];

    <h4>Create your Account</h4>

    <form action="login.php" method="post" name="acceptinvite">
        name: <input type="text" name="createname"><br>
        email: <input type="text" name="createemail"><br>
        password: <input type="password" name="createpassword"><br>
        <input type="submit" value="Create Account">
    </form>

    <?php

    if (isset($_POST["createname"]) && !empty($_POST["createname"])) {

        //define variables
        $name = mysql_real_escape_string($_POST['createname']);
        $email = mysql_real_escape_string($_POST['createemail']);
        $password = mysql_real_escape_string($_POST['createpassword']);
        $teamLeader = 0;
        $organisationId = mysql_real_escape_string($_SESSION['organisationId']);
        $orgName = mysql_real_escape_string($_SESSION['orgName']);

        //finish registering the user
        $acceptInviteTeam = ("INSERT INTO `users` (`organisationId`, `name`, `email`, `password`, `isTeamLeader`) VALUES ('$organisationId', '$name', '$email', '$password', '$teamLeader')");
        $result = mysql_query($acceptInviteTeam)  or die (mysql_error());
    }

    else {
        echo "Fill out the form and use the correct credentials";
    }


    ?>
4

1 回答 1

0

这就是问题所在。当您输入链接http://thisapp.com/login.php?competitionId=51da7ed4d686a&organisationId=51d81cab92709时,您拥有 organizationId 并将其保存在会话中。但是表单的目标只是 login.php。当您提交表单时,url 中没有 organizationId,因此会话变量被 null 覆盖。这是修复:

$_SESSION['competitionId'] = isset($_GET['competitionId']) ? $_GET['competitionId'] : $_SESSION['competitionId'];
$_SESSION['organisationId'] = isset($_GET['organisationId']) ? $_GET['organisationId'] : $_SESSION['organisationId'];

或者,您可以使用 if 语句。

于 2013-07-09T08:27:52.987 回答