Hướng dẫn php insert data into mysql from form - php chèn dữ liệu vào mysql từ biểu mẫu


Chèn dữ liệu vào MySQL bằng MySQLI và PDO

Sau khi một cơ sở dữ liệu và một bảng đã được tạo, chúng ta có thể bắt đầu thêm dữ liệu vào chúng.

Dưới đây là một số quy tắc cú pháp để tuân theo:

  • Truy vấn SQL phải được trích dẫn trong PHP
  • Các giá trị chuỗi bên trong truy vấn SQL phải được trích dẫn
  • Giá trị số không được trích dẫn
  • Từ null không được trích dẫn

Câu lệnh INSERT được sử dụng để thêm các bản ghi mới vào bảng MySQL:

Chèn vào các giá trị Table_Name (cột1, cột2, cột3, ...) (value1, value2, value3, ...)
VALUES (value1, value2, value3,...)

Để tìm hiểu thêm về SQL, vui lòng truy cập hướng dẫn SQL của chúng tôi.

Trong chương trước, chúng tôi đã tạo một bảng trống có tên "MyGuests" với năm cột: "ID", "FirstName", "LastName", "Email" và "Reg_date". Bây giờ, chúng ta hãy điền vào bảng với dữ liệu.

Lưu ý: Nếu một cột là Auto_increment (như cột "id") hoặc dấu thời gian với bản cập nhật mặc định của current_timesamp (như cột "reg_date"), nó không cần phải được chỉ định trong truy vấn SQL; MySQL sẽ tự động thêm giá trị. If a column is AUTO_INCREMENT (like the "id" column) or TIMESTAMP with default update of current_timesamp (like the "reg_date" column), it is no need to be specified in the SQL query; MySQL will automatically add the value.

Các ví dụ sau đây thêm một bản ghi mới vào bảng "MyGuests":

Ví dụ (hướng đối tượng mysqli) theo định hướng đối tượng)

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Tạo kết nối $ Conn = new mysqli ($ servername, $ username, $ password, $ dbname); // kiểm tra kết nối if ($ Conn-> Connect_error) & nbsp; {& nbsp; & nbsp; die ("Kết nối không thành công:".
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$ sql = "chèn vào myguests (firstName, lastName, email) giá trị ('john', 'doe', '' ')";
VALUES ('John', 'Doe', '')";

if ($ Conn-> truy vấn ($ sql) === true) {& nbsp; echo "bản ghi mới được tạo thành công";} other {& nbsp; Echo "Lỗi:". $ SQL. "". $ Conn-> lỗi;}
  echo "New record created successfully";
} else {
  echo "Error: " . $sql . "
" . $conn->error;
}

$ Conn-> đóng (); ?>
?>




Ví dụ (thủ tục MySQLI)

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Tạo kết nối $ Conn = new mysqli ($ servername, $ username, $ password, $ dbname); // kiểm tra kết nối if ($ Conn-> Connect_error) & nbsp; {& nbsp; & nbsp; die ("Kết nối không thành công:".
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}

$ sql = "chèn vào myguests (firstName, lastName, email) giá trị ('john', 'doe', '' ')";
VALUES ('John', 'Doe', '')";

if ($ Conn-> truy vấn ($ sql) === true) {& nbsp; echo "bản ghi mới được tạo thành công";} other {& nbsp; Echo "Lỗi:". $ SQL. "". $ Conn-> lỗi;}
  echo "New record created successfully";
} else {
  echo "Error: " . $sql . "
" . mysqli_error($conn);
}

$ Conn-> đóng (); ?>
?>


Ví dụ (thủ tục MySQLI)

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

// Tạo kết nối $ Conn = new mysqli ($ servername, $ username, $ password, $ dbname); // kiểm tra kết nối if ($ Conn-> Connect_error) & nbsp; {& nbsp; & nbsp; die ("Kết nối không thành công:".
  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $sql = "INSERT INTO MyGuests (firstname, lastname, email)
  VALUES ('John', 'Doe', '')";
  // use exec() because no results are returned
  $conn->exec($sql);
  echo "New record created successfully";
} catch(PDOException $e) {
  echo $sql . "
" . $e->getMessage();
}

$ sql = "chèn vào myguests (firstName, lastName, email) giá trị ('john', 'doe', '' ')";
?>




Trong bài viết này, chúng tôi sẽ lưu trữ dữ liệu trong cơ sở dữ liệu được gửi qua Mẫu HTML. & NBSP;

Requirements:

  • Máy chủ XAMPP (Máy chủ web)
  • HTML
  • PHP
  • Mysql

Hình thức HTML: Đầu tiên chúng tôi tạo một biểu mẫu HTML cần lấy đầu vào người dùng từ bàn phím. Biểu mẫu HTML là một tài liệu lưu trữ thông tin của người dùng trên máy chủ web bằng cách sử dụng các điều khiển tương tác. Một biểu mẫu HTML chứa các loại thông tin khác nhau như tên người dùng, mật khẩu, số liên hệ, ID email, v.v. First we create an HTML form that need to take user input from keyboard. HTML form is a document which stores information of a user on a web server using interactive controls. An HTML form contains different kind of information such as username, password, contact number, email id etc.

Các yếu tố được sử dụng trong biểu mẫu HTML là hộp kiểm, hộp nhập, nút radio, gửi nút, v.v. Với sự trợ giúp của các yếu tố này, thông tin của người dùng được gửi trên máy chủ web. Thẻ biểu mẫu được sử dụng để tạo biểu mẫu HTML.form tag is used to create an HTML form.

Syntax:

Form Elements...

hoặc

Để chuyển các giá trị cho trang tiếp theo, chúng tôi sử dụng tên trang với cú pháp sau. Chúng ta có thể sử dụng phương thức GET hoặc POST để gửi dữ liệu đến máy chủ.

Form Elements...

Kết nối cơ sở dữ liệu: Việc thu thập dữ liệu liên quan được gọi là cơ sở dữ liệu. XAMPP là viết tắt của đa nền tảng, Apache, MySQL, PHP và Perl. Đây là một trong những máy chủ địa phương trọng lượng nhẹ đơn giản để phát triển trang web. Trong PHP, chúng ta có thể kết nối với cơ sở dữ liệu bằng máy chủ web localhost XAMPP.: The collection of related data is called a database. XAMPP stands for cross-platform, Apache, MySQL, PHP, and Perl. It is among the simple light-weight local servers for website development. In PHP, we can connect to database using localhost XAMPP web server.

Syntax:

PHP

$servername = "localhost";

$username = "username";

Form Elements...
1 =
Form Elements...
3;

Form Elements...
5 =
Form Elements...
7;

Form Elements...
9 = 1 2$servername4

5$username7

Form Elements...
17
Form Elements...
5$servername1

$servername2 $servername3

Form Elements...
9$servername5

5$servername7$servername3$servername9

= 0= 1

Form Elements...
9= 3

= 4

= 5 = 6

5$servername3= 97"localhost"17"localhost"3"localhost"4

$servername2 $servername3

Form Elements...
9"localhost"8"localhost"9;0

5;2 ;3;

;5;6 ;7

5;2 $username0 = 1"localhost"9 = 1$username4 = 1__

= 4

Cách nhận dữ liệu biểu mẫu: Chúng tôi sẽ thu thập dữ liệu biểu mẫu được gửi qua biểu mẫu HTML. Phương pháp PHP $ _Request là biến Super Global php được sử dụng để thu thập dữ liệu sau khi gửi biểu mẫu HTML.We are going to collect the form data submitted through HTML form. PHP $_REQUEST method is a PHP super global variable which is used to collect data after submitting the HTML form.

Syntax:

PHP

Is

5= 9 = "username"1= 3$servername"username"4

5$servername2 $servername3"username"8$servername3= 9

Form Elements...
01

= 0;2

Form Elements...
04;

5;5;6 ;7

= 0;2 = 9;

5= 4

= 4

Form Elements...
17

Form Elements...
9
Form Elements...
19

Form Elements...
17

Hoàn thành các bước để thiết kế dự án:

  • Bắt đầu máy chủ XAMPP.
  • Mở LocalHost/PHPMyAdmin trong trình duyệt web của bạn.
  • Tạo cơ sở dữ liệu của nhân viên tên và bảng của trường đại học.staff and table of name college.
  • Viết mã HTML và PHP trong notepad của bạn vào một thư mục cụ thể.
  • Gửi dữ liệu thông qua biểu mẫu HTML.
  • Xác minh kết quả.

Các bước chi tiết: & nbsp; 

  • Bắt đầu XAMPP Server bằng cách mở XAMPP và nhấp vào XAMPP Start. & NBSP; & NBSP;
     

  • Mở LocalHost/PHPMyAdminin Trình duyệt web của bạn và tạo cơ sở dữ liệu với tên cơ sở dữ liệu làm nhân viên và nhấp vào Tạo. & NBSP; & NBSP;localhost/phpmyadminin your web browser and create database with database name as staff and click on create. 
     

Hướng dẫn php insert data into mysql from form - php chèn dữ liệu vào mysql từ biểu mẫu

  • Sau đó tạo tên bảng Cao đẳng. & Nbsp; & nbsp;college
     

Hướng dẫn php insert data into mysql from form - php chèn dữ liệu vào mysql từ biểu mẫu

  • Nhập các cột và nhấp vào Lưu

  • Bây giờ Mở Notepad và bắt đầu viết mã PHP và lưu nó dưới dạng index.php và mở notepad khác và lưu nó dưới dạng chèn.php lưu cả hai tệp trong một thư mục trong HTDOCS.htdocs.

Tên tệp: index.php

PHP

Form Elements...
21

Form Elements...
22
Form Elements...
23
Form Elements...
24

Form Elements...
25
Form Elements...
26

Form Elements...
27
Form Elements...
28

Form Elements...
25
Form Elements...
30

Form Elements...
25
Form Elements...
32

Form Elements...
27
Form Elements...
34

Form Elements...
35
Form Elements...
36

Is

Form Elements...
43

Form Elements...
44
Form Elements...
45
Form Elements...
46
Form Elements...
47
Form Elements...
48
Form Elements...
49

5= 9 = "username"1= 3$servername"username"4

Form Elements...
58
Form Elements...
59

Form Elements...
43

Form Elements...
44
Form Elements...
45
Form Elements...
46
Form Elements...
47
Form Elements...
65
Form Elements...
66

5$servername2 $servername3"username"8$servername3= 9

Form Elements...
01

Form Elements...
58
Form Elements...
59

Form Elements...
43

Form Elements...
44
Form Elements...
45
Form Elements...
46
Form Elements...
47
Form Elements...
82
Form Elements...
83

= 0;2

Form Elements...
04;

Form Elements...
58
Form Elements...
59

Form Elements...
43

Form Elements...
44
Form Elements...
45
Form Elements...
46
Form Elements...
47
Form Elements...
9900

5;5;6 ;7

Form Elements...
58
Form Elements...
59

Form Elements...
43

Form Elements...
44
Form Elements...
45
Form Elements...
46
Form Elements...
471617

= 0;2 = 9;

Form Elements...
58
Form Elements...
59

Hoàn thành các bước để thiết kế dự án:

Form Elements...
3535

Form Elements...
2737

Form Elements...
2539

40

Bắt đầu máy chủ XAMPP.

PHP

Is

42

Form Elements...
26

545

Form Elements...
30

Form Elements...
32

5

Form Elements...
34

= 0

5= 9 = "username"1= 3$servername"username"4

5$servername2 $servername3"username"8$servername3= 9

Form Elements...
01

Form Elements...
58$servername7$servername371

7273

= 0= 4

= 0;2

Form Elements...
04;

5;5;6 ;7

= 0;2 = 9;

Hoàn thành các bước để thiết kế dự án:

Bắt đầu máy chủ XAMPP.

Mở LocalHost/PHPMyAdmin trong trình duyệt web của bạn.

Form Elements...
58$servername174$servername194$servername214$servername23$servername24

= 0$servername2$servername27

Form Elements...
97"localhost"9$servername31

Tạo cơ sở dữ liệu của nhân viên tên và bảng của trường đại học.

72= 1$servername37

72= 1$servername40;

Viết mã HTML và PHP trong notepad của bạn vào một thư mục cụ thể.

72= 1$servername49$servername1

= 0;5;6;7

Form Elements...
58;2 $servername57

72$servername59

Form Elements...
9$servername1

= 0= 4

= 0$servername65

Form Elements...
9$servername1

= 0

Form Elements...
17

537

39

40

Đầu ra: Nhập LocalHost/7058/index.php trong trình duyệt của bạn, nó sẽ hiển thị biểu mẫu. Sau khi gửi biểu mẫu, dữ liệu biểu mẫu được gửi vào cơ sở dữ liệu.Type localhost/7058/index.php in your browser, it will display the form. After submitting the form, the form data is submitted into database.

Hướng dẫn php insert data into mysql from form - php chèn dữ liệu vào mysql từ biểu mẫu

Hãy để kiểm tra cơ sở dữ liệu của chúng tôi

PHP là ngôn ngữ kịch bản phía máy chủ được thiết kế dành riêng cho phát triển web. Bạn có thể học PHP từ đầu bằng cách làm theo hướng dẫn PHP và các ví dụ PHP này.


Làm thế nào chúng ta có thể lưu trữ dữ liệu biểu mẫu trong cơ sở dữ liệu MySQL bằng PHP?

Máy chủ XAMPP (Máy chủ web) HTML. PHP. Mysql ...
Bắt đầu XAMPP Server ..
Mở LocalHost/PHPMyAdmin trong trình duyệt web của bạn ..
Tạo cơ sở dữ liệu của nhân viên tên và bảng của trường đại học ..
Viết mã HTML và PHP trong notepad của bạn vào một thư mục cụ thể ..
Gửi dữ liệu thông qua biểu mẫu HTML ..
Xác minh kết quả ..

Làm cách nào để chèn dữ liệu vào MySQL từ biểu mẫu HTML bằng Python?

Chèn một hàng duy nhất vào bảng MySQL từ Python..
Kết nối với MySQL từ Python. ....
Xác định truy vấn chèn SQL. ....
Nhận đối tượng con trỏ từ kết nối. ....
Thực thi truy vấn chèn bằng phương thức Execute (). ....
Cam kết thay đổi của bạn. ....
Nhận số lượng hàng bị ảnh hưởng. ....
Xác minh kết quả bằng cách sử dụng truy vấn chọn SQL ..

Làm cách nào tôi có thể chèn nhiều hàng vào bảng HTML trong MySQL bằng PHP?

Chèn nhiều hàng vào một bảng.Người ta cũng có thể chèn nhiều hàng vào bảng với một truy vấn chèn cùng một lúc.Để thực hiện điều này, bao gồm nhiều danh sách các giá trị cột trong phần chèn vào câu lệnh, trong đó các giá trị cột cho mỗi hàng phải được đặt trong dấu ngoặc đơn và được phân tách bằng dấu phẩy.include multiple lists of column values within the INSERT INTO statement, where column values for each row must be enclosed within parentheses and separated by a comma.

Làm cách nào để kết nối biểu mẫu HTML với cơ sở dữ liệu MySQL mà không cần sử dụng PHP?

Kết nối HTML với cơ sở dữ liệu MySQL..
$ con = mysqli_connect ('localhost', 'root', '', 'db_connect') ;.
DB DB_Connect là tên cơ sở dữ liệu của chúng tôi mà chúng tôi đã tạo trước đó ..
Sau khi kết nối cơ sở dữ liệu, bạn cần lấy biến bài viết từ biểu mẫu.....
$ txtName = $ _Post ['txtName'] ;.
$ txtemail = $ _post ['txtemail'] ;.
$ txtphone = $ _post ['txtphone'] ;.