What are some common pitfalls to watch out for when storing user input in a MySQL database using PHP?
One common pitfall to watch out for when storing user input in a MySQL database using PHP is SQL injection attacks. To prevent this, you should always use prepared statements with parameterized queries to sanitize and validate user input before inserting it into the database.
// Example of using prepared statements to store user input in a MySQL database
// Assume $conn is the database connection object
// User input
$userInput = $_POST['user_input'];
// Prepare a SQL statement with a parameter
$stmt = $conn->prepare("INSERT INTO table_name (column_name) VALUES (?)");
$stmt->bind_param("s", $userInput);
// Execute the statement
$stmt->execute();
// Close the statement and connection
$stmt->close();
$conn->close();