Home >Backend Development >PHP Tutorial >How to handle data export and import in forms using PHP

How to handle data export and import in forms using PHP

PHPz
PHPzOriginal
2023-08-10 23:40:561191browse

How to handle data export and import in forms using PHP

How to use PHP to handle data export and import in forms

Overview:
In web development, forms are used for data interaction between users and servers One of the important ways. After the user submits the form data, the server needs to process the data and perform operations such as saving, exporting or importing. This article will introduce how to use PHP to process data in forms and implement data export and import functions.

1. Receive and process form data
First, we need to create a form in HTML and use the POST method to send the form data to the server. In PHP, form data is received through the $_POST global variable. For example, we have a form containing name and email fields. We can obtain data through the following code:

$name = $_POST['name'];
$email = $_POST['email'];

Then, we can process the received data, such as verifying the legality of the data, cleaning the data, Store data etc. In this example, we can use regular expressions to validate the email and store the data in the database.

// 邮箱验证
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "邮箱格式不正确";
} else {
    // 数据库存储操作
    $conn = new mysqli('localhost', 'username', 'password', 'database');
    if ($conn->connect_error) {
        die("数据库连接失败:" . $conn->connect_error);
    }
    
    $sql = "INSERT INTO users (name, email) VALUES ('$name', '$email')";
    if ($conn->query($sql) === TRUE) {
        echo "数据保存成功";
    } else {
        echo "数据保存失败:" . $conn->error;
    }
    $conn->close();
}

2. Export data to CSV format
In order to facilitate the export of data, the commonly used format is CSV (comma separated values). In PHP, we can use the fputcsv() function to write data to a CSV file.

// 查询数据
$conn = new mysqli('localhost', 'username', 'password', 'database');
if ($conn->connect_error) {
    die("数据库连接失败:" . $conn->connect_error);
}
$sql = "SELECT * FROM users";
$result = $conn->query($sql);

// 创建CSV文件
$filename = "users.csv";
$file = fopen($filename, "w");

// 写入表头
fputcsv($file, array('姓名', '邮箱'));

// 写入数据
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        fputcsv($file, array($row['name'], $row['email']));
    }
}

fclose($file);
$conn->close();

echo "数据导出成功,文件名为" . $filename;

3. Import CSV files and store data
For exported CSV files, we can read the data in it through the fgetcsv() function and store it.

// 读取CSV文件
$filename = "users.csv";
$file = fopen($filename, "r");

// 创建数据库连接
$conn = new mysqli('localhost', 'username', 'password', 'database');
if ($conn->connect_error) {
    die("数据库连接失败:" . $conn->connect_error);
}

// 清空表数据
$conn->query("DELETE FROM users");

// 逐行读取CSV文件,并存储数据
while (($data = fgetcsv($file)) !== FALSE) {
    $name = $data[0];
    $email = $data[1];
    
    // 数据库存储操作
    $sql = "INSERT INTO users (name, email) VALUES ('$name', '$email')";
    if ($conn->query($sql) === TRUE) {
        echo "数据导入成功";
    } else {
        echo "数据导入失败:" . $conn->error;
    }
}

fclose($file);
$conn->close();

Summary:
Through the above steps, we can use PHP to process the data in the form and implement the data export and import functions. In actual development, according to project needs, we can verify, clean, store and other operations on form data, and flexibly apply various data formats for export and import.

The above is the detailed content of How to handle data export and import in forms using PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more