How can one selectively insert data into specific database columns in PHP?

To selectively insert data into specific database columns in PHP, you can use an SQL query with placeholders for the values you want to insert. By specifying the columns you want to insert data into, you can control which values are inserted into which columns.

<?php
// Establish a connection to the database
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Prepare an SQL query with placeholders for the values to be inserted
$stmt = $pdo->prepare("INSERT INTO mytable (column1, column2, column3) VALUES (:value1, :value2, :value3)");

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

// Set the values to be inserted into specific columns
$value1 = 'value1';
$value2 = 'value2';
$value3 = 'value3';

// Execute the query
$stmt->execute();
?>