By GokiSoft.com| 14:47 22/04/2022|
Học PHP

[Video] Thêm/sửa/xóa tài khoản người dung PHP/MySQL - Khóa học lập trình PHP/MySQL - C2110I


#readme.txt


Nội dung kiến thức:
	- Viết CRUD -> quản lý tài khoản người dùng
	- Nâng cao:
		- Mã hóa mật khẩu -> Bảo vệ thông tin tài khoản người dùng

Tổ chức dự án:
	- db:
		- config.php
		- dbhelper.php
	- utils
		- utility.php
	- list.php -> Hiển thị danh sách tài khoản người
	- add.php -> Thêm người dùng
	- edit.php -> Sửa thông tin tài khoản người dùng
	- delete.php -> Xóa thông tin tài khoản

	MD5 Custom:
		md5(pwd) -> hash
		hash + PRIMARY_KEY -> MD5(?) -> SDFSDF

		MD5('123456') -> e10adc3949ba59abbe56e057f20f883e
		e10adc3949ba59abbe56e057f20f883eHsdkf289354hd%^^&%234
		-> baf34b345db51cd7c47353671620abcd


#config.php


<?php
define('HOST', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', '');
define('DATABASE', 'c2110i');


#dbhelper.php


<?php
require_once('config.php');

function execute($sql) {
	// B1) Tao ket noi toi CSDL
	$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
	mysqli_set_charset($conn, 'utf8');

	// B2) Thuc hien cau insert into
	// echo $sql;
	// die();
	// insert, update, delete
	mysqli_query($conn, $sql);

	// B3) Dong ket noi
	mysqli_close($conn);
}

function executeResult($sql, $isSingle = false) {
	// B1) Tao ket noi toi CSDL
	$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
	mysqli_set_charset($conn, 'utf8');

	// B2) Thuc hien cau insert into
	$resultset = mysqli_query($conn, $sql);

	$data = null;

	if($isSingle) {
		$data = mysqli_fetch_array($resultset, 1);
	} else {
		$data = [];
		while(($row = mysqli_fetch_array($resultset, 1)) != null) {
			$data[] = $row; //$row -> array key & value
		}
	}
	
	// B3) Dong ket noi
	mysqli_close($conn);

	return $data;
}


#utility.php


<?php
// Viet cau truy van theo cau truc: $sql = "???";
function getPost($key) {
	$value = '';

	if(isset($_POST[$key])) {
		$value = $_POST[$key];
		$value = str_replace("'", "\\'", $value);
	}

	return $value;
}

// Viet cau truy van theo cau truc: $sql = "???";
function getGet($key) {
	$value = '';

	if(isset($_GET[$key])) {
		$value = $_GET[$key];
		$value = str_replace("'", "\\'", $value);
	}

	return $value;
}


function getMD5Security($pwd) {
	return md5(md5($pwd).'jhgJHGsdy^&%723GJ67532GHFd');
}


#add.php


<?php
require_once('utils/utility.php');
require_once('db/dbhelper.php');

$fullname = $email = $pwd = "";
if(!empty($_POST)) {
	$fullname = getPost('fullname');
	$email = getPost('email');
	$pwd = getPost('pwd');
	$pwd = getMD5Security($pwd);

	$sql = "insert into student(fullname, email, password) values ('$fullname', '$email', '$pwd')";
	execute($sql);

	header('Location: list.php');
	die();
}
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Add User Page</title>
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}

		.card {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<div class="container">
	<form method="post" onsubmit="return validateData();">
		<div class="form-group">
			<label>Full Name: </label>
			<input required type="text" name="fullname" class="form-control">
		</div>
		<div class="form-group">
			<label>Email: </label>
			<input required type="email" name="email" class="form-control">
		</div>
		<div class="form-group">
			<label>Password: </label>
			<input required type="password" name="pwd" class="form-control">
		</div>
		<div class="form-group">
			<label>Confirm Password: </label>
			<input required type="password" name="confirmPwd" class="form-control">
		</div>
		<div class="form-group">
			<button class="btn btn-success">Save</button>
			<a href="list.php"><button type="button" class="btn btn-secondary">Back</button></a>
		</div>
	</form>
</div>

<script type="text/javascript">
	function validateData() {
		if($('[name=pwd]').val() != $('[name=confirmPwd]').val()) {
			alert('Password does not match')
			return false
		}
		return true
	}
</script>
</body>
</html>


#delete.php


<?php
require_once('utils/utility.php');
require_once('db/dbhelper.php');

$fullname = $email = $pwd = "";
if(!empty($_POST)) {
	$id = getPost('id');

	$sql = "delete from student where id = '$id'";
	execute($sql);

	header('Location: list.php');
	die();
}

$id = getGet('id');
$sql = "select * from student where id = '$id'";
$std = executeResult($sql, true);
if($std == null) {
	header('Location: list.php');
	die();
}
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Add User Page</title>
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}

		.card {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<div class="container">
	<form method="post" onsubmit="return validateData();">
		<h3>Are you sure to delete this user?</h3>
		<div class="form-group">
			<label>Full Name: <?=$std['fullname']?></label>
			<input required type="text" name="id" class="form-control" value="<?=$std['id']?>" style="display: none;">
		</div>
		<div class="form-group">
			<label>Email: <?=$std['email']?></label>
		</div>
		<div class="form-group">
			<button class="btn btn-danger">Confirm Delete</button>
			<a href="list.php"><button type="button" class="btn btn-secondary">Back</button></a>
		</div>
	</form>
</div>

<script type="text/javascript">
	function validateData() {
		if($('[name=pwd]').val() != $('[name=confirmPwd]').val()) {
			alert('Password does not match')
			return false
		}
		return true
	}
</script>
</body>
</html>


#edit.php


<?php
require_once('utils/utility.php');
require_once('db/dbhelper.php');

$fullname = $email = $pwd = "";
if(!empty($_POST)) {
	$fullname = getPost('fullname');
	$email = getPost('email');
	$pwd = getPost('pwd');
	$id = getPost('id');

	if($pwd == '') {
		$sql = "update student set fullname = '$fullname', email = '$email' where id = '$id'";
	} else {
		$pwd = getMD5Security($pwd);
		$sql = "update student set fullname = '$fullname', email = '$email', password = '$pwd' where id = '$id'";
	}
	
	execute($sql);

	header('Location: list.php');
	die();
}

$id = getGet('id');
$sql = "select * from student where id = '$id'";
$std = executeResult($sql, true);
if($std == null) {
	header('Location: list.php');
	die();
}
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Add User Page</title>
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}

		.card {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<div class="container">
	<form method="post" onsubmit="return validateData();">
		<div class="form-group">
			<label>Full Name: </label>
			<input required type="text" name="fullname" class="form-control" value="<?=$std['fullname']?>">
			<input required type="text" name="id" class="form-control" value="<?=$std['id']?>" style="display: none;">
		</div>
		<div class="form-group">
			<label>Email: </label>
			<input required type="email" name="email" class="form-control" value="<?=$std['email']?>">
		</div>
		<div class="form-group">
			<label>Password: </label>
			<input type="password" name="pwd" class="form-control">
		</div>
		<div class="form-group">
			<label>Confirm Password: </label>
			<input type="password" name="confirmPwd" class="form-control">
		</div>
		<div class="form-group">
			<button class="btn btn-success">Save</button>
			<a href="list.php"><button type="button" class="btn btn-secondary">Back</button></a>
		</div>
	</form>
</div>

<script type="text/javascript">
	function validateData() {
		if($('[name=pwd]').val() != $('[name=confirmPwd]').val()) {
			alert('Password does not match')
			return false
		}
		return true
	}
</script>
</body>
</html>


#list.php


<?php
require_once('utils/utility.php');
require_once('db/dbhelper.php');

$sql = "select * from student";
$uList = executeResult($sql);
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>User Management Page</title>
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>

	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}

		.card {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<div class="container">
	<a href="add.php"><button class="btn btn-success mb-3">Add User</button></a>
	<table class="table table-bordered">
		<thead>
			<tr>
				<th>No</th>
				<th>Full Name</th>
				<th>Email</th>
				<th style="width: 60px"></th>
				<th style="width: 60px"></th>
			</tr>
		</thead>
		<tbody>
<?php
$index = 0;
foreach($uList as $item) {
	echo '<tr>
			<td>'.(++$index).'</td>
			<td>'.$item['fullname'].'</td>
			<td>'.$item['email'].'</td>
			<td>
				<a href="edit.php?id='.$item['id'].'"><button class="btn btn-warning">Edit</button></a>
			</td>
			<td>
				<a href="delete.php?id='.$item['id'].'"><button class="btn btn-danger">Delete</button></a>
			</td>
		</tr>';
}
?>
		</tbody>
	</table>
</div>
</body>
</html>


Tags:



Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó