I am trying to insert values in mssql db via php. I am getting an error Cannot insert explicit value for identity column in when IDENTITY_INSERT is set to OFF.
I have added query line to set IDENTITY_INSERT but still get same error.
Code I am using to insert values in mssql db.
$serverName = "xxxx\SQLEXPRESS"; //serverName\instanceName
$connectionInfo = array( "Database"=>"xxxx", "UID"=>"xxx", "PWD"=>"xxxx" , "CharacterSet" => "UTF-8");
$con = sqlsrv_connect( $serverName, $connectionInfo);
if($con) {
echo "Connection established";
}
else{
echo "Connection could not be established.";
die( print_r( sqlsrv_errors(), true));
}
$query = "SET IDENTITY_INSERT dbo.xxx ON ";
$query = "INSERT INTO dbo.xxx([Id],[IdFirma], [VrstaDokumenta], [BrojDokumenta], [BrojDokumentaKroz],
[DatumDokumenta], [IdKupac], [VrstaCijene], [IdKorisnik], [NacinPlacanja], [DatumZadnjeAkcije], [Status], [StatusArhive],
[StatusIzmjene],
[StatusStampe], [VrstaFakture])
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
$params = array($id,$IdFirma,$VrstaDokumenta,$BrojDokumenta, $BrojDokumentaKroz, $DatumDokumenta, $IdKupac, $VrstaCijene, $IdKorisnik,
$NacinPlacanja, $DatumZadnjeAkcije, $Status, $StatusArhive, $StatusIzmjene, $StatusStampe, $VrstaFakture);
/* Prepare and execute the statement. */
$stmt = sqlsrv_query( $con, $query, $params);
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
Error I get:
Array ( [0] => Array ( [0] => 23000 [SQLSTATE] => 23000 [1] => 544 [code] => 544 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot insert explicit value for identity column in table 'FKNarudzbeKupacaZaglavlje' when IDENTITY_INSERT is set to OFF. [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot insert explicit value for identity column in table 'FKNarudzbeKupacaZaglavlje' when IDENTITY_INSERT is set to OFF. ) )
Could someone tell me how to solve this issue or give advice?