By GokiSoft.com|
15:48 15/06/2020|
Học PHP
[Share Code] Tìm hiểu PHP & MySQL qua ví dụ quản lý người dùng - Lập trình PHP
#Hướng dẫn học PHP & MySQL
B1. Tạo form dữ liệu nhập thông tin người dùng => Dùng form có sẵn
B2. Tạo CSDL : quanlysinhvien
- Tảo bảng user
create table user (
id int primary key auto_increment,
fullname varchar(50) not null,
email varchar(150),
birthday date,
password varchar(32),
address varchar(250)
)
B3. Thao tác dữ liệu PHP & MySQL
#Cấu hình kết nối CSDL - config.php
<?php
define('HOST', 'localhost');
define('DATABASE', 'quanlysinhvien');
define('USERNAME', 'root');
define('PASSWORD', '');
#Hàm sử thao tác với CSDL > dbhelper.php
<?php
//insert, update, delete
function execute($sql) {
//insert, update, delete du lieu vao database
// start : insert into database
// tao ket noi toi CSDL
$con = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
// Tao truy van
mysqli_query($con, $sql);
// Dong ket noi
mysqli_close($con);
// ends
}
function execute_result($sql) {
// start : select from database
// tao ket noi toi CSDL
$con = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
// Tao truy van
$resultset = mysqli_query($con, $sql);
$resultList = [];
while ($row = mysqli_fetch_array($resultset, 1)) {
$resultList[] = $row;
}
// Dong ket noi
mysqli_close($con);
return $resultList;
}
#Xử lý dữ liệu form > process-form.php
<?php
$fullname = $email = $birthday = $password = $confirmation_pwd = $address = '';
if (!empty($_POST)) {
if (isset($_POST['fullname'])) {
$fullname = $_POST['fullname'];
}
if (isset($_POST['email'])) {
$email = $_POST['email'];
}
if (isset($_POST['birthday'])) {
$birthday = $_POST['birthday'];
}
if (isset($_POST['password'])) {
$password = $_POST['password'];
}
if (isset($_POST['confirmation_pwd'])) {
$confirmation_pwd = $_POST['confirmation_pwd'];
}
if (isset($_POST['address'])) {
$address = $_POST['address'];
}
if ($password != $confirmation_pwd) {
//thong bao error ra man hinh
} else {
$sql = "insert into user(fullname, email, birthday, password, address) values ('$fullname', '$email', '$birthday', '$password', '$address')";
execute($sql);
}
}
#Trang giao diện website > student.php
<?php
require_once ('config.php');
require_once ('dbhelper.php');
require_once ('process-form.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Registation Form * Form Tutorial</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h2 class="text-center">Registation Form - Input User's Detail Information</h2>
</div>
<div class="panel-body">
<form method="post" action="">
<div class="form-group">
<label for="usr">Name:</label>
<input required="true" type="text" class="form-control" id="usr" name="fullname" value="<?=$fullname?>">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input required="true" type="email" class="form-control" id="email" name="email" value="<?=$email?>">
</div>
<div class="form-group">
<label for="birthday">Birthday:</label>
<input type="date" class="form-control" id="birthday" value="<?=$birthday?>" name="birthday">
</div>
<div class="form-group">
<label for="pwd">Password: <font color="red"><?=($password != $confirmation_pwd)?'Mật khẩu không khớp':''?></font></label>
<input required="true" type="password" class="form-control" id="pwd" name="password">
</div>
<div class="form-group">
<label for="confirmation_pwd">Confirmation Password:</label>
<input required="true" type="password" class="form-control" id="confirmation_pwd" name="confirmation_pwd">
</div>
<div class="form-group">
<label for="address">Address:</label>
<input type="text" class="form-control" id="address" name="address" value="<?=$address?>">
</div>
<button class="btn btn-success">Register</button>
</form>
</div>
</div>
<div class="panel panel-primary">
<div class="panel-body">
<table class="table table-bordered">
<thead>
<tr>
<th>STT</th>
<th>Fullname</th>
<th>Email</th>
<th>Birthday</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php
$sql = "select * from user";
$userList = execute_result($sql);
// ends
foreach ($userList as $row) {
echo '<tr>
<td>'.$row['id'].'</td>
<td>'.$row['fullname'].'</td>
<td>'.$row['email'].'</td>
<td>'.$row['birthday'].'</td>
<td>'.$row['address'].'</td>
</tr>';
}
?>
</tbody>
</table>
</div>
</div>
</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)