Làm cách nào để gửi biểu mẫu trong PHP?

Chương trình Giáo dục Kỹ thuật (EngEd) này được hỗ trợ bởi Mục. Triển khai tức thì các container trên nhiều nhà cung cấp đám mây trên toàn cầu

Dùng thử miễn phí

Làm việc với Form trong PHP

8 Tháng Ba, 2021

Chúng tôi chủ yếu sử dụng các biểu mẫu HTML khi thu thập thông tin đầu vào của người dùng trong các ứng dụng dựa trên web. Chúng bao gồm các biểu mẫu liên hệ, biểu mẫu đăng nhập và cả biểu mẫu đăng ký. Biểu mẫu là giao diện cơ bản giữa người dùng và máy chủ. Tạo biểu mẫu trên ứng dụng web được thực hiện bằng HTML. PHP kết nối ứng dụng web với máy chủ cơ sở dữ liệu

Thông qua bài viết này, bạn sẽ học được cách

  • Tạo biểu mẫu HTML
  • Tìm hiểu về các phương thức yêu cầu của Giao thức truyền tải siêu văn bản (HTTP) (GET, POST và PUT)
  • Hiểu các phương thức POST và GET của PHP

điều kiện tiên quyết

Trước khi chúng tôi bắt đầu, bạn nên có kiến ​​thức trước về HTML, PHP và MySQL

Tổng quan về các biểu mẫu

Biểu mẫu là một tài liệu có các trường trống để người dùng chèn hoặc cập nhật dữ liệu. Dữ liệu của người dùng được lưu trữ trong cơ sở dữ liệu và được truy xuất bất cứ lúc nào

Sử dụng biểu mẫu để thu thập dữ liệu trong ứng dụng web là một nhiệm vụ đơn giản

Một số hình thức phổ biến bao gồm

  • Biểu mẫu liên hệ
  • Biểu mẫu tìm kiếm
  • Biểu mẫu đăng nhập
  • Mẫu đăng ký

Biểu mẫu được trình bày cho người dùng chèn dữ liệu và gửi biểu mẫu bằng nút

<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
2. Hầu hết thời gian, dữ liệu biểu mẫu được gửi đến máy chủ để xử lý. Xử lý đầu vào của người dùng liên quan đến việc xác thực đầu vào, hoạt động cơ sở dữ liệu và cung cấp phản hồi cho người dùng. Có bốn thao tác cơ sở dữ liệu liên quan, đó là tạo, đọc, cập nhật và xóa. Mẫu này thường được biết đến với từ viết tắt là hoạt động
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
3

Giao thức truyền siêu văn bản (HTTP) cho phép giao tiếp giữa máy khách (trình duyệt) và máy chủ. Một yêu cầu HTTP được gửi bởi một máy khách đến máy chủ, sau đó sẽ trả về một phản hồi. Mặc dù HTTP hỗ trợ một số phương pháp, chúng tôi sẽ tập trung vào

<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
4,
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
5 và
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
6. Dữ liệu được xử lý dựa trên phương pháp đã chọn

Phương thức

<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
4 lấy dữ liệu từ máy chủ. Phương thức
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
5 gửi dữ liệu từ biểu mẫu HTML đến máy chủ để tạo tài nguyên. Phương thức
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
6 gửi dữ liệu đến máy chủ để tạo hoặc cập nhật tài nguyên. Một số nhà phát triển không thể phân biệt giữa phương thức POST và PUT

Phương thức PUT là

<?php
    # Check if name and email fileds are empty
    if(empty($_GET['name']) && empty($_GET['email'])){
        # If the fields are empty, display a message to the user
        echo "Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_GET['name'];
        $email= $_GET['email'];
        echo ('Welcome:     '. $name. '<br/>');
        echo ('This is your email address:'   . $email. '<br/>');
    }
?>
0. Điều này có nghĩa là gọi một phương thức PUT nhiều lần sẽ không ảnh hưởng đến cơ sở dữ liệu vì dữ liệu đã được cập nhật. Ngược lại, gọi một phương thức
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
5 ảnh hưởng đến cơ sở dữ liệu vì bạn tạo nhiều đối tượng

Cách tạo biểu mẫu HTML

Biểu mẫu HTML có thể chứa các phần tử đặc biệt như nút, nút radio và hộp kiểm. Do đó, người dùng dễ dàng tương tác với trang web. Biểu mẫu phải thân thiện với người dùng. Điều này có nghĩa là người dùng không có kỹ năng kỹ thuật sẽ có thể sử dụng nó

Các biểu mẫu được xác định bởi các thẻ

<?php
    # Check if name and email fileds are empty
    if(empty($_GET['name']) && empty($_GET['email'])){
        # If the fields are empty, display a message to the user
        echo "Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_GET['name'];
        $email= $_GET['email'];
        echo ('Welcome:     '. $name. '<br/>');
        echo ('This is your email address:'   . $email. '<br/>');
    }
?>
2. Thẻ
<?php
    # Check if name and email fileds are empty
    if(empty($_GET['name']) && empty($_GET['email'])){
        # If the fields are empty, display a message to the user
        echo "Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_GET['name'];
        $email= $_GET['email'];
        echo ('Welcome:     '. $name. '<br/>');
        echo ('This is your email address:'   . $email. '<br/>');
    }
?>
3 bao quanh tất cả các đầu vào. Nó cũng đưa ra hướng dẫn về cách thức và địa điểm gửi biểu mẫu. Biểu mẫu HTML gửi dữ liệu tới tập lệnh PHP của bạn bằng cách sử dụng phương pháp
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
5 hoặc
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
4

Dưới đây là ví dụ về biểu mẫu gửi dữ liệu đến tệp có tên

<?php
    # Check if name and email fileds are empty
    if(empty($_GET['name']) && empty($_GET['email'])){
        # If the fields are empty, display a message to the user
        echo "Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_GET['name'];
        $email= $_GET['email'];
        echo ('Welcome:     '. $name. '<br/>');
        echo ('This is your email address:'   . $email. '<br/>');
    }
?>
6. Để có mã nguồn hoàn chỉnh, hãy sử dụng mã HTML này và thay đổi phương thức thành POST hoặc GET nếu cần

<!DOCTYPE html>
<html lang="en">
<head>
    <title>HTML Form</title>
</head>
<body>
    <h1>HTML Form</h1>
    <form method="" action="index.php">
        Name: <input type="text" name="name"><br><br/>
        Email: <input type="text" name="email"><br/>
        <br/>
        <input type="submit" value="submit" >
    </form>
</body>
</html>

Đầu ra cho đoạn mã trên như trong ảnh chụp màn hình bên dưới

Làm cách nào để gửi biểu mẫu trong PHP?

<?php
    # Check if name and email fileds are empty
    if(empty($_GET['name']) && empty($_GET['email'])){
        # If the fields are empty, display a message to the user
        echo "Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_GET['name'];
        $email= $_GET['email'];
        echo ('Welcome:     '. $name. '<br/>');
        echo ('This is your email address:'   . $email. '<br/>');
    }
?>
7 xác định trang nơi đầu vào biểu mẫu được gửi. Dữ liệu có thể được gửi trên cùng một trang với biểu mẫu hoặc trên một trang khác.
<?php
    # Check if name and email fileds are empty
    if(empty($_GET['name']) && empty($_GET['email'])){
        # If the fields are empty, display a message to the user
        echo "Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_GET['name'];
        $email= $_GET['email'];
        echo ('Welcome:     '. $name. '<br/>');
        echo ('This is your email address:'   . $email. '<br/>');
    }
?>
8 chỉ định cách dữ liệu được xử lý. Đây có thể là
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
5,
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
4 hoặc
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
6. Phương thức
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
4 thu thập dữ liệu từ máy chủ và gửi nó trong URL. Dữ liệu được gửi qua phương pháp
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
5 được lưu trữ trong phần thân yêu cầu HTTP và không thể nhìn thấy trên URL

phương thức ĐĂNG

<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
5 là một phương pháp siêu toàn cầu, thu thập dữ liệu biểu mẫu và gửi nó đến máy chủ HTTP. Dữ liệu đã nhập được mã hóa và nội dung bị ẩn. Phương thức POST có phạm vi toàn cầu và dữ liệu được truy cập từ bất kỳ tập lệnh nào

Phương thức POST được ưu tiên vì dữ liệu được gửi qua nó không hiển thị trong URL. Phương thức POST cũng rất quan trọng vì dữ liệu không thể được giải mã bằng cách xem nhật ký máy chủ web

POST không có giới hạn về lượng dữ liệu được gửi từ biểu mẫu. Điều này là do dữ liệu được gửi qua phần thân của yêu cầu HTTP. Phương pháp

<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
5 phù hợp với biểu mẫu
<?php 
    $servername = "localhost";
    $username = "root"; # MySQL user
    $password = ""; # MySQL Server root password
    $dbname='crud'; # Database name
    $conn = new mysqli($servername, $username, $password, $dbname);
    if ($conn->connect_error) {
        # Display an error mesage if the connection fails
        die("Connection failed: " . $conn->connect_error);
    }
?>
6

Xử lý dữ liệu biểu mẫu (tập lệnh PHP)

Mã PHP bên dưới có thể được sử dụng để xử lý đầu vào từ biểu mẫu HTML bằng phương thức POST. Mã phải được đặt trong tập lệnh, là mục tiêu biểu mẫu

Nó có thể nằm trong cùng một tập lệnh có biểu mẫu HTML hoặc có thể nằm trên một tập lệnh khác. Trong trường hợp này, chúng ta sẽ có mã PHP trong tập lệnh giống như biểu mẫu HTML

<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>

Đầu ra của đoạn mã trên được hiển thị trong hình động bên dưới

NHẬN phương pháp

<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
4 là phương pháp siêu toàn cầu mặc định thu thập hoặc truy xuất dữ liệu từ máy chủ. Nó có phạm vi toàn cầu. Do đó, dữ liệu được truy cập từ bất kỳ tập lệnh nào trong chương trình. Phương thức GET gửi dữ liệu trong URL

Dữ liệu được truyền qua phương thức này hiển thị trên URL của yêu cầu HTTP. Yêu cầu HTTP có thể được lưu vào bộ đệm và lưu trong lịch sử trình duyệt. Nhược điểm của phương thức GET là không nên sử dụng nó với dữ liệu nhạy cảm như mật khẩu vì nó không an toàn

Phương thức GET có giới hạn về lượng dữ liệu được gửi từ biểu mẫu. Dữ liệu được gửi trên URL phụ thuộc vào hệ điều hành của máy chủ web và loại trình duyệt

Hầu hết các hệ thống có giới hạn _______25_______8 ký tự. Ví dụ tốt nhất về việc sử dụng phương thức GET là với các biểu mẫu của công cụ tìm kiếm. Mã bên dưới có thể được sử dụng để xử lý biểu mẫu HTML với phương thức được đặt là

<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
4

________số 8_______

Đây là đầu ra của ví dụ về phương thức GET

Bảng so sánh các phương thức GET và POST

POST MethodGET MethodBookmarking kết quả là không thể. Kết quả có thể được đánh dấu. Dữ liệu không còn trong lịch sử trình duyệt. Nó bị ẩn. Dữ liệu vẫn còn trong lịch sử trình duyệt. Hiệu suất thấp vì POST không thể giải mã dữ liệu. Hiệu suất cao do tính chất đơn giản của việc hiển thị dữ liệu. An toàn hơnKém an toàn hơnKhông giới hạn lượng dữ liệu gửi đến máy chủ. Giới hạn lượng dữ liệu gửi đến máy chủ. Nó hoạt động với dữ liệu nhạy cảm. Nó không thể hoạt động với dữ liệu nhạy cảm

Các biểu mẫu HTML và các hoạt động CRUD của cơ sở dữ liệu MySQL

Chúng ta sẽ tìm hiểu cách thực hiện các thao tác CRUD trên cơ sở dữ liệu MySQL bằng các biểu mẫu HTML. Chúng ta sẽ học cách sử dụng biểu mẫu HTML để tạo, đọc, cập nhật và xóa dữ liệu khỏi cơ sở dữ liệu MySQL

Đầu tiên, tạo cơ sở dữ liệu MySQL và đặt tên là

include("connect.php")
0. Tạo một bảng có
include("connect.php")
1cột, đặt tên là
include("connect.php")
2

Các cột là

  1. nhận dạng
  2. tên
  3. e-mail

Tạo kết nối máy chủ cơ sở dữ liệu

Sau khi tạo cơ sở dữ liệu và bảng, chúng ta cần một tập lệnh PHP kết nối với máy chủ cơ sở dữ liệu MySQL. Tạo một tệp có tên

include("connect.php")
3 và đặt vào đó đoạn mã sau. Các tập lệnh tạo kết nối đến máy chủ cơ sở dữ liệu MySQL

<?php 
    $servername = "localhost";
    $username = "root"; # MySQL user
    $password = ""; # MySQL Server root password
    $dbname='crud'; # Database name
    $conn = new mysqli($servername, $username, $password, $dbname);
    if ($conn->connect_error) {
        # Display an error mesage if the connection fails
        die("Connection failed: " . $conn->connect_error);
    }
?>

Sau này chúng tôi sẽ bao gồm tệp này bằng cách sử dụng hàm

include("connect.php")
4

include("connect.php")

Tạo nên

Để tạo một bản ghi trong cơ sở dữ liệu, hãy sử dụng mã bên dưới. Sau khi biểu mẫu được gửi qua phương pháp

<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
5, nó sẽ được xử lý và một bản ghi được tạo trong bảng
include("connect.php")
2

<?php
// Include script to make a database connection
include("connect.php");
// Check if input fileds are empty
if(empty($_POST['name']) && empty($_POST['email'])){
    // If the fields are empty, display a message to the user
    echo "Please fill in the fields";
// Process the form data if the input fields are not empty
}else{
    $name= $_POST['name'];
    $email= $_POST['email'];
    echo ('Your Name is:     '. $name. '<br/>');
    echo ('Your Email is:'   . $email. '<br/>');
    # Insert into the database
    $query = "INSERT INTO user(name,email) VALUES ('$name','$email')";
    if (mysqli_query($conn, $query)) {
        echo "New record created successfully !";
    } else {
         echo "Error inserting record: " . $conn->error;
    }
}
?>

Đây là hình thức

<!DOCTYPE html>
<html lang="en">
<head>
    <title>FORMS</title>
</head>
<body>
    <h1>Form</h1>
    <form method="post" action="form-post.php">
        Name: <input type="text" name="name"><br><br/>
        Email: <input type="text" name="email"><br/>
        <br/>
        <input type="submit" name="save" value="submit" >
    </form>
</body>
</html>

Video dưới đây cho thấy cách hoạt động của đoạn mã trên

Ghi chú. Sau khi chúng tôi gửi biểu mẫu đầu vào, một bản ghi mới được tạo trong cơ sở dữ liệu và được hiển thị trong một bảng

Đọc

Đoạn mã bên dưới truy xuất dữ liệu đã chèn và hiển thị nó trong bảng HTML

<?php
# Include script to make a database connection
include("connect.php")
$ Read From the database and display in the table
$query2 = "SELECT * FROM user";
$result = $conn->query($query2);
if ($result->num_rows > 0) {
    # Output data for each row
    echo "<table id='tsa' border='1' id='example' class='table table-striped responsive-utilities table-hover'>
              <thead>
                <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Email</th>
                <th>Action 1</th>
                <th>Action 2</th>
                </tr>
              </thead>
              ";
    while($row = $result->fetch_assoc()) {
       echo "<tr id='green' ",">",
            "<td>", $row["id"],"</td>",
            "<td>", $row["name"],"</td>",
            "<td>", $row["email"],"</td>",
            "<td>",
                "<form action='update.php' method='post'>
                 <input name='id' value='",$row["id"],"' hidden>
                 <button type='submit' name='update' value='update'>Edit</button>
                </form>",
            "</td>",
            "<td>",
                "<form action='form-post.php' method='post'>
                 <input name='id' value='",$row["id"],"' hidden>
                 <button type='submit' name='delete' value='delete'>Delete</button>
                </form>",
            "</td>",
            "</tr>";
    }
    echo  "</table>";
}else {
 echo "No Records!";
}
?>

Dưới đây là hình ảnh động về cách hoạt động của thao tác đọc

Cập nhật

Biểu mẫu HTML được sử dụng để cập nhật dữ liệu hiện có trong cơ sở dữ liệu. Trong trường hợp này, chúng tôi sẽ triển khai chức năng cập nhật.

include("connect.php")
7 được hiển thị khi chúng ta nhấp vào nút
include("connect.php")
8 trong ô của bảng

Lưu ý mã được sử dụng để tạo nút chỉnh sửa trên bảng. Nút

include("connect.php")
9 là nút
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
2 cho biểu mẫu có các trường nhập ẩn

Sau khi nhấp vào nút

include("connect.php")
8,
<?php
// Include script to make a database connection
include("connect.php");
// Check if input fileds are empty
if(empty($_POST['name']) && empty($_POST['email'])){
    // If the fields are empty, display a message to the user
    echo "Please fill in the fields";
// Process the form data if the input fields are not empty
}else{
    $name= $_POST['name'];
    $email= $_POST['email'];
    echo ('Your Name is:     '. $name. '<br/>');
    echo ('Your Email is:'   . $email. '<br/>');
    # Insert into the database
    $query = "INSERT INTO user(name,email) VALUES ('$name','$email')";
    if (mysqli_query($conn, $query)) {
        echo "New record created successfully !";
    } else {
         echo "Error inserting record: " . $conn->error;
    }
}
?>
2 của mục cần chỉnh sửa sẽ được gửi tới tập lệnh
<?php
// Include script to make a database connection
include("connect.php");
// Check if input fileds are empty
if(empty($_POST['name']) && empty($_POST['email'])){
    // If the fields are empty, display a message to the user
    echo "Please fill in the fields";
// Process the form data if the input fields are not empty
}else{
    $name= $_POST['name'];
    $email= $_POST['email'];
    echo ('Your Name is:     '. $name. '<br/>');
    echo ('Your Email is:'   . $email. '<br/>');
    # Insert into the database
    $query = "INSERT INTO user(name,email) VALUES ('$name','$email')";
    if (mysqli_query($conn, $query)) {
        echo "New record created successfully !";
    } else {
         echo "Error inserting record: " . $conn->error;
    }
}
?>
3. Có thể sử dụng phương pháp
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
4 hoặc
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
5

    <td>
        <form action='update.php' method='post'>
            <input name='id' value='",$row["id"],"' hidden>
            <button type='submit' name='update' value='update'>Edit</button>
        </form>
    </td>

Trong tập lệnh

<?php
// Include script to make a database connection
include("connect.php");
// Check if input fileds are empty
if(empty($_POST['name']) && empty($_POST['email'])){
    // If the fields are empty, display a message to the user
    echo "Please fill in the fields";
// Process the form data if the input fields are not empty
}else{
    $name= $_POST['name'];
    $email= $_POST['email'];
    echo ('Your Name is:     '. $name. '<br/>');
    echo ('Your Email is:'   . $email. '<br/>');
    # Insert into the database
    $query = "INSERT INTO user(name,email) VALUES ('$name','$email')";
    if (mysqli_query($conn, $query)) {
        echo "New record created successfully !";
    } else {
         echo "Error inserting record: " . $conn->error;
    }
}
?>
3, một biểu mẫu có dữ liệu khớp với thông tin đã gửi được hiển thị để chỉnh sửa. Sau khi chỉnh sửa hoàn tất, dữ liệu cập nhật sẽ được gửi lại cho tập lệnh để xử lý. Trong trường hợp này, chúng tôi đang sử dụng cùng một tập lệnh để xử lý các yêu cầu của
include("connect.php")
9

Đoạn script dưới đây được sử dụng để cập nhật dữ liệu

<?php
// Include script to make a database connection
include("connect.php");
// Empty string to be used later
$name='';
$email='';
$id='';

// Button click to update using POST method
if(!empty($_POST['update']) && !empty($_POST['id']) )  {
  $id=$_POST['id'];
  // Fetch record with ID and populate it in the form
  $query2 = "SELECT * FROM user WHERE id='".$_POST['id']."' ";
  $result = $conn->query($query2);
  if ($result->num_rows > 0) {
    // Output data for each row
    while($row = $result->fetch_assoc()) {
      $name=$row["name"];
      $email=$row["email"];
    }
    echo "Current Details: " ."<b> - Name:</b> " . $name. " <b>Email:</b>" . $email. "<br>";
  } else {
    echo "Error updating";
  }
}

// Button click to update using GET method
if(!empty($_GET['update']) && !empty($_GET['id']) )  {
  $id=$_GET['id'];
  // Fetch record with id and populate it in the form
  $query2 = "SELECT * FROM user WHERE id='".$_GET['id']."' ";
  $result = $conn->query($query2);
  if ($result->num_rows > 0) {
    // Output data for each row
    while($row = $result->fetch_assoc()) {
      $name=$row["name"];
      $email=$row["email"];
    }
    echo "Current Details: " ."<b> - Name:</b> " . $name. " <b>Email:</b>" . $email. "<br>";
  } else {
    echo "Error updating";
  }
}
// Check that the input fields are not empty and process the data
if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['id']) ){
    // Insert into the database
  $query = "UPDATE user SET name='".$_POST['name']."', email='".$_POST['email']."' WHERE id='".$_POST['id']."' ";
  if (mysqli_query($conn, $query)) {
      echo "Record updated successfully!<br/>";
      echo '<a href="form-get.php">Get Form</a><br/>
            <a href="form-post.php">Post Form</a>';
      die(0);
  } else {
      // Display an error message if unable to update the record
       echo "Error updating record: " . $conn->error;
       die(0);
  }
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>FORM</title>
</head>
<body>
    <h1>Form</h1>
    <p>Edit the record</p>
    <form method="POST" action="update.php">
        ID: <input type="text" name="id" value="<?php echo($id); ?>" required><br><br/>
        Name: <input type="text" name="name" value="<?php echo($name); ?>" required><br><br/>
        Email: <input type="text" name="email" value="<?php echo($email); ?>" required><br/>
        <br/>
        <input type="submit" value="update">
    </form>
</body>
</html>

Video dưới đây cho thấy chức năng cập nhật sẽ hoạt động như thế nào trên trình duyệt

Xóa bỏ

Để xóa một bản ghi trong bảng, người dùng nhấp vào nút

<?php
// Include script to make a database connection
include("connect.php");
// Check if input fileds are empty
if(empty($_POST['name']) && empty($_POST['email'])){
    // If the fields are empty, display a message to the user
    echo "Please fill in the fields";
// Process the form data if the input fields are not empty
}else{
    $name= $_POST['name'];
    $email= $_POST['email'];
    echo ('Your Name is:     '. $name. '<br/>');
    echo ('Your Email is:'   . $email. '<br/>');
    # Insert into the database
    $query = "INSERT INTO user(name,email) VALUES ('$name','$email')";
    if (mysqli_query($conn, $query)) {
        echo "New record created successfully !";
    } else {
         echo "Error inserting record: " . $conn->error;
    }
}
?>
8 trong bảng HTML

Lưu ý mã được sử dụng để hiển thị nút bên trong ô của bảng

<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
0

Nút

<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
2 có trường
<?php
// Include script to make a database connection
include("connect.php");
// Check if input fileds are empty
if(empty($_POST['name']) && empty($_POST['email'])){
    // If the fields are empty, display a message to the user
    echo "Please fill in the fields";
// Process the form data if the input fields are not empty
}else{
    $name= $_POST['name'];
    $email= $_POST['email'];
    echo ('Your Name is:     '. $name. '<br/>');
    echo ('Your Email is:'   . $email. '<br/>');
    # Insert into the database
    $query = "INSERT INTO user(name,email) VALUES ('$name','$email')";
    if (mysqli_query($conn, $query)) {
        echo "New record created successfully !";
    } else {
         echo "Error inserting record: " . $conn->error;
    }
}
?>
2 ẩn. Dữ liệu được gửi đến một biểu mẫu và được xử lý bởi tập lệnh PHP bên dưới. Nếu
<?php
// Include script to make a database connection
include("connect.php");
// Check if input fileds are empty
if(empty($_POST['name']) && empty($_POST['email'])){
    // If the fields are empty, display a message to the user
    echo "Please fill in the fields";
// Process the form data if the input fields are not empty
}else{
    $name= $_POST['name'];
    $email= $_POST['email'];
    echo ('Your Name is:     '. $name. '<br/>');
    echo ('Your Email is:'   . $email. '<br/>');
    # Insert into the database
    $query = "INSERT INTO user(name,email) VALUES ('$name','$email')";
    if (mysqli_query($conn, $query)) {
        echo "New record created successfully !";
    } else {
         echo "Error inserting record: " . $conn->error;
    }
}
?>
2 không trống, thì bản ghi có
<?php
// Include script to make a database connection
include("connect.php");
// Check if input fileds are empty
if(empty($_POST['name']) && empty($_POST['email'])){
    // If the fields are empty, display a message to the user
    echo "Please fill in the fields";
// Process the form data if the input fields are not empty
}else{
    $name= $_POST['name'];
    $email= $_POST['email'];
    echo ('Your Name is:     '. $name. '<br/>');
    echo ('Your Email is:'   . $email. '<br/>');
    # Insert into the database
    $query = "INSERT INTO user(name,email) VALUES ('$name','$email')";
    if (mysqli_query($conn, $query)) {
        echo "New record created successfully !";
    } else {
         echo "Error inserting record: " . $conn->error;
    }
}
?>
2 đã gửi sẽ bị xóa

<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
1

Chức năng xóa hoạt động như trong video bên dưới

Bạn có thể tải xuống mã nguồn từ đây

Phần kết luận

Tóm lại, yêu cầu HTTP cho phép giao tiếp giữa máy khách và máy chủ. Chúng ta đã học cách tạo biểu mẫu HTML và xử lý dữ liệu bằng PHP. Chúng ta cũng đã học về các phương thức POST, PUT và GET

Chúng tôi biết rằng

<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
5 tạo dữ liệu,
<?php
    # Check if name and email fileds are empty
    if(empty($_POST['name']) && empty($_POST['email'])){
        # If the fields are empty, display a message to the user
        echo " <br/> Please fill in the fields";
    # Process the form data if the input fields are not empty
    }else{
        $name= $_POST['name'];
        $email= $_POST['email'];
        echo ('Your Name is:     '. $name. '<br/>');
        echo ('Your Email is:'   . $email. '<br/>');
    }
?>
4 đọc dữ liệu từ máy chủ và PUT cập nhật dữ liệu trong máy chủ. Chúng tôi đã học cách thực hiện các thao tác CRUD trên cơ sở dữ liệu MySQL bằng PHP

Tôi hy vọng bài viết này sẽ làm sáng tỏ và cung cấp cho bạn sự hiểu biết khi làm việc với các biểu mẫu HTML trong PHP

Làm cách nào để gửi biểu mẫu bằng PHP?

Làm cách nào để đăng dữ liệu biểu mẫu lên URL trong PHP?

Đăng dữ liệu biểu mẫu trong PHP. Để đăng dữ liệu biểu mẫu trong PHP, bạn có thể sử dụng thư viện PHP Curl tích hợp sẵn . Bạn cần khởi tạo thư viện PHP Curl trước bằng cách gọi hàm curl_init(). Bạn có thể chuyển URL mục tiêu trong quá trình khởi tạo hoặc đặt nó sau bằng cách gọi hàm curl_setopt() với tham số "CURLOPT_URL".

Nút gửi trong PHP là gì?

Nút Gửi HTML được dùng để gửi dữ liệu biểu mẫu tới tập lệnh được đề cập trong thuộc tính ACTION . Đây là của chúng ta.