Php-sqlsrv webhook从数据数组中插入数据

时间:2017-09-12 13:37:58

标签: php sql-server sqlsrv

我有一个php脚本,我想用来从远程服务器获取数据。远程服务器正在发出通知,我已设置我的脚本以从中接收数据。我在sql server上没有太多经验。

我可以连接到服务器,但我似乎无法成功地将数据插入到表中。

到目前为止,这是我的代码。代码显示了我如何尝试从服务器插入数据。我还有一个更新表的步骤,但不包括在内。无论如何这里是代码

 <?php

    $serverName = "asp.net"; 
    $connectionInfo = array( "Database"=>"DB_A2A8ED_Main", "UID"=>"DB_A2A8ED_Main_admin", "PWD"=>"123456");
    $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));
}

$service_name = $_POST['service_name'];
$business_number = $_POST['business_number'];
$transaction_reference = $_POST['transaction_reference'];
$internal_transaction_id = $_POST['internal_transaction_id'];
$transaction_timestamp = $_POST['transaction_timestamp'];
$transaction_type = $_POST['transaction_type'];
$account_number = $_POST['account_number'];
$sender_phone = $_POST['sender_phone'];
$first_name = $_POST['first_name'];
$middle_name = $_POST['middle_name'];
$last_name = $_POST['last_name'];
$amount = $_POST['amount'];
$currency = $_POST['currency'];
$signature = $_POST['signature'];
$status = 'unseen';

$params = array($service_name,$business_number,$transaction_reference,$internal_transaction_id,$transaction_timestamp,$transaction_type,$account_number,$sender_phone,$first_name,$middle_name,$last_name,$amount,$currency,$signature,$status);

$sql = "INSERT INTO dbo.mpesa (service_name,business_number,transaction_reference,internal_transaction_id,transaction_timestamp,transaction_type,account_number,sender_phone,first_name,middle_name,last_name,amount,currency,signature,status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

$stmt = sqlsrv_query( $conn, $sql, $params);
if( $stmt === false ) {
     die( print_r( sqlsrv_errors(), true));
}
?>

我收到此错误

Connection established.
<br />Array
(
    [0] => Array
        (
            [0] => 23000
            [SQLSTATE] => 23000
            [1] => 515
            [code] => 515
            [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Cannot insert the value NULL into column 'service_name', table 'DB_A2A8ED_Main.dbo.mpesa'; column does not allow nulls. INSERT fails.
            [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Cannot insert the value NULL into column 'service_name', table 'DB_A2A8ED_Main.dbo.mpesa'; column does not allow nulls. INSERT fails.
        )

    [1] => Array
        (
            [0] => 01000
            [SQLSTATE] => 01000
            [1] => 3621
            [code] => 3621
            [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]The statement has been terminated.
            [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]The statement has been terminated.
        )

)

这是创建表的SQL

CREATE TABLE  mpesa (
  id int IDENTITY(1,1) PRIMARY KEY,
  service_name varchar(400) NOT NULL,
  business_number varchar(400) NOT NULL,
  transaction_reference varchar(400) NOT NULL,
  internal_transaction_id varchar(400) NOT NULL,
  transaction_timestamp varchar(400) NOT NULL,
  transaction_type varchar(400) NOT NULL,
  account_number varchar(400) NOT NULL,
  sender_phone varchar(400) NOT NULL,
  first_name varchar(400) NOT NULL,
  middle_name varchar(400) NOT NULL,
  last_name varchar(400) NOT NULL,
  amount varchar(400) NOT NULL,
  currency varchar(400) NOT NULL,
  signature varchar(400) NOT NULL,
  status varchar(400) NOT NULL,
  balance varchar(400) NOT NULL
);

0 个答案:

没有答案