Weird undefined index errors
I have a few variables that are assigned the values of session variables (See the first four)--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 = my开发者_如何学JAVAsql_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);
}
?>
These values are in a function in a separate file. When I use the function (which inserts a whole bunch of items in a database), everything, including the proper values for the variables above, gets inserted into the database successfully. However, I'm still getting:
Notice: Undefined index: judgeID in c:\Inetpub\wwwroot\CaseManagement\dbfunctions.php on line 6
Notice: Undefined index: month in c:\Inetpub\wwwroot\CaseManagement\dbfunctions.php on line 7
Notice: Undefined index: year in c:\Inetpub\wwwroot\CaseManagement\dbfunctions.php on line 8
I can't figure out why. The variables work as they should.
main.php:
<?php
session_start();
include_once('dbfunctions.php');
include_once('functions.php');
$result = newAdminCivilReport();
?>
session_start()
must be the very first thing in your PHP. So it needs to be:
<?php
session_start(); // <=== this MUST be first
include_once('dbfunctions.php');
include_once('functions.php');
$result = newAdminCivilReport();
?>
EDIT:
Variables in PHP functions are local unless declared otherwise, so make the following change to dbfunctions.php
:
function newAdminCivilReport(){
global $_SESSION; // <==== add this line at the start
I don't see session_start()
called here before accessing the $_SESSION
variables.
精彩评论