You are on page 1of 12

PHP

• Tìm kiếm
• Thêm, xóa, sửa
Thao tác cơ sở dữ liệu
1. Get Input

request

2.1 Tạo SQL

2.2 Thực thi SQL


Database
response

3. Output
Trang chủ: index.php
1. Get Input

2.1 Tạo SQL

2.2 Thực thi SQL


Database
response

3. Output
Trang chuyên mục: chuyen-muc.php?id=1
1. Get Input

id

2.1 Tạo SQL

2.2 Thực thi SQL


Database
response

3. Output

$id = isset($_REQUEST["id"]) ? $_REQUEST["id"] * 1 : 0; select * from grab_category where id = 2


$sql = "select * from grab_content where cid={$id}"; select count(*) as c from grab_content where cid = 2
$result = select_list($sql); select * from grab_content where cid = 2 limit 10 offset 20
Chi tiết: chi-tiet.php?id=5
1. Get Input

id

2.1 Tạo SQL

2.2 Thực thi SQL


Database
response

3. Output

$id = isset($_REQUEST["id"]) ? $_REQUEST["id"] * 1 : 0;


$sql = "select * from grab_content where id={$id}";
$result = select_one($sql);
Thêm mới: form điền thông tin bài viết
add.php

2 $sql = "select * from grab_category";


$cates = select_list($sql);

<?php foreach ($cates as $item) {?>


<option value="<?php echo $item["id"]?>"><?php echo $item["name"]?></option>
<?php } ?>
Thêm mới: thực hiện thêm mới bài viết và kết quả
add_exec.php

2.1 Tạo SQL


$sql = data_to_sql_insert($tbl, $data);
1. Get Input 2.2 Thực thi sql
$cid = isset($_REQUEST["cid"]) ? $_REQUEST["cid"] : 0; $ret = exec_update($sql);
$img = upload_file_by_name("img");
$title = isset($_REQUEST["title"]) ? $_REQUEST["title"] : "";
$code = isset($_REQUEST["code"]) ? $_REQUEST["code"] : "";
$description = isset($_REQUEST["description"]) ? $_REQUEST["description"] : "";
$body = isset($_REQUEST["body"]) ? $_REQUEST["body"] : "";
$status = isset($_REQUEST["status"]) ? $_REQUEST["status"] : 0;
Sửa: form điền thông tin bài viết được lấy từ db
edit.php?id=958
1 $id = isset($_REQUEST["id"]) ? $_REQUEST["id"] : 0;
2 $sql = "select * from grab_content where id={$id}";
$result = select_one($sql);

Chú ý phần hiển thị Category và Image trong code edit.php dòng 69 và 76.
Sửa: thực hiện sửa bài viết và kết quả
edit_exec.php

2.1 Tạo SQL


$sql = data_to_sql_update($tbl,$data,$cond);
1. Get Input 2.2 Thực thi sql
$cid = isset($_REQUEST["cid"]) ? $_REQUEST["cid"] : 0; $ret = exec_update($sql);
$img = upload_file_by_name("img");
$title = isset($_REQUEST["title"]) ? $_REQUEST["title"] : "";
$code = isset($_REQUEST["code"]) ? $_REQUEST["code"] : "";
$description = isset($_REQUEST["description"]) ? $_REQUEST["description"] : "";
$body = isset($_REQUEST["body"]) ? $_REQUEST["body"] : "";
$status = isset($_REQUEST["status"]) ? $_REQUEST["status"] : 0;
Xóa: Xác nhận thông tin bài viết được lấy từ db
delete.php?id=958

1 $id = isset($_REQUEST["id"]) ? $_REQUEST["id"] : 0;


2 $sql = "select * from grab_content where id={$id}";
$result = select_one($sql);

Chú ý phần hiển thị Category và Image (sv tự làm).


Xóa: thực hiện xóa bài viết và kết quả
delete_exec.php

1. Get Input

$id = isset($_REQUEST["id"]) ? $_REQUEST["id"] * 1 : 0;

2.1 Tạo SQL


$sql = "delete from grab_content WHERE id =$id" ;

2.2 Thực thi sql


$ret = exec_update($sql);
Quản lý bài viết
search.php?q=Ha

$q = isset($_REQUEST["q"]) ?
$_REQUEST["q"] : ''; 1
$cond = "";
if ($q){
$sq = sql_str($q); 2
$cond = "where ";
$cond .= " title like '%{$sq}%' ";
}

$sql = "select * from grab_content


{$cond} order by id desc
limit 10 ";

$result = select_list($sql);

SV tự làm phân trang và mở rộng điều kiện tìm kiếm.

You might also like