What is the correct syntax for inserting data into a MySQL table using PDO in PHP?
When inserting data into a MySQL table using PDO in PHP, you need to prepare a SQL statement with placeholders for the values to be inserted. Then, bind the values to the placeholders using the bindParam() or bindValue() method. Finally, execute the prepared statement to insert the data into the table.
// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
// Prepare the SQL statement with placeholders
$stmt = $pdo->prepare("INSERT INTO tablename (column1, column2) VALUES (:value1, :value2)");
// Bind the values to the placeholders
$stmt->bindParam(':value1', $value1);
$stmt->bindParam(':value2', $value2);
// Set the values to be inserted
$value1 = 'example1';
$value2 = 'example2';
// Execute the prepared statement
$stmt->execute();