What are the advantages and disadvantages of storing user input data in a PHP session before inserting it into a MySQL database?

Storing user input data in a PHP session before inserting it into a MySQL database can help prevent data loss in case of errors during the insertion process. However, this approach may also introduce security risks if the session data is not properly sanitized and validated before insertion.

<?php
session_start();

// Store user input data in session
$_SESSION['user_input'] = $_POST['user_input'];

// Validate and sanitize the user input data before inserting into the database
$user_input = filter_var($_SESSION['user_input'], FILTER_SANITIZE_STRING);

// Insert the sanitized user input data into the database
// $query = "INSERT INTO table_name (column_name) VALUES ('$user_input')";
// Perform the database query here

// Unset the session data after insertion
unset($_SESSION['user_input']);
?>