What are the security implications of saving form data directly to a CSV file after each input?

Saving form data directly to a CSV file after each input can pose security risks such as exposing sensitive information if the file is not properly secured. To mitigate this risk, it is recommended to store the data in a secure database instead of a plain text file. This way, access control and encryption can be implemented to protect the data.

// Connect to a MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Process form data
$name = $_POST['name'];
$email = $_POST['email'];

// Prepare and execute SQL statement to insert data into a secure table
$sql = "INSERT INTO users (name, email) VALUES ('$name', '$email')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();