简体   繁体   English

PHP sqlsrv_query:插入数据时出错

[英]PHP sqlsrv_query : Error while inserting data

I am trying to insert data into my database table in SQL Server. 我正在尝试将数据插入SQL Server中的数据库表中。 I am able to connect to the database and while inserting I get the following error: 我能够连接到数据库,并且在插入时出现以下错误:

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'. ) )

To keep things simple, I have hardcoded the values (instead of picking on form submit) that need to be inserted into the table: 为了简单起见,我对需要插入表中的值(而不是从表单提交中进行选择)进行了硬编码:

/*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>';
}

The fields that are called above are not all the fields of the table. 上面调用的字段并非表的所有字段。 There are a few more but since they allow null values, I did not include them. 还有很多,但是由于它们允许null值,因此我没有包括它们。 I have googled for this error but unable to understand what it actually means and where is the issue! 我已经用谷歌搜索了这个错误,但无法理解它的实际含义以及问题所在! Any pointers? 有指针吗?

EDIT 1: Successfully connects through: 编辑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));
}

I think your Query as wrong. 我认为您的查询是错误的。 Please check the query with right syntax. 请使用正确的语法检查查询。

$insertSql = "INSERT INTO tablename (ID,Name, Company, Email, Phone, Assets, Comm, Capability) VALUES (val1,val2,val3,val4.....)"; $ insertSql =“将表名称(ID,名称,公司,电子邮件,电话,资产,通信,功能)插入到值(val1,val2,val3,val4 .....)”;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM