SQL Server PHP MS SQL Update return success but no update in database

w41d8nur  于 12个月前  发布在  PHP
关注(0)|答案(1)|浏览(162)

I am trying to update a row in my database via AJAX. $stmt_aufschlag returns true, but no updates in the database are made. Executing the query directly in SQL Management Studio works fine.

Once I define a second variable with sqlsrv_query($conn, $sql_aufschlag) it works and the rows are updating. This is weird because I have several functions where the update query works fine with a single sqlsrv_query and I have another case where I have so assign sqlsrv_query twice. Am I missing some arguements?

Here's my PHP code:

$serverName = "server2020";

$connectionInfo = array("Database"=>"Auftragsmanagement", 'ReturnDatesAsStrings'=> true, "CharacterSet" => "UTF-8");
$conn = sqlsrv_connect($serverName, $connectionInfo);
function save_aufschlag($conn){
        $artikel = $_POST['artikel'];
        $preisgruppe = $_POST['preisgruppe'];
        $aufschlag = str_replace(',', '.', str_replace('.', '', $_POST['aufschlag']));
        $sql_aufschlag = "UPDATE dbo.Artikel SET Kalkulation_AufschlagPG$preisgruppe = $aufschlag WHERE id = '$artikel'";
        $stmt_aufschlag = sqlsrv_query($conn, $sql_aufschlag);
        $test = sqlsrv_query($conn, $sql_aufschlag);
        if($stmt_aufschlag){
            echo json_encode(array("statusCode"=>200, "sql"=>$sql_aufschlag));
        } 
        else if($stmt_aufschlag === false){
            echo json_encode(array("statusCode"=>201, "error"=>print_r( sqlsrv_errors(), true), "sql"=>$sql_aufschlag));
        }
    }
8hhllhi2

8hhllhi21#

Thanks to @Salman A and @Zhorov!

SET NOUCOUNT ON; at the beginning of the SQL Query is the solution.

相关问题