How can the PHP script be modified to display an error message if the insertion is not successful?

To display an error message if the insertion is not successful, we can use the mysqli_error() function to check for any errors that occur during the insertion process. If an error is detected, we can output a custom error message to the user.

// Attempt to insert data into the database
$insert_query = "INSERT INTO table_name (column1, column2) VALUES ('$value1', '$value2')";
$result = mysqli_query($connection, $insert_query);

// Check if the insertion was successful
if ($result) {
    echo "Data inserted successfully!";
} else {
    echo "Error: " . mysqli_error($connection);
}