Duplicate ID issue when trying to copy table row

Joined
Jul 30, 2022
Messages
1
Reaction score
0
Hello all,

I am trying to create a script to allow me to copy a table row from one database to another but I am having an issue creating new ID's when a duplicate is found. It just tells me that the ID has already been copied if found in the second database. This is also a safety feature to avoid data been over written.

URL is page.php?id=12

The column for ID is set to AI.

Code:
$mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName);
 
$id = $_GET['id'];

$sql="select * from db2.contacts where ($id = intval($id));";//  check id is already copied

$res=mysqli_query($mysqli,$sql);

if (mysqli_num_rows($res) > 0) {
$row = mysqli_fetch_assoc($res);
if($id==$row['id'])
{
echo "Already copied"; //error message if already copied

}

} else{

$query=mysqli_query($mysqli,"INSERT INTO db2.contacts SELECT * FROM  db1.contacts WHERE id =$id");

echo "Successfully copied";
}

Any help would be gratefully appreciated as I am banging my head against the table lol

Cheers.
 
Joined
Mar 11, 2022
Messages
227
Reaction score
32
Is id also the auto increment field? If yes, than there's your problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top