简体   繁体   中英

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.

Besides my code being a stackowerflow patchwork, it kinda works, only that I cant figure what I did wrong with the query. 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.

$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 = "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)); 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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