简体   繁体   English

sqlsrv_query无法按预期工作

[英]sqlsrv_query isn't working as expected

I'm in a vocational training right now and my task is to create a web-formula for accounting stuff. 我目前正在接受职业培训,我的任务是为会计工作创建一个网络公式。 The server is running IIS 6.2 on Windows Server 2012 R2, PHP 7.2.1 and I finally got the MS PHP SQL Extension running. 该服务器在Windows Server 2012 R2,PHP 7.2.1上运行IIS 6.2,最后我运行了MS PHP SQL Extension

Besides my code being a stackowerflow patchwork, it kinda works, only that I cant figure what I did wrong with the query. 除了我的代码是stackowerflow拼凑而成的代码之外,它还可以工作,只是无法弄清楚查询中的错误。 On submitting the formula it says Transaction rolled back, and I really don't know why. 在提交公式时,它说交易回滚了,我真的不知道为什么。 The query is, when pasted into MS SQL Server Management Studio, successfully executed, but not in the form. 当查询粘贴到MS SQL Server Management Studio中时,已成功执行,但不是以表格形式。

$serverName = "server\sqlexpress";
$connOptions = array( "Database"=>"123", "UID"=>"456", "PWD"=>"xxx");
$conn = sqlsrv_connect( $serverName, $connOptions );

if( $conn === false ) {
    die( print_r( sqlsrv_errors(), true ));
}
if ( sqlsrv_begin_transaction( $conn ) === false ) {
     die( print_r( sqlsrv_errors(), true ));
}

/* This is the query */
$sql1 = "INSERT INTO Person (Kundennummer, Vorname, Nachname, Strasse, Hausnummer, Etage, Email) VALUES (?, ?, ?, ?, ?, ?, ?)";
$params1 = array($kundennummer, $vorname, $nachname ,$strasse, $hausnummer, $etage, $email);
$query1 = sqlsrv_query( $conn, $sql1, $params1 );

/* This is where it rolls back the transaction */
if($query1) {
    sqlsrv_commit( $conn );
    echo "Transaction committed.<br />";
} 
else {
    sqlsrv_rollback( $conn );
    echo "Transaction rolled back.<br />";
}
sqlsrv_close($conn);

The Connection is working, because if i replace $sql1 with 连接正在工作,因为如果我将$ sql1替换为

$sql1 = "SELECT 'Hello world'";

it says commited. 它说承诺。 Any guesses? 有什么猜想吗? Thanks alot, and, in case you seeing some more bad stuff happening in my code, feel free to correct me. 非常感谢,如果您发现我的代码中发生了更多不良情况,请随时纠正我。

And suddenly i've got the answer. 突然我得到了答案。 When I added echo(print_r(sqlsrv_errors(), true)); 当我添加echo(print_r(sqlsrv_errors(), true)); to the code, it says "INSERT permission was denied on the object [...]", so I added permission to the user. 在代码中,它显示“对该对象的插入权限被拒绝”,因此我向用户添加了权限。 Problem solved, thanks to this answer 问题解决了,多亏了这个答案

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

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