What are some common pitfalls when using PHP to insert values into a database?

One common pitfall when using PHP to insert values into a database is not properly sanitizing user input, which can lead to SQL injection attacks. To prevent this, always use prepared statements with parameterized queries to bind user input securely.

// Connect to the database
$pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");

// Prepare a SQL statement with placeholders for user input
$stmt = $pdo->prepare("INSERT INTO mytable (column1, column2) VALUES (:value1, :value2)");

// Bind the user input to the placeholders
$stmt->bindParam(':value1', $value1);
$stmt->bindParam(':value2', $value2);

// Set the values of the variables
$value1 = $_POST['input1'];
$value2 = $_POST['input2'];

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