David David - 1 month ago 9
PHP Question

Error with insert into sql server database with php

I have error with insert multi column data into sqlserver database with php

php Code:

<?php
$serverName = "GHAREBAGHI\sqlexpress, 1433";
$connectionInfo = array( "Database"=>"PwKara", "UID"=>"test","PWD"=>"100100");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
$tsql= "INSERT INTO Requests
(EMP_NO ,SubmittedDate ,Type ,StartDate ,EndDate ,StartHour ,EndHour
,Duration ,SubmittedByEmployeeID ,OperationsID ,Description ,CurEmp_NO ,AcceptCode ,IsWardenCheck ,RequestStatus ,IOStatus ,CurSection)
VALUES
(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
$para = array("93229 ,'2016-10-19 09:23:48.850' ,54 ,'2016-10-19 09:23:48.850' ,'2016-10-19 09:23:48.850' ,2359 ,2359 ,2 ,83229 ,3 ,'php test' ,8813 ,0 ,0 ,0 ,0 ,6");

if (!sqlsrv_query($conn, $tsql, $para))
{
die('Error: ' . sqlsrv_errors());
}
echo "1 record added";
?>


Resault:

php error cod

The line that the error was pointed out to him:

die('Error: ' . sqlsrv_errors());

Answer

$para = array why are those in " .... " ? This is not array, this is string. Delete " on both sides. It will be like:

$para = array(93229, '2016-10-19 09:23:48.850', 54, '2016-10-19 09:23:48.850', '2016-10-19 09:23:48.850', 2359, 2359, 2, 83229, 3, 'php test', 8813, 0, 0, 0, 0, 6);