What is the purpose of the SELECT statement within an INSERT INTO statement in PHP?

The purpose of the SELECT statement within an INSERT INTO statement in PHP is to insert data into a table by selecting data from another table. This can be useful when you want to copy specific data from one table to another or when you need to combine data from multiple tables into one.

<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Insert data into table2 by selecting data from table1
$sql = "INSERT INTO table2 (column1, column2, column3)
        SELECT column1, column2, column3
        FROM table1
        WHERE condition";

if ($conn->query($sql) === TRUE) {
    echo "Data inserted successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

// Close connection
$conn->close();
?>