奇怪的未定义索引错误

时间:2011-09-15 18:24:06

标签: php

我有几个变量被分配了会话变量的值(参见前四个) - dbfunctions.php:

<?php 
    include_once('functions.php'); 

function newAdminCivilReport(){

$userId = $_SESSION['userId'];
$judge = $_SESSION['judgeID'];
$month = $_SESSION['month'];
$year = $_SESSION['year'];

$con = mysql_connect("-","-","-");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
else {
    // connected to database successfully
}
mysql_select_db("casemanagers", $con);
$PendingCivil = mysql_real_escape_string($_POST['PendingCivil']);
$PendingAsbestos = mysql_real_escape_string($_POST['PendingAsbestos']);
$PendingDomestic = mysql_real_escape_string($_POST['PendingDomestic']);
$AsgNewCivil = mysql_real_escape_string($_POST['AsgNewCivil']);
$AsgNewCivil = mysql_real_escape_string($_POST['AsgNewCivil']);
$AsgNewAsbestos = mysql_real_escape_string($_POST['AsgNewAsbestos']);
$AsgNewDomestic = mysql_real_escape_string($_POST['AsgNewDomestic']);
$AsgTransferCivil = mysql_real_escape_string($_POST['AsgTransferCivil']);
$AsgTransferAsbestos = mysql_real_escape_string($_POST['AsgTransferAsbestos']);
$AsgTransferDomestic = mysql_real_escape_string($_POST['AsgTransferDomestic']);
$AsgReopenedCivil = mysql_real_escape_string($_POST['AsgReopenedCivil']);
$AsgReopenedAsbestos = mysql_real_escape_string($_POST['AsgReopenedAsbestos']);
$AsgReopenedDomestic = mysql_real_escape_string($_POST['AsgReopenedDomestic']);
$DispTOCivil = mysql_real_escape_string($_POST['DispTOCivil']);
$DispTOAsbestos = mysql_real_escape_string($_POST['DispTOAsbestos']);
$DispTODomestic = mysql_real_escape_string($_POST['DispTODomestic']);
$clerkITNumber = mysql_real_escape_string($_POST['clerkITNumber']);


$query = "INSERT INTO `casemanagers`.`civil` (`JudgeID`, `Month`, `Year`, `PendingCivil`, `PendingAsbestos`, `PendingDomestic`, `AsgNewCivil`, `AsgNewAsbestos`, `AsgNewDomestic`, `AsgTransferCivil`, `AsgTransferAsbestos`, `AsgTransferDomestic`, `AsgReopenedCivil`, `AsgReopenedAsbestos`, `AsgReopenedDomestic`, `DispWOPCivil`, `DispWOPAsbestos`, `DispWOPDomestic`, `DispFinalCivil`, `DispFinalAsbestos`, `DispFinalDomestic`, `DispBTCivil`, `DispBTAsbestos`, `DispBTDomestic`, `DispJTCivil`, `DispJTAsbestos`, `DispJTDomestic`, `DispTOCivil`, `DispTOAsbestos`, `DispTODomestic`, `OldCivil`, `OldAsbestos`, `OldDomestic`, `userID`, `clerkITNumber`) VALUES ($judge, $month, $year, $PendingCivil, $PendingAsbestos, $PendingDomestic, $AsgNewCivil, $AsgNewAsbestos, $AsgNewDomestic, $AsgTransferCivil, $AsgTransferAsbestos, $AsgTransferDomestic, $AsgReopenedCivil, $AsgReopenedAsbestos, $AsgReopenedDomestic, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, $DispTOCivil, $DispTOAsbestos, $DispTODomestic, 0, 0, 0, $userId, $clerkITNumber);";
mysql_select_db("casemanagers", $con);

$result = mysql_query($query);
return $result;
mysql_close($con);
}
?>

这些值位于单独文件中的函数中。当我使用该函数(在数据库中插入一大堆项目)时,所有内容(包括上面变量的正确值)都会成功插入到数据库中。但是,我还是得到了:

注意:未定义的索引:第6行的c:\ Inetpub \ wwwroot \ CaseManagement \ dbfunctions.php中的judgeID

注意:未定义的索引:第7行的c:\ Inetpub \ wwwroot \ CaseManagement \ dbfunctions.php中的月份

注意:未定义的索引:第8行的c:\ Inetpub \ wwwroot \ CaseManagement \ dbfunctions.php中的年份

我无法弄清楚原因。变量按预期工作。

main.php:

<?php


session_start();

   include_once('dbfunctions.php');
   include_once('functions.php');
    $result = newAdminCivilReport();

?>

2 个答案:

答案 0 :(得分:2)

在访问session_start()变量之前,我没有看到此处调用$_SESSION

答案 1 :(得分:2)

session_start()必须是PHP中非常的第一件事。所以需要:

<?php
   session_start(); // <=== this MUST be first
   include_once('dbfunctions.php');
   include_once('functions.php');

    $result = newAdminCivilReport();

?>

编辑: 除非另有说明,否则PHP函数中的变量是本地的,因此请对dbfunctions.php进行以下更改:

function newAdminCivilReport(){
    global $_SESSION; // <==== add this line at the start