2017-08-24 59 views
-2

在回复中$numOfEmployees每次提交新表单时都是一样的。我在这里做错了什么?我试图每次将它保存到会话中,但仍然无法正常工作。我是新来的PHP,到目前为止还不是最大的粉丝为什么这个PHP变量不会按每个请求递增?

<?php 

class Employee{ 

    private $fn; 
    private $ln; 
    private $dpt; 
    private $ID; 

    public function setVars($fna, $lna, $dpta, $numOfEmployeesa){ 
     $this-> fn = $fna; 
     $this -> ln = $lna; 
     $this -> dpt = $dpta; 
     $this -> ID = $numOfEmployeesa; 
    } 

} 
if(isset($_SESSION['numOfEmployees'])){ 
    //get it 
    $numOfEmployees = $_SESSION['numOfEmployees']; 
} else { 
    //set a default value if not isset 
    $numOfEmployees = 0; 
} 
if(isset($_SESSION['employeeArray'])){ 
    //get it 
    $employeeArray = $_SESSION['employeeArray']; 
} else { 
    //set a default value if not isset 
    $employeeArray = array(); 
} 

$employee = new Employee(); 
$fn = $_POST['firstname']; 
$ln = $_POST['lastname']; 
$dpt = $_POST['department']; 

$employee -> setVars($fn, $ln, $dpt, $numOfEmployees); 

$numOfEmployees++; 

echo "First Name: " . $fn . "\nLast Name: " . $ln . "\nDepartment: " . $dpt . "\nID: " . sprintf('%08d', $numOfEmployees) . "\nNumber of employees: " . $numOfEmployees; 

$employeeArray[] = $employee; 

$_SESSION['employeeArray'] = $employeeArray; 
$_SESSION['numOfEmployees'] = $numOfEmployees; 

?> 
+1

需要更换'$ someValue中='和'$ numOfEmployees ='和'$ employeeArray ='在你的两个if语句 – cmorrissey

+1

要使用会话,你需要调用'session_start' –

+0

谢谢我修复了变量仍然不能正常工作 – Zachscs

回答

0

试着在你的文件的开头加入session_start()

+0

我把它放在文件的顶部,但在<?php标签内? – Zachscs

+0

谢谢,那是什么!当它允许我时,我会将您的答案标记为正确。 – Zachscs

相关问题