使用sqlsrv插入查询

时间:2017-11-25 14:30:58

标签: php sql-server

当我尝试在我的数据库中插入一个值时,它返回给我没有错误,但它没有插入任何内容,在sql server上它运行正常。

<?php
$serverName = "12.15.88.81";
$connectionInfo = array( "Database"=>"Dcs", "UID"=>"sa", "PWD"=>"123456");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
    die( print_r( sqlsrv_errors(), true ));

}
if ( sqlsrv_begin_transaction( $conn ) === false ) {
     die( print_r( sqlsrv_errors(), true ));
}
$sql = "INSERT INTO [dbo].[Dcs_name] (ID,NAME) VALUES (?,?)";
$params = array('11','Ani');
$stmt = sqlsrv_query( $conn, $sql, $params);



$stmt = sqlsrv_query( $conn, $sql, $params);

if( $stmt === false ) {
    if( ($errors = sqlsrv_errors() ) != null) {
        foreach( $errors as $error ) {
            echo "SQLSTATE: ".$error[ 'SQLSTATE']."<br />";
            echo "code: ".$error[ 'code']."<br />";
            echo "message: ".$error[ 'message']."<br />";
        }
    }
}

?>

1 个答案:

答案 0 :(得分:0)

你是对的我添加了提交,它非常有用,非常感谢你。

if( $stmt ) {
     sqlsrv_commit( $conn );
     echo "Transaction committed.<br />";
} else {
     sqlsrv_rollback( $conn );
     echo "Transaction rolled back.<br />";
}