PHP会话变量返回空白

时间:2013-02-11 17:29:45

标签: php mysql

这是我的代码(为长度道歉):

$companyid = $_SESSION["companyid"];

$_SESSION["youtubeurl"] = mysql_real_escape_string($_POST["youtubeurl"]);
$_SESSION["logourl"] = mysql_real_escape_string($_POST["logourl"]);

$_SESSION["plan1head"] = mysql_real_escape_string($_POST["plan1head"]);
$_SESSION["plan1description"] = mysql_real_escape_string($_POST["plan1description"]);
$_SESSION["plan1headline1"] = mysql_real_escape_string($_POST["plan1headline1"]);
$_SESSION["plan1price1"] = mysql_real_escape_string($_POST["plan1price1"]);
$_SESSION["plan1headline2"] = mysql_real_escape_string($_POST["plan1headline2"]);
$_SESSION["plan1price2"] = mysql_real_escape_string($_POST["plan1price2"]);

$_SESSION["plan2head"] = mysql_real_escape_string($_POST["plan2head"]);
$_SESSION["plan2description"] = mysql_real_escape_string($_POST["plan2description"]);
$_SESSION["plan2headline1"] = mysql_real_escape_string($_POST["plan2headline1"]);
$_SESSION["plan2price1"] = mysql_real_escape_string($_POST["plan2price1"]);
$_SESSION["plan2headline2"] = mysql_real_escape_string($_POST["plan2headline2"]);
$_SESSION["plan2price2"] = mysql_real_escape_string($_POST["plan2price2"]);

$_SESSION["plan3head"] = mysql_real_escape_string($_POST["plan3head"]);
$_SESSION["plan3description"] = mysql_real_escape_string($_POST["plan3description"]);
$_SESSION["plan3headline1"] = mysql_real_escape_string($_POST["plan3headline1"]);
$_SESSION["plan3price1"] = mysql_real_escape_string($_POST["plan3price1"]);
$_SESSION["plan3headline2"] = mysql_real_escape_string($_POST["plan3headline2"]);
$_SESSION["plan3price2"] = mysql_real_escape_string($_POST["plan3price2"]);

$_SESSION["plan1price1type"] = $_POST["plan1price1type"];
$_SESSION["plan1price2type"] = $_POST["plan1price2type"];
$_SESSION["plan2price1type"] = $_POST["plan2price1type"];
$_SESSION["plan2price2type"] = $_POST["plan2price2type"];
$_SESSION["plan3price1type"] = $_POST["plan3price1type"];
$_SESSION["plan3price2type"] = $_POST["plan3price2type"];

mysql_connect("localhost", "name", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

$getstats = mysql_query("SELECT * FROM company WHERE companyid =   
'$companyid'");

$stats = mysql_fetch_array($getstats);

$calculaterating = $stats["ratingscore"] / $stats["ratingcount"];

if (!$calculaterating) {

$rating = "None";

} else {

$rating = $calculaterating;

}

$usage = $stats["usage"];

if(isset($_POST["submitpreview"])) {

$companyid = $_SESSION["companyid"];

$youtubeurl = $_SESSION["youtubeurl"];
$logourl = $_SESSION["logourl"];

$plan1head = $_SESSION["plan1head"];
$plan1description = $_SESSION["plan1description"];
$plan1headline1 = $_SESSION["plan1headline1"];
$plan1price1 = $_SESSION["plan1price1"];
$plan1headline2 = $_SESSION["plan1headline2"];
$plan1price2 = $_SESSION["plan1price2"];

$plan2head = $_SESSION["plan2head"];
$plan2description = $_SESSION["plan2description"];
$plan2headline1 = $_SESSION["plan2headline1"];
$plan2price1 = $_SESSION["plan2price1"];
$plan2headline2 = $_SESSION["plan2headline2"];
$plan2price2 = $_SESSION["plan2price2"];

$plan3head = $_SESSION["plan3head"];
$plan3description = $_SESSION["plan3description"];
$plan3headline1 = $_SESSION["plan3headline1"];
$plan3price1 = $_SESSION["plan3price1"];
$plan3headline2 = $_SESSION["plan3headline2"];
$plan3price2 = $_SESSION["plan3price2"];

$headlines = $_SESSION["headlines"];

$plan1price1type = $_SESSION["plan1price1type"];
$plan1price2type = $_SESSION["plan1price2type"];
$plan2price1type = $_SESSION["plan2price1type"];
$plan2price2type = $_SESSION["plan2price2type"];
$plan3price1type = $_SESSION["plan3price1type"];
$plan3price2type = $_SESSION["plan3price2type"];

mysql_connect("localhost", "name", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

$islisted = mysql_query("SELECT * FROM companypage WHERE companyid = '$companyid'");

if ($islisted) {
mysql_query("UPDATE companypage SET
plan1head = '$plan1head',
plan1description = '$plan1description',
plan1price1type = '$plan1price1type',
plan1headline1 = '$plan1headline1',
plan1price1 = '$plan1price1',
plan1price2type = '$plan1price2type',
plan1headline2 = '$plan1headline2',
plan1price2 = '$plan1price2',
plan2head = '$plan2head', 
plan2description = '$plan2description',
plan2price1type = '$plan2price1type',
plan2headline1 = '$plan2headline1',
plan2price1 = '$plan2price1',
plan2price2type = '$plan2price2type',
plan2headline2 = '$plan2headline2',
plan2price2 = '$plan2price2',
plan3head = '$plan3head',
plan3description = '$plan3description',
plan3price1type = '$plan3price1type',
plan3headline1 = '$plan3headline1',
plan3price1 = '$plan3price1',
plan3price2type = '$plan3price2type',
plan3headline2 = '$plan3headline2',
plan3price2 = '$plan3price2',
logourl = '$logourl',
youtubeurl = '$youtubeurl',
headlines = '$headlines'
WHERE companyid='$companyid'") or die(mysql_error());

} else {

mysql_query("INSERT INTO companypage (
companyid,
    plan1head,
plan1description,
plan1price1type,
plan1headline1,
plan1price1,
plan1price2type,
plan1headline2,
plan1price2,
plan2head,
plan2description,
plan2price1type,
plan2headline1,
plan2price1,
plan2price2type,
plan2headline2,
plan2price2,
plan3head,
plan3description,
plan3price1type,
plan3headline1,
plan3price1,
plan3price2type,
plan3headline2,
plan3price2,
logourl,
youtubeurl,
headlines) 

VALUES(
    '$companyid',
'$plan1head',
'$plan1description',
'$plan1price1type',
'$plan1headline1',
'$plan1price1',
'$plan1price2type',
'$plan1headline2',
'$plan1price2',
'$plan2head',
'$plan2description',
'$plan2price1type',
'$plan2headline1',
'$plan2price1',
'$plan2price2type',
'$plan2headline2',
'$plan2price2',
'$plan3head',
'$plan3description',
'$plan3price1type',
'$plan3headline1',
'$plan3price1',
'$plan3price2type',
'$plan3headline2',
'$plan3price2',
'$logourl',
'$youtubeurl',
'$headlines')
") or die(mysql_error());

}

echo "<script>window.close();</script";

}

基本上,这里发生的是用户有一个页面,可以通过一系列表格,下拉菜单等来更改。要创建或更新他们的页面,他们填写表格和点击预览按钮,这是一个target_blank,弹出一个新选项卡,预览他们的页面将是什么样子。上一页中的所有变量都会首先插入到会话中供以后使用。在“预览”页面上是一个更新按钮。如果他们喜欢他们看到的内容,他们会点击更新按钮(因此在PHP代码中执行“提交预览”)。然后PHP代码检查它们是否创建了一个页面,并将它们添加到表中或根据变量更新它们的当前表。

这是我的问题。当预览页面第一次加载时,所有变量,会话或POST都会正确显示。但是,当执行提交预览并将信息存储在数据库中时,会存储以下内容:companyid存储为0,其他所有内容在表中存储为空白(即使它不应接受空值?) 。这适用于插入和更新。所以它显然让我觉得我的会话变量(在$ _POST变量的页面的第一个页面加载,我知道这是正确的,因为它们出现在预览页面上)在某种程度上存在错误当我在执行submitpreview时将它们传递回正常变量时传递错误。但是真正让我感到困惑的是,公司代码也没有正确存储(存储为0而不是正确的id),即使我在其他地方回显它显示正确。我确定我所做的是一个简单的愚蠢错误,但我无法理解。

感谢您的帮助,并对代码和问题的长度感到抱歉。

2 个答案:

答案 0 :(得分:2)

你错过了

session_start();

这必须是您希望使用会话变量的任何文件的第一行。

请参阅主题的php.net documentation

答案 1 :(得分:2)

如果那是你的实际脚本,那么在执行所有mysql_real_escape_string()调用时你还没有连接到MySQL。您必须连接必须才能使此功能正常工作。现在它只是返回一系列布尔FALSE值来表示错误。

将您的数据库连接代码移动到脚本的开头,它应该开始更好地工作。但是,当然,你应该升级到mysqli或PDO,因为不推荐使用mysql _ *()。

当然,从不假设数据库操作已成功。始终检查失败的返回值,例如:

$var = mysql_real_escape_string($_POST['var']) or die(mysql_error());
                                              ^^^^^^^^^^^^^^^^^^^^^^

会告诉你问题是什么。