How can the code provided be improved to correctly read and insert CSV data into the database table?

The issue with the provided code is that it is not properly reading and inserting CSV data into the database table. To solve this issue, we need to use PHP's built-in functions like `fgetcsv()` to correctly read the CSV file and then insert each row into the database table using prepared statements to prevent SQL injection.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

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

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

// Open the CSV file
$file = fopen('data.csv', 'r');

// Read and insert data into the database
while (($data = fgetcsv($file)) !== FALSE) {
    $stmt = $conn->prepare("INSERT INTO table_name (column1, column2, column3) VALUES (?, ?, ?)");
    $stmt->bind_param("sss", $data[0], $data[1], $data[2]);
    $stmt->execute();
}

// Close the file and connection
fclose($file);
$conn->close();
?>