We help IT Professionals succeed at work.

Row insertion failed. Array

Joe Danyi
Joe Danyi asked
on
559 Views
Last Modified: 2017-03-11
I am trying to insert items from a form into MSSQL via PHP but I am having some trouble, please see my code bellow and see if you can point me in the right direction as to my trouble. And I thank you in advance.
<?php  
$serverName = "Servername"; 
$uid = "423";   
$pwd = "Password";  
$databaseName = "423"; 

$connectionInfo = array( "UID"=>$uid,                            
                         "PWD"=>$pwd,                            
                         "Database"=>$databaseName); 
  
/* Connect using SQL Server Authentication. */  
$conn = sqlsrv_connect( $serverName, $connectionInfo);  
  
// TRY TO CONNECT
$conn = sqlsrv_connect( $serverName, $connectionInfo);  
if( $conn === false )  
{  
     echo "Could not connect.\n";  
     print_r( sqlsrv_errors() );  
}  

// DEFINE OUR VARIABLES
$today = date('c');
$phpgur = $_POST["gur"];
$phpbur = $_POST["bur"];

/* Set parameter values. */
$params = array($phpbur,$phpgur,$today); // 

/* Set up the parameterized query. */  
$tsql = "INSERT INTO 423.tss (bur,gur,un) VALUES (?,?,?,)";  

/* Prepare and execute the query. */  
$stmt = sqlsrv_query($conn, $tsql, $params);  
if( $stmt )  
{  
     echo "Row successfully inserted.\n";  
}  
else  
{  
     echo "Row insertion failed.\n";  
     print_r( sqlsrv_errors() );  
}  


echo "connectionInfo";
var_dump($connectionInfo);

echo "conn";
var_dump($conn);

echo "params";
var_dump($params);

echo "today is $today";

echo "stmt";
var_dump($stmt);

echo "tsql is $tsql";

/* Free statement and connection resources. */ 
sqlsrv_free_stmt($stmt);  

Open in new window



I revive the following error:
Row insertion failed. Array ( [0] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 102 [code] => 102 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near '423.'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near '423.'. ) ) connectionInfoarray(3) { ["UID"]=> string(3) "423" ["PWD"]=> string(60) "2daf7bedb637d0c77748462f3a073e0a8d895c2a31a5ac3452ad6675fa1a" ["Database"]=> string(3) "423" } connresource(5) of type (SQL Server Connection) paramsarray(3) { [0]=> string(3) "bur" [1]=> string(3) "gur" [2]=> string(25) "2017-03-07T20:09:54-05:00" } today is 2017-03-07T20:09:54-05:00stmtbool(false) tsql is INSERT INTO 423.tss (bur,gur,un) VALUES (test10,test10,12/12/12)
Warning: sqlsrv_free_stmt() expects parameter 1 to be resource, boolean given in G:\s\wunderlist-api-demo-master\brace\connect4.php on line 63

Open in new window


I am using server 2012 R2,
SQL server 2008 ( I believe)
PHP 5.6 on IIS
Thank you in advance let me know if you need more info.
Comment
Watch Question

Most Valuable Expert 2011
Author of the Year 2014

Commented:
Just a "swag" guess, but try omitting the last comma.

Not this:
$tsql = "INSERT INTO 423.tss (bur,gur,un) VALUES (?,?,?,)";  

Instead this:
$tsql = "INSERT INTO 423.tss (bur,gur,un) VALUES (?,?,?)";
Joe DanyiOwner

Author

Commented:
Alas im afraid not :(
 
i did make that change to the script tho.

Any further input appreciated
Joe DanyiOwner

Author

Commented:
http://stackoverflow.com/questions/24512094/connecting-php-to-ms-sql-server
it could be because of use of "thing" instead of using 'thing' maybe?
 "to avoid escapingg you could also use single quotes ' instead of double quotes ". Then PHP does not resolve variables within the string. (see this question)"
Most Valuable Expert 2011
Author of the Year 2014
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions