I have been trying to create a script to copy over data from the contacts table on dababase 1 to database 2.
I have finally come up with the below script that allows me to copy over the data.
The issue that I am facing is that if the ID already exists in database 2 which prevents avoid over writing data, it gives me a warning that the ID already exists.
This is where I am stuck, is there anyway to tell the script to create a new ID if the ID already exists please?
$mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName);
$id = $_GET['id'];
$sql="select * from exdigita_vadercic.contacts where (id='$id');";// check id is already copied
$res=mysqli_query($mysqli,$sql);
if (mysqli_num_rows($res) > 0) {
// output data of each row
$row = mysqli_fetch_assoc($res);
if($id==$row['id'])
{
echo "Already copied"; //error message if already copied
}
} else{
$query=mysqli_query($mysqli,"INSERT INTO exdigita_vadercic.contacts SELECT * FROM contacts WHERE id =$id");
echo "Successfully copied";
}