I'm working on someone's else database creating a simple PHP solution, running Apache (XAMPP+Windows 2003 Server) that connects to a MS SQL SERVER database running Windows 2008 R2.
I've been working a few days, I've created a few tables, I've inserted data on those tables, but now I'm trying to insert data into an existing table using PHP, but for this table in particular the insert statement doesn't work at all, but there is no error message.
Here is the code.
<?php
include '../../includes/mssql.php';
$insereItem = "INSERT INTO TBL_NOTAS_FATURAMENTO_ITENS (CD_LANCAMENTO ......... ) VALUES (26452,.....0)";
$stmt = sqlsrv_query( $conn, $insereItem);
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
echo $insereItem . "
";
?>
There is no error message.
As you can see I've removed data from the insert statement so it doesn't use too much space here (there are 88 values to be inserted). There is no error message, there is no error in Apache logs, there is no error in php logs but the data is not commited.
As you can see I did a "echo" to the statement, so if I copy and paste the statement in SQL Server Management Studio the insert works as it should, but if I try commiting with PHP it won't work.
The table I'm trying to insert data has an insert trigger and I'm pretty sure that this is the problem because if I disable the trigger then the insert works as it should (so there is no error in building up the insert command).
I've already tried to use other sqlsrv_ commands, using the begin connection, prepare, execute and then commit, and the result is the same. The sqlsrv_execute returns true, sqlsrv_prepare returns
PS: the real code isn't the one above, since the real code uses an array to parse parameters, but the final insert statement is the same, and the problem happens either way.
Here's the code to the insert trigger: (as I said before, the database was not built by me)
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER TRIGGER [TRG_TBL_NOTAS_FATURAMENTO_ITENS_REGINSERIR] ON [dbo].[TBL_NOTAS_FATURAMENTO_ITENS]
FOR INSERT
AS
IF EXISTS (SELECT 'TRUE' FROM INSERTED)
BEGIN
--SET NOCOUNT ON
--IF @@ROWCOUNT=0 RETURN
DECLARE @CD_LANCAMENTO AS INT
DECLARE @CD_FILIAL AS INT
DECLARE @CD_ITEM AS INT
DECLARE @CD_CME AS INT
DECLARE @CD_MATERIAL AS INT
DECLARE @CD_EMPRESA AS INT
DECLARE @CD_ID AS INT
DECLARE @DT_EMISSAO AS DATETIME
SELECT @CD_LANCAMENTO = (SELECT CD_LANCAMENTO FROM INSERTED)
SELECT @CD_EMPRESA = (SELECT CD_EMPRESA FROM DBO.TBL_NOTAS_FATURAMENTO WHERE (CD_LANCAMENTO = @CD_LANCAMENTO))
SELECT @CD_FILIAL = (SELECT CD_FILIAL FROM DBO.TBL_NOTAS_FATURAMENTO WHERE (CD_LANCAMENTO = @CD_LANCAMENTO))
SELECT @CD_ITEM = (SELECT CD_ITEM FROM INSERTED)
SELECT @CD_CME = (SELECT CD_CME FROM INSERTED)
SELECT @CD_MATERIAL = (SELECT CD_MATERIAL FROM INSERTED)
SELECT @CD_ID = (SELECT CD_ID FROM INSERTED)
SELECT @DT_EMISSAO = (SELECT DT_EMISSAO FROM DBO.TBL_NOTAS_FATURAMENTO WHERE (CD_LANCAMENTO = @CD_LANCAMENTO))
--CALCULA IMPOSTOS
EXEC SP_CALCULA_IMPOSTOS_NOTAS_FATURAMENTO @CD_LANCAMENTO, @CD_ITEM, @CD_CME, @CD_MATERIAL
--TOTAL DO LANCAMENTO
EXEC SP_TOTAL_NOTAS_FATURAMENTO @CD_LANCAMENTO
--ATUALIZA VOLUMES
EXEC SP_CALCULA_VOLUMES_NOTAS_FATURAMENTO @CD_LANCAMENTO, @CD_EMPRESA
--ATUALIZA KARDEX
EXEC SP_CALCULA_ESTOQUE 41, @CD_LANCAMENTO, @CD_ID, @DT_EMISSAO, @CD_FILIAL, @CD_MATERIAL
END