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.
Any help would be gratefully appreciated as I am banging my head against the table lol
Cheers.
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.