What are best practices for handling file uploads and database interactions in PHP scripts?

When handling file uploads in PHP scripts, it is important to validate the file type, size, and ensure proper error handling. When interacting with a database, it is crucial to use parameterized queries to prevent SQL injection attacks and sanitize user input to avoid potential security vulnerabilities.

// File upload handling
if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
    $file_name = $_FILES['file']['name'];
    $file_tmp = $_FILES['file']['tmp_name'];
    move_uploaded_file($file_tmp, 'uploads/' . $file_name);
} else {
    echo "Error uploading file.";
}

// Database interaction
$conn = new mysqli('localhost', 'username', 'password', 'database');
$stmt = $conn->prepare("INSERT INTO table_name (column_name) VALUES (?)");
$stmt->bind_param("s", $value);
$value = $_POST['input'];
$stmt->execute();
$stmt->close();
$conn->close();