By GokiSoft.com|
15:00 20/04/2022|
Học PHP
[Source Code] Tìm hiều kết nối CSDL trong PHP - Khóa học PHP/MySQL
Tham khao video sau
#readme.txt
Nội dung kiến thức:
- Kết nối CSDL
=============================================
Thêm sinh viên mới vào CSDL
Lấy thông tin danh sách sinh viên -> Hiển thị ra website
Phần 1: Giới thiệu về MySQL
- phpmyadmin
- Tìm hiểu về user: Thông tin tài khoản kết nối tới CSDL
- Tài khoản mặc định (root)
- User Name: root
- Pwd:
- Tạo 1 tài khoản mới:
- User Name: C2110I
- Pwd: 8pAsyPJl5wqd5IAr
- Tên máy tính cài CSDL:
- localhost
Phần 2: Thiết kế database
- Tạo CSDL: C2110I
- Tao bảng student
create table student (
id int primary key auto_increment,
fullname varchar(50),
email varchar(150),
password varchar(32)
)
Phần 3: Lập trình
#add.php
<?php
require_once('utility.php');
require_once('dbhelper.php');
require_once('process_form_add.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Quan Ly Sinh Vien</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">
<div class="card">
<div class="card-header bg-info text-white">
Nhap Thong Tin Sinh Vien
</div>
<div class="card-body">
<form method="post">
<div class="form-group">
<label>Full Name: </label>
<input type="text" name="fullname" class="form-control">
</div>
<div class="form-group">
<label>Email: </label>
<input type="email" name="email" class="form-control">
</div>
<div class="form-group">
<label>Password: </label>
<input type="password" name="pwd" class="form-control">
</div>
<div class="form-group">
<button class="btn btn-info">Register</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
#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) {
// 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 = [];
while(($row = mysqli_fetch_array($resultset, 1)) != null) {
$data[] = $row;
}
// B3) Dong ket noi
mysqli_close($conn);
return $data;
}
#process_form_add.php
<?php
$fullname = $email = $pwd = "";
//Phan 1: Lay dc du lieu gui tu client len server
if(!empty($_POST)) {
// if(isset($_POST['fullname'])) {
// $fullname = $_POST['fullname'];
// $fullname = str_replace("'", "\\'", $fullname);
// }
$fullname = getPost('fullname');
// if(isset($_POST['email'])) {
// $email = $_POST['email'];
// $email = str_replace("'", "\\'", $email);
// }
$email = getPost('email');
// if(isset($_POST['pwd'])) {
// $pwd = $_POST['pwd'];
// $pwd = str_replace("'", "\\'", $pwd);
// }
$pwd = getPost('pwd');
$sql = "insert into student(fullname, email, password) values ('$fullname', '$email', '$pwd')";
execute($sql);
}
#utility.php
<?php
function getPost($key) {
$value = '';
if(isset($_POST[$key])) {
$value = $_POST[$key];
$value = str_replace("'", "\\'", $value);
}
return $value;
}
function getGet($key) {
$value = '';
if(isset($_GET[$key])) {
$value = $_GET[$key];
$value = str_replace("'", "\\'", $value);
}
return $value;
}
// function getCookie($key) {
// $value = '';
// if(isset($_COOKIE[$key])) {
// $value = $_COOKIE[$key];
// $value = str_replace("'", "\\'", $value);
// }
// return $value;
// }
// function getSession($key) {
// $value = '';
// if(isset($_SESSION[$key])) {
// $value = $_SESSION[$key];
// $value = str_replace("'", "\\'", $value);
// }
// return $value;
// }
#list.php
<?php
require_once('utility.php');
require_once('dbhelper.php');
$sql = "select * from student";
$data = executeResult($sql);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Quan Ly Sinh Vien</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">
<div class="card">
<div class="card-header bg-info text-white">
Danh Sach Sinh Vien
</div>
<div class="card-body">
<table class="table table-bordered">
<thead>
<tr>
<th>No</th>
<th>Full Name</th>
<th>Email</th>
<th>Password</th>
</tr>
</thead>
<tbody>
<?php
$index = 0;
foreach ($data as $item) {
echo '<tr>
<td>'.($index + 1).'</td>
<td>'.$item['fullname'].'</td>
<td>'.$item['email'].'</td>
<td>'.$item['password'].'</td>
</tr>';
$index++;
}
?>
</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)