-1
I have two databases and I have a table Table in the database Servor1 no data and another database Server2 with a table Tabelay. And I want to make one select
on the table Tabelay and with your data I make a insert
on the table Table which is in another database. I’ve done some code, but it’s not working properly. And this error appears:
Error: INSERT INTO Servidor1.TabelaX (ID, Data) SELECT ID, Data FROM
Servidor2.TabelaY; VALUES (4, 918745125);
Duplicate entry '2' for key 'PRIMARY'
<html>
<body>
<h2>Procurar Dados</h2>
<?php
echo "<h3> Clientes </h3>";
// Criar conexao
$conn= mysqli_connect('localhost','root',null,'Servidor2') or die (mysqli_connect_error());
//Verificar conexao
if (!$conn) {
die("Falha de conexao: ". mysqli_connect_error());
}
$ID = $_POST['ID'];
$sql = "SELECT * FROM TabelaY WHERE ID = $ID";
$result = mysqli_query($conn, $sql);
mysqli_select_db($conn,"Servidor1");
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$row1 = $row["ID"];
$row2 = $row["Data"];
}
} else {
echo "0 results";
}
$sql = "INSERT INTO Servidor1.TabelaX (ID, Data)
SELECT ID, Data
FROM Servidor2.TabelaY;
VALUES ($row1, $row2);";
if (mysqli_multi_query($conn, $sql)) {
echo "Dados Inseridos";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
<p> </p>
<a href="http://localhost/BaseDados/Index.html">
Voltar à entrada </a>
</body>
</html>
This error appears: Error: INSERT INTO Servor1.Tabelax (ID, Data) SELECT ID, Data FROM Servor2.Tabelay; Duplicate entry '2' for key 'PRIMARY'
– Nelson Silva