I am trying to execute a stored procedure from my php script but I am running into some syntax problems.
I have an ODBC connection and I do not know the syntax used when doing a prepared statement for this.
I have found an example which uses sqlserv_param
shown below but it does not fire.
The array being passed into php has this format:
SqlArr[empid,Wkend,Day,Title,Description,Value,Timestamp,AbbrevJob]
My DbConnectPSI has this following line of code to show you what kind of connection I am using.
$connect = odbc_connect("DRIVER={ODBC Driver 11 for SQL Server};
Server=$serverName;Database=$myDb", $userName, $myPass);
// Connection String in to SQL
Stored Procedure:
Alter PROCEDURE InsertfromPHP
-- Add the parameters for the stored procedure here
@paramEmpid int,
@paramWkEnd Date,
@paramDay Date,
@paramTitle varchar(20),
@paramDescription varchar(20),
@paramValue float,
@paramAbbrevJob int
AS
BEGIN
SET NOCOUNT ON;
-- Insert statements for procedure here
If Exists (Select * from EmployeeTimeSheetsTemp where empid=@paramEmpid and Day=@paramDay and Title=@paramTitle and Description=@paramDescription)
Update EmployeeTimesheetsTemp set Value=@paramValue where empid=@paramEmpid and Day=@paramDay and Title=@paramTitle and Description=@paramDescription
Else
Insert into EmployeeTimeSheetsTemp (Empid,WkEnd,Day,Title,Description,Value,Timestamp,AbbrevJob)
Values(@paramEmpid,@paramWkEnd,@paramDay,@paramTitle,@paramDescription,@paramValue,getdate(),@paramAbbrevJob)
END
GO
PHP SCRIPT:
<?php
include_once 'DbConnectPSI.php';
global $connect;
global $record3;
global $emptyQ;
global $rightOn;
global $SqlArr;
global $i;
$rightOn="Thank you, your time has been inserted successfully";
$SqlArr = $_POST['SqlArr'];
$tsql_callSP = "execute InsertfromPHP( ?,?,?,?,?,?,?)";
$stmt3 = odbc_prepare( $connect, $tsql_callSP);
if($stmt3===false){throw new error exception(odbc_errormsg())}
for ($i=0;$i<sizeof($SqlArr);$i++) {
$empId = $SqlArr[$i][0];
$WkEnd = $SqlArr[$i][1] ;
$Day = $SqlArr[$i][2] ;
$Title = $SqlArr[$i][3] ;
$Description = $SqlArr[$i][4] ;
$Value = $SqlArr[$i][5] ;
$AbbrevJob = $SqlArr[$i][8] ;
/*$params = array(
array($employeeId, SQLSRV_PARAM_IN),
array($WkEnd, SQLSRV_PARAM_IN),
array($Day, SQLSRV_PARAM_IN),
array($Title, SQLSRV_PARAM_IN),
array($Description, SQLSRV_PARAM_IN),
array($Value, SQLSRV_PARAM_IN),
array($AbbrevJob, SQLSRV_PARAM_IN)
);*/
/* Execute the query. */
$lego = odbc_execute($stmt3,array($Empid,$WkEnd,$Day,$Title,$Description,$Value,$AbbrevJob);
if($lego===false){throw new error exception(odbc_errormsg())}
}
odbc_close($connect);
?>
What syntax errors need to be fixed for this to work properly?
Can I do this in php since it works in sql server?
exec insertfromPHP @paramEmpId=89,@paramWkend='2015-10-24',@paramDay='2015-10-21',@paramTitle='1',@paramDescription='1',@paramValue=15,@paramAbbrevJob=3
but replace the concrete values with the sqlarr objects?
Error received: Error occuring at the moment