我有一段似乎没有工作的代码,因为它在读出数组时出错。
我使用此函数从users数组中获取变量:
static function getVar($v) {
if(in_array($v, user::$struc)) {
return self::$user[user::$struc[$v]];
}
else die("getVar: ".$v." > is unkown in struc");
}
现在我在使用时遇到错误:
user::getVar('Klantnummer');
它给了我一个通知:
注意:未定义的偏移量:在第79行的C:\ xampp \ htdocs \ Snackbar \ System \ Modules \ User.php中为0 第79行是这样的:return self :: $ user [user :: $ struc [$ v]];
我已经在登录页面上放了一个var_dump,看看它是否将数据放入数组中,并且工作正常,如下所示:
array(9) {
["Klantnummer"]=> string(1) "2"
["LoginNaam"]=> string(5) "Thimo"
["Wachtwoord"]=> string(128) "SPOOFED OUT
["Voornaam"]=> string(5) "Thimo"
["Achternaam"]=> string(7) "Franken"
["Adres"]=> string(10) "Poortugaal"
["Huisnummer"]=> string(2) "11"
["Plaats"]=> string(10) "Poortugaal"
["Postcode"]=> string(6) "3176VE"
}
现在这里是$struc
数组:
static $struc = array(
"Klantnummer" => 0,
"LoginNaam" => 1,
"Wachtwoord" => 2,
"Voornaam" => 3,
"Achternaam" => 4,
"Adres" => 5,
"Huisnummer" => 6,
"Plaats" => 7,
"Postcode" => 8
);
任何人都知道可能出现什么问题?
完整代码:
<?php
class user {
static $struc = array(
"Klantnummer" => 0,
"LoginNaam" => 1,
"Wachtwoord" => 2,
"Voornaam" => 3,
"Achternaam" => 4,
"Adres" => 5,
"Huisnummer" => 6,
"Plaats" => 7,
"Postcode" => 8
);
static $ses = "loggedin";
static $hash = "whirlpool";
static $user = array();
static $usda = "userdat";
static function login($username, $password) {
$host = 'localhost';
$database = 'Snackbar';
$user = 'root';
$password = '';
try {
$conn = new PDO('mysql:host='.$host.';dbname='.$database, $user, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
$msg = "Regelnummer: ".$e->getLine()."<br/>
Bestand: ".$e->getFile()."<br/>
Foutmelding: ".$e->getMessage()."";
trigger_error($msg);
}
$HashPW = hash('whirlpool', $_POST['Password']);
$Hashed = strtoupper($HashPW);
$Login = $conn->prepare("SELECT * FROM Klanten WHERE LoginNaam = :Naam AND Wachtwoord=:Wachtwoord");
$Login->bindParam(':Naam', $_POST['Name'], PDO::PARAM_STR, 45);
$Login->bindParam(':Wachtwoord', $Hashed, PDO::PARAM_STR, 129);
$Login->execute();
if($Login->rowCount() > 0) {
self::$user = $Login->fetch(PDO::FETCH_ASSOC);
$_SESSION[self::$ses] = true;
$_SESSION[self::$usda] = self::$user;
var_dump(self::$user);
print("Succesvol ingelogd");
}
else
{
$_SESSION[self::$ses] = false;
$_SESSION[self::$usda] = false;
print("Verkeerde login naam en/of wachtwoord");
}
}
static function logOut() {
$_SESSION[self::$ses] = false;
$_SESSION[self::$usda] = false;
self::$user = null;
}
static function loggedIn() {
return $_SESSION[self::$ses];
}
static function getVar($v) {
if(in_array($v, user::$struc)) {
return self::$user[user::$struc[$v]];
}
else die("getVar: ".$v." > is unkown in struc");
}
}
答案 0 :(得分:1)
您的方法getVar()
有错误。 self::$user
不存在,导致self::$user[user::$struc[$v]]
未定义。您真正想要访问的是self::$struct[$v]
。
static function getVar($v) {
if(in_array($v, self::$struc)) {
return self::$struc[$v];
}
else die("getVar: ".$v." > is unkown in struc");
}
有关工作示例,请参阅http://sandbox.onlinephpfunctions.com/code/e4f3a3ada495ec97e16f55b4132b99c9c1f4b4f9。