gpt4 book ai didi

php - 将 session 变量输入 mysql 时出现问题

转载 作者:行者123 更新时间:2023-11-30 01:30:25 26 4
gpt4 key购买 nike

因此,我尝试将一些 session 变量输入到数据库中,并且成功插入了除 $_SESSION['organizationId'] 之外的所有行。一些上下文:用户登陆下面代码片段中给出的网址,我得到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";
}


?>

最佳答案

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

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

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

关于php - 将 session 变量输入 mysql 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17542540/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com