PHP sqlsrv_query:插入数据时出错

时间:2014-02-27 10:11:03

标签: php sql sql-insert sqlsrv

我正在尝试将数据插入SQL Server中的数据库表。我能够连接到数据库,并在插入时收到以下错误:

Errors: Array ( [0] => Array ( [0] => 42S02 [SQLSTATE] => 42S02 [1] => 208 [code] => 208 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid object name 'dbo.Salesforce'. [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid object name 'dbo.Salesforce'. ) )

为了简单起见,我已经硬编码了需要插入表中的值(而不是选择表单提交):

/*Insert data.*/
$insertSql = "INSERT INTO dbo.Salesforce (ID,Name, Company, Email, Phone, Assets, Comm, Capability)
VALUES (?,?,?,?,?,?,?,?)";
/* Construct the parameter array. */
$ID = 5;
$Name = "Sarah";
$Company = "Track24";
$Email = "xxx@live.com";
$Phone = "+xxxxxx";
$Assets = "Vehicles";
$Comm = "GSM";
$Capability = "IVMS";
$params1 = array($ID,
               $Name,
               $Company,
               $Email,
               $Phone,
               $Assets,
               $Comm,
               $Capability);
$stmt = sqlsrv_query($conn, $insertSql, $params1);
if($stmt === false)
{
die('Errors: ' . print_r(sqlsrv_errors(), TRUE));
}
else
{
echo '<br clear="all"><label style="color:red;">Thank you for your details.</label>';
}

上面调用的字段不是表格的所有字段。还有一些,但由于它们允许null值,我没有包含它们。我已经google了这个错误,但无法理解它的实际含义以及问题在哪里!有什么指针吗?

编辑1:成功连接:

/* Specify the server and connection string attributes. */
$serverName = "xxxx-PC\SQLExpress";
$connectionOptions = array("Database"=>"Salesforce");
$conn = sqlsrv_connect($serverName, $connectionOptions);
if($conn === false)
{
      die(print_r(sqlsrv_errors(), true));
}

1 个答案:

答案 0 :(得分:0)

我认为您的查询错误。请使用正确的语法检查查询。

$ insertSql =“INSERT INTO tablename(ID,Name,Company,Email,Phone,Assets,Comm,Capability) VALUES(val1,val2,val3,val4 .....)“;