Checking for duplicates

Please will someone help me with the following code.

The code copies data from one table to another on a button click. When first clicked the script selects and inserts the data ok but I need to check for duplicates and report how many duplicates there are.

I am struugling with identifying the duplicates and reporting back.

$options = [

//Connect to MySQL using PDO.
$pdo = new PDO("mysql:host=localhost;dbname=tbl_temp_products", "root", "", $options);


//The name of the new table.
$categories = 'categories';

//Import the data from the old table into the new table.
$select = $pdo->prepare('SELECT categories_id, parent_id FROM tbl_temp_categories WHERE categories_id = ?');
if ($select->rowCount() > 0) {
    echo 'Duplicates found';
} else {
$pdo->query("INSERT $categories SELECT categories_id, parent_id FROM tbl_temp_categories");

<form method ="post">
 <button class="btn btn-primary" type="submit">Update Categories</button>

<?php } ?>

Thank you for any help you can offer me

Why are you creating duplicate data?

I import data from an xml feed into a temporary table so I can work on it and then I need to update the live tables with the data.

Well, you should check for duplicates when you update the live tables and that way, you would not have duplicates in your live data. But, here is a tutorial on how to check for duplicates using queries. It explains how to check for one field, two fields and many fields depending on your database layout. Simple to understand. Hope this helps… Tutorial - find duplicate values

The OP has a thread from a year ago - Xml to mysql import script where someone asked what the OP wanted to do for a duplicate and listed methods for the different possibilities -

For the first two cases, if there is a duplicate index error, it means there was an existing row, if there was no error a new row was inserted. You can count or add items to a list as needed.

For the INSERT … ON DUPLICATE KEY UPDATE … method, in order to count or make a list of existing values, you would initially get the highest auto-increment id value. You would then get the last_insert_id() value from the query. If the last_insert_id() value is greater than the initial highest auto-increment id value, a new row was inserted, otherwise an existing row was updated.

For the IGNORE method, you would detect if a row was affected by the insert query. If yes, a new row was inserted, otherwise there was an existing row and nothing occurred when the query was executed.

Sponsor our Newsletter | Privacy Policy | Terms of Service