Workflow
ExecuteMySQLFromPHP-Step2_ThucThiTruyVan_ExecuteSQLQuery_INSERT
Step 1: tạo file vidu_insert.php
Các bước thực hiện câu lệnh MySQL trong PHP như sau:
- Tạo kết nối đến database mong muốn (nhúng file
dbconnect.php vào file đang viết code)
- Chuẩn bị câu lệnh (QUERY) mong muốn:
- Thường sử dụng tên biến
$sql
- Yêu cầu PHP thực thi câu lệnh trên bằng hàm
mysqli_query($conn, $sql)
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <meta http-equiv="X-UA-Compatible" content="ie=edge">
7 <title>Ví dụ INSERT dữ liệu</title>
8</head>
9<body>
10 <?php
11 // Truy vấn database để lấy danh sách
12 // 1. Include file cấu hình kết nối đến database, khởi tạo kết nối $conn
13 // Ví dụ: file đang viết code ở /example/thuc_thi_cau_lenh_insert.php
14 // C:\xampp\htdocs\web02\dbconnect.php vào file đang viết code
15 //
16 include_once(__DIR__ . '/../dbconnect.php');
17
18 // 2. Chuẩn bị QUERY
19 $tenhinhthucthanhtoan = 'Thanh toán SHIPCOD (giao hàng mới trả tiền)'; //$_POST['httt_ten'];
20 $sql = "INSERT INTO `hinhthucthanhtoan`(httt_ten) VALUES('$tenhinhthucthanhtoan');";
21
22 // 3. Thực thi
23 mysqli_query($conn, $sql);
24 ?>
25</body>
26</html>