'PHP MS SQL Update return success but no update in database
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));
}
}
Solution 1:[1]
Thanks to @Salman A and @Zhorov!
SET NOUCOUNT ON; at the beginning of the SQL Query is the solution.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Phil |
