What are the potential pitfalls of using the ON DUPLICATE KEY UPDATE clause in MySQL when working with PHP?

When using the ON DUPLICATE KEY UPDATE clause in MySQL with PHP, a potential pitfall is not properly sanitizing user input, which can lead to SQL injection attacks. To prevent this, always use prepared statements with parameterized queries to securely handle user input.

// Connect to the database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Prepare a statement with parameterized query
$stmt = $mysqli->prepare("INSERT INTO table_name (column1, column2) VALUES (?, ?) ON DUPLICATE KEY UPDATE column2 = VALUES(column2)");

// Bind parameters
$stmt->bind_param("ss", $value1, $value2);

// Set parameter values
$value1 = "input_value1";
$value2 = "input_value2";

// Execute the statement
$stmt->execute();

// Close the statement and connection
$stmt->close();
$mysqli->close();