Why is the script not inserting data into the table?

The script may not be inserting data into the table due to errors in the SQL query, connection to the database, or missing input data. To solve this issue, ensure that the SQL query is correct, the database connection is established successfully, and all necessary input data is provided before executing the query.

<?php
// Establish a connection to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

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

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

// Insert data into the table
$data = "example data";
$sql = "INSERT INTO table_name (column_name) VALUES ('$data')";

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

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