我试图用PHP OOP创建自己的CMS。基本上我有2个主类文件。其中一个是User.class.php
,就像这样:
<?php
class User{
private $db;
public function __construct()
{
$this->db = new Connection();
$this->db = $this->db->dbConnect();
}
public function Login($name, $pass)
{
if(!empty($name)&&!empty($pass))
{
$st = $this->db->prepare("select * from admins where username=? and password=?");
$st->bindParam(1,$name);
$st->bindParam(2,$pass);
$st->execute();
if($st->rowCount() == 1)
{
header('Location: maint/dashboard.php');
$_SESSION["admin_username"] = $name;
$joined = new Joined();
$joined->Insert($_SESSION["admin_username"]);
exit();
}
else
{
header("Location: index.php");
exit();
}
}
}
}
?>
此类文件实际上是我的登录表单的操作文件。还有另一个名为Joined.class.php
的类文件,其中包含:
<?php
class Joined{
private $db;
public function __construct()
{
$this->db = new Connection();
$this->db = $this->db->dbConnect();
}
public function Insert($name)
{
if(!empty($name))
{
$ins = $this->db->prepare("UPDATE admins SET login='NOW()' WHERE username=?");
$ins->bindParam(1,$name);
$ins->execute();
}
}
}
?>
它的作用是从前一个类文件中获取$_SESSION["admin_username"]
并在admins表上运行更新查询,并将我的表上的登录字段设置为当前日期,其中username是等于$_SESSION["admin_username"]
。
此代码看起来很完美,并且不会返回任何类型的错误。但是,每当我登录我的帐户时,它也不会更新登录字段,我也不知道为什么!所以,如果您知道如何解决这个问题,请告诉我...提前致谢。
答案 0 :(得分:0)
我怀疑某种会话问题。由于您实际上并不需要此部分代码的会话,请在user.class.php中尝试此操作
$joined->Insert($name);
答案 1 :(得分:0)
哎呀,我找到了:
<?php
class User{
private $db;
public function __construct()
{
$this->db = new Connection();
$this->db = $this->db->dbConnect();
}
public function Login($name, $pass)
{
if(!empty($name)&&!empty($pass))
{
$st = $this->db->prepare("select * from admins where username=? and password=?");
$st->bindParam(1,$name);
$st->bindParam(2,$pass);
$st->execute();
if($st->rowCount() == 1)
{
$_SESSION["admin_username"] = $name;
$joined = new Joined();
$joined->Insert($_SESSION["admin_username"]);
header('Location: maint/dashboard.php');
exit();
}
else
{
header("Location: index.php");
exit();
}
}
}
}
?>
基本上,它从上到下读取代码,所以我必须在调用类后调用标题。
$_SESSION["admin_username"] = $name;
$joined = new Joined();
$joined->Insert($_SESSION["admin_username"]);
header('Location: maint/dashboard.php');
exit();