将多行插入 MySQL Table(PHP 问题)

Insert multiple rows into MySQL Table ( PHP problem )

如何使用 php 将多行插入 MySQL 以便在一次查询后 MySQL Table 看起来像:

user_id Message Date Subject
1243 Hello world! 06.04.2021 First Message
 5265 Hi John!  07.04.2021 Second Message

你可以使用MYSQL准备好的语句

    include(your connection code...)

    // prepare and bind
    $stmt = $conn->prepare("INSERT INTO yourtable(user_id, message, date, subject) VALUES (?, ?, ?, ?)");
    $stmt->bind_param("isss", $user_id, $message, $date, $subject);

    @foreach statement for your data
    
    // set parameters and execute
    $user_id= $data->id;
    $message= $data->message;
    $date= $data->date;
    $subject= $data->subject;
    $stmt->execute();
    
    @endforeach

此方法甚至可以保护您的应用免受 SQL 注入