Good afternoon.
How do I return an error when there is an attempt to insert a row in a table, and the primary key already exists? By default, the database does not insert the row. How to show this error as "Existing item"?
Follow part of the code:
$mysqli = new mysqli('localhost', 'root', '', 'datalin');
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$inclui="INSERT INTO DTC (DTC_CDRDES,DTC_CGC,DTC_TIPDOC,DTC_NUMNFC,DTC_QTDVOL,DTC_DATREC,DTC_PROCES,DTC_FORNEC) VALUES ('$CDRDES','$CNPJ','$TipDoc','$NumNFC','$QTDVol','$DataRec','$proces','$FORNEC')";
mysqli_query($mysqli,$inclui);
if($inclui):
echo "<script>
alert('Usuario Incluido com sucusso. :D');
window.location='index.php';
</script>";
else:
echo "<script>
alert('Infelizmente não foi possível excluir. :C');
window.location='index.php';
</script>";
endif;//echo "Cadastro realizado com sucesso!<br>Agradecemos a atenção.";
mysqli_close($mysqli);
Thanks in advance for the help.