1
Good afternoon.
How to return an error when there is an attempt to insert a row into a table, and the primary key already exists? By default, the database does not insert the line. How to display this error as "Existing item"?
Follows 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 for your help.
Thanks for the reply. It worked by putting if($success) without the "!".
– Marvin
The
!
is a denial. Without this, you will enter theif
when the query runs smoothly. With the exclamation, you only enter when a problem occurs (which can be a primary key violation, you need to look at the error message to be sure).– bfavaretto