2013-07-09 64 views
0

所以我想輸入一些會話變量到數據庫中,並且我成功地插入了所有行,除了$ _SESSION ['organisationId']。一些情況:用戶登錄到下面snippit中給出的URL,我得到organisationId,然後在創建帳戶時要求爲該用戶分配該組織id - 作爲各種「邀請系統」 。麻煩輸入會話變量到mysql

//這是我使用的網址: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"; 
    } 


    ?> 
+0

organisationId的數據類型是什麼?我在其中看到字母 –

+0

uniqid(); &db(varchar 255 latin1) – JP89

+0

echo $ organisationId'$ acceptInviteTeam = ...'之前,看看你得到了什麼 –

回答

0

這裏的問題是什麼。 當你輸入鏈接http://thisapp.com/login.php?competitionId=51da7ed4d686a&organisationId=51d81cab92709你有organisationId並將它保存在會話中。但是表單的目標僅僅是login.php。當你提交表單時,url中沒有organisationId,所以會話變量被null覆蓋。 這是修復:

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

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

+0

謝謝!解決了。非常明顯,現在:) – JP89

+0

不客氣:) –