How can the "Duplicate entry" error be resolved when copying data between tables in PHP?
When copying data between tables in PHP, the "Duplicate entry" error can occur if there are conflicting primary key values being inserted. To resolve this issue, you can use the INSERT IGNORE statement in your SQL query to ignore duplicate entries and continue inserting the rest of the data.
<?php
$sourceTable = "source_table";
$destinationTable = "destination_table";
// Connect to the database
$mysqli = new mysqli("localhost", "username", "password", "database");
// Copy data from source table to destination table
$query = "INSERT IGNORE INTO $destinationTable SELECT * FROM $sourceTable";
$result = $mysqli->query($query);
if ($result) {
echo "Data copied successfully!";
} else {
echo "Error copying data: " . $mysqli->error;
}
// Close the connection
$mysqli->close();
?>