sql 使用准备好的语句更新 foreach 循环中的多列

sql update multiple column in a foreach loop using prepared statement

我正在研究这个 PHP 脚本,了解如何准备多次执行 UPDATE 语句。下面的脚本显示了使用准备语句更新 1 列。

示例来自 PHP 手册 https://www.php.net/manual/en/function.sqlsrv-prepare.php

<?php
$serverName = "serverName\sqlexpress";
$connectionInfo = array( "Database"=>"dbName", "UID"=>"username", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false) {
    die( print_r( sqlsrv_errors(), true));
}

$sql = "UPDATE Table_1
        SET OrderQty = ?
        WHERE SalesOrderID = ?";

// Initialize parameters and prepare the statement. 
// Variables $qty and $id are bound to the statement, $stmt.
$qty = 0; $id = 0;
$stmt = sqlsrv_prepare( $conn, $sql, array( &$qty, &$id));
if( !$stmt ) {
    die( print_r( sqlsrv_errors(), true));
}

// Set up the SalesOrderDetailID and OrderQty information. 
// This array maps the order ID to order quantity in key=>value pairs.
$orders = array( 1=>10, 2=>20, 3=>30);

// Execute the statement for each order.
foreach( $orders as $id => $qty) {
    // Because $id and $qty are bound to $stmt1, their updated
    // values are used with each execution of the statement. 
    if( sqlsrv_execute( $stmt ) === false ) {
          die( print_r( sqlsrv_errors(), true));
    }
}
?>

如果我有多个列要更新怎么办,我如何创建一个数组以将多个变量绑定到 foreach 中的准备语句?

包含 3 列的新更新 SQL 语句。

$sql = "UPDATE Table_1
        SET OrderQty = ?,
        SET ProductName = ?,
        SET ProductPRice = ?
        WHERE SalesOrderID = ?";

您可以尝试使用不同的实际参数值构建数组。并修复 UPDATE 语句的语法:

<?php
$serverName = "serverName\sqlexpress";
$connectionInfo = array("Database" => "dbName", "UID" => "username", "PWD" => "password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if ($conn === false) {
    die( print_r( sqlsrv_errors(), true));
}
$sql = "
    UPDATE Table_1
    SET OrderQty = ?, ProductName = ?, ProductPrice = ?
    WHERE SalesOrderID = ?
";

// Initialize parameters and prepare the statement. 
// Variables $qty and $id are bound to the statement, $stmt.
$qty = 0; $name = ""; $price = 0.00; $id = 0;
$stmt = sqlsrv_prepare($conn, $sql, array(&$qty, &$name, &$price, &$id));
if ($stmt === false) {
    die( print_r( sqlsrv_errors(), true));
}

// Set up the SalesOrderDetailID and OrderQty information. 
// This array maps the order ID to order quantity in key=>value pairs.
$orders = array(
    array("qty" => 10, "name" => "Product1", "price" => 10.01, "id" => 1),
    array("qty" => 20, "name" => "Product2", "price" => 10.02, "id" => 2),
    array("qty" => 30, "name" => "Product3", "price" => 10.03, "id" => 3)
);

// Execute the statement for each order.
foreach ($orders as $order) {
    // Because $id and $qty are bound to $stmt1, their updated
    // values are used with each execution of the statement. 
    $qty   = $order["qty"];
    $name  = $order["name"]; 
    $price = $order["price"];
    $id    = $order["id"];
    if (sqlsrv_execute($stmt) === false) {
        die( print_r( sqlsrv_errors(), true));
    }
}

// End
sqlsrv_free_stmt($stmt);  
sqlsrv_close($conn); 
?>