What are some common mistakes to avoid when inserting data into a database using PHP, especially when dealing with auto-increment fields?

When inserting data into a database using PHP, especially when dealing with auto-increment fields, it's important to avoid explicitly setting the auto-increment field value. Let the database handle the auto-incrementing of the field to prevent conflicts and ensure data integrity. Additionally, remember to sanitize user input to prevent SQL injection attacks.

<?php
// Connect 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 database without setting the auto-increment field
$sql = "INSERT INTO table_name (column1, column2) VALUES ('value1', 'value2')";
if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

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