PHP案例:连接数据库实现登录功能
一、创建数据库与表
二、创建PHP项目login
1、用户实体类 - User.php
<?php
/**
* Created by PhpStorm.
* User: howard
* Date: 2017/4/12
* Time: 13:54
*/
namespace net\hw\bean;
class User
{
private $id;
private $username;
private $password;
private $telephone;
private $registerTime;
private $popedom;
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return mixed
*/
public function getUsername()
{
return $this->username;
}
/**
* @param mixed $username
*/
public function setUsername($username)
{
$this->username = $username;
}
/**
* @return mixed
*/
public function getPassword()
{
return $this->password;
}
/**
* @param mixed $password
*/
public function setPassword($password)
{
$this->password = $password;
}
/**
* @return mixed
*/
public function getTelephone()
{
return $this->telephone;
}
/**
* @param mixed $telephone
*/
public function setTelephone($telephone)
{
$this->telephone = $telephone;
}
/**
* @return mixed
*/
public function getRegisterTime()
{
return $this->registerTime;
}
/**
* @param mixed $registerTime
*/
public function setRegisterTime($registerTime)
{
$this->registerTime = $registerTime;
}
/**
* @return mixed
*/
public function getPopedom()
{
return $this->popedom;
}
/**
* @param mixed $popedom
*/
public function setPopedom($popedom)
{
$this->popedom = $popedom;
}
function __toString()
{
return "User{id=$this->id, username='$this->username',
password='$this->password', telephone='$this->telephone',
registerTime='$this->registerTime', popedom=$this->popedom}";
}
}
?>
2、数据源类 - DataSource.php
<?php
/**
* Created by PhpStorm.
* User: howard
* Date: 2017/4/12
* Time: 13:56
*/
namespace net\hw\util;
class DataSource
{
public function getConn()
{
$host = "localhost";
$username = "root";
$password = "root";
$dbname = "simonshop";
$conn = mysqli_connect($host, $username, $password, $dbname);
if ($conn->connect_errno)
{
die("连接MySQL数据库失败!" . $conn->connect_error);
}
return $conn;
}
}
3、用户数据访问类 - UserDao.php
<?php
namespace net\hw\dao;
require_once("/net/hw/bean/User.php");
require_once("/net/hw/util/DataSource.php");
use \net\hw\bean\User;
use \net\hw\util\DataSource;
class UserDao
{
public function findUserById($id)
{
$dataSource = new DataSource();
$conn = $dataSource->getConn();
$sql = "select * from t_user where id = $id";
$result = $conn->query($sql);
$row = $result->fetch_array(MYSQLI_ASSOC);
$user = new User();
$user->setId($row['id']);
$user->setUsername($row['username']);
$user->setPassword($row['password']);
$user->setTelephone($row['telephone']);
$user->setRegisterTime($row['register_time']);
$user->setPopedom($row['popedom']);
return $user;
}
public function findAllUsers()
{
$dataSource = new DataSource();
$conn = $dataSource->getConn();
$sql = "select * from t_user";
$result = $conn->query($sql);
$i = 0;
$users = array();
while($row = $result->fetch_assoc()) {
$users[$i] = new User();
$users[$i]->setId($row['id']);
$users[$i]->setUsername($row['username']);
$users[$i]->setPassword($row['password']);
$users[$i]->setTelephone($row['telephone']);
$users[$i]->setRegisterTime($row['register_time']);
$users[$i]->setPopedom($row['popedom']);
$i++;
}
return $users;
}
public function login($username, $password)
{
$dataSource = new DataSource();
$conn = $dataSource->getConn();
$sql = "select * from t_user where username = '$username' and password = '$password'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
return true;
} else {
return false;
}
}
}
?>
说明:$result->fetch_array(MYSQLI_ASSOC) 就相当于 $result->fetch_assoc()
4、登录页面 - login.php
<!DOCTYPE html>
<html>
<head>
<title>用户登录</title>
<style type="text/css">
body {
text-align: center;
}
table {
margin: auto;
}
</style>
</head>
<body>
<h3>用户登录</h3>
<form action="doLogin.php" method="POST">
<table border="1" cellspacing="0" cellpadding="5">
<tr>
<td>用户名</td><td>
<input id="username" type="text" name="username"></td>
</tr>
<tr>
<td>密码</td>
<td><input id="password" type="password" name="password"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="登录" οnclick="return checkForm();">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</body>
<script type="text/javascript">
function checkForm() {
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
if (username == null || username == "") {
alert("用户名不能为空!");
document.getElementById("username").focus();
return false;
}
if (password == null || password == "") {
alert("密码不能为空!");
document.getElementById("password").focus();
return false;
}
return true;
}
</script>
</html>
5、登录处理页面 - doLogin.php
<?php
require_once("/net/hw/dao/UserDao.php");
use \net\hw\dao\UserDao;
$username = $_POST["username"];
$password = $_POST["password"];
$userDao = new UserDao();
$success = $userDao->login($username, $password);
if ($success) {
$location = "success.php?username=$username";
header("location:$location");
} else {
$location = "failure.php?username=$username";
header("location:$location");
}
?>
6、登录成功页面 - succes.php
<!DOCTYPE html>
<html>
<head>
<title>登录成功</title>
</head>
<body>
<h1>
<?php
$username=$_GET["username"];
echo $username;
?>,登录成功</h1>
</body>
</html>
7、登录失败页面 - failure.php
<!DOCTYPE html>
<html>
<head>
<title>登录失败</title>
</head>
<body>
<h1>
<?php
$username=$_GET["username"];
echo $username;
?>,登录失败</h1>
</body>
</html>
运行效果如下:
8、显示单个用户页面 - showOneUser.php
<?php
require_once("/net/hw/dao/UserDao.php");
use \net\hw\dao\UserDao;
$userDao = new UserDao();
$id = 2;
$user = $userDao->findUserById($id);
echo $user . "<br>";
?>
9、显示全部用户页面 - showAllUsers.php
<?php
require_once("/net/hw/dao/UserDao.php");
use \net\hw\dao\UserDao;
$userDao = new UserDao();
$users = $userDao->findAllUsers();
foreach ($users as $user) {
echo $user . "<br>";
}