How can the PHP script be modified to ensure data is inserted into the correct columns in the table?

To ensure data is inserted into the correct columns in the table, the PHP script needs to specify the column names in the SQL INSERT statement. This way, the data will be mapped to the corresponding columns in the table.

<?php
// Assuming $conn is the database connection object

// Define variables from form input
$name = $_POST['name'];
$email = $_POST['email'];
$age = $_POST['age'];

// SQL query with column names specified
$sql = "INSERT INTO users (name, email, age) VALUES ('$name', '$email', '$age')";

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

// Close the database connection
$conn->close();
?>