By GokiSoft.com|
09:57 20/04/2022|
Học PHP
[Source Code] Tìm hiểu về kết nối CSDL trong PHP - Khoá học PHP/MySQL - C2108G3
#readme.txt
- Hướng dẫn cách sử dụng công cụ phpmyadmin:
User: Tài khoản để truy cập (kết nối) tới CSDL:
Tài khoản root:
- username: root
- pwd: ''
Tạo TK mới:
username: C2108G3
pwd: 9zM2fqDOjXBaAEEK
Host: localhost
- Hướng dẫn làm việc với database:
create database C2108G3
create table student (
id int primary key auto_increment,
fullname varchar(50),
email varchar(150),
pwd varchar(32)
)
#config.php
<?php
define('HOST', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', '');
define('DATABASE', 'c2108g3');
#dbhelper.php
<?php
require_once('config.php');
function execute($sql) {
// B1) Ket noi CSDL
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
mysqli_set_charset($conn, 'utf8');
// B2) Insert du lieu vao database
// echo $sql;die();
// insert, update, delete
mysqli_query($conn, $sql);
// B3) Dong ket noi CSDL
mysqli_close($conn);
}
function executeResult($sql) {
// B1) Ket noi CSDL
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
mysqli_set_charset($conn, 'utf8');
// B2) Insert du lieu vao database
// echo $sql;die();
// insert, update, delete
$resultset = mysqli_query($conn, $sql);
$data = [];
while(($row = mysqli_fetch_array($resultset, 1)) != null) {
$data[] = $row;
}
// B3) Dong ket noi CSDL
mysqli_close($conn);
return $data;
}
#index.php
<?php
require_once('utility.php');
require_once('dbhelper.php');
require_once('process_form_register.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">
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>
<th style="width: 50px"></th>
</tr>
</thead>
<tbody>
<?php
$sql = "select * from student";
$data = executeResult($sql);
$index = 0;
foreach ($data as $item) {
echo '<tr>
<td>'.($index + 1).'</td>
<td>'.$item['fullname'].'</td>
<td>'.$item['email'].'</td>
<td>'.$item['pwd'].'</td>
<td>
<button class="btn btn-danger">Delete</button>
</td>
</tr>';
$index++;
}
?>
</tbody>
</table>
</div>
</div>
<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>
#process_form_register.php
<?php
// Xu ly -> doc duoc du lieu gui tu client len server
$fullname = $email = $pwd = "";
if(!empty($_POST)) {
// if(isset($_POST['fullname'])) {
// $fullname = $_POST['fullname'];
// $fullname = str_replace("'", "\'", $fullname);
// }
// if(isset($_POST['email'])) {
// $email = $_POST['email'];
// $email = str_replace("'", "\'", $email);
// }
// if(isset($_POST['pwd'])) {
// $pwd = $_POST['pwd'];
// $pwd = str_replace("'", "\'", $pwd);
// }
$fullname = getPost('fullname');
$email = getPost('email');
$pwd = getPost('pwd');
$sql = "insert into student (fullname, email, pwd) values ('$fullname', '$email', '$pwd')";
execute($sql);
}
#utility.php
<?php
function getPost($key, $special = "'") {
$value = '';
if(isset($_POST[$key])) {
$value = $_POST[$key];
//Huy ky tu dac biet trong $value
$value = str_replace($special, "\\".$special, $value);
}
return $value;
}
function getGet($key, $special = "'") {
$value = '';
if(isset($_GET[$key])) {
$value = $_GET[$key];
//Huy ky tu dac biet trong $value
$value = str_replace($special, "\\".$special, $value);
}
return $value;
}
function getCookie($key, $special = "'") {
$value = '';
if(isset($_COOKIE[$key])) {
$value = $_COOKIE[$key];
//Huy ky tu dac biet trong $value
$value = str_replace($special, "\\".$special, $value);
}
return $value;
}
function getSession($key, $special = "'") {
$value = '';
if(isset($_SESSION[$key])) {
$value = $_SESSION[$key];
//Huy ky tu dac biet trong $value
$value = str_replace($special, "\\".$special, $value);
}
return $value;
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)