1

我在 PHP 中一次执行大量 ms_sql 查询时遇到问题:

以下是我正在尝试做的简化版本:

$updatesql = "UPDATE Pricing SET Price = '".$_POST['price1']."' WHERE PriceID = 1 ";
$updatesql = "UPDATE Pricing SET Price = '".$_POST['price2']."' WHERE PriceID = 2 ";
$updatesql = "UPDATE Pricing SET Price = '".$_POST['price3']."' WHERE PriceID = 3 ";
$updatesql = "UPDATE Pricing SET Price = '".$_POST['price4']."' WHERE PriceID = 4 ";
$updatesql = "UPDATE Pricing SET Price = '".$_POST['price5']."' WHERE PriceID = 5 ";
$updatesql = "UPDATE Pricing SET Price = '".$_POST['price6']."' WHERE PriceID = 6 ";
$updatesql = "UPDATE Pricing SET Price = '".$_POST['price7']."' WHERE PriceID = 7 ";
$updatesql = "UPDATE Pricing SET Price = '".$_POST['price8']."' WHERE PriceID = 8 ";

$executesql = mssql_query($updatesql);

当我一一浏览它们时,它们都可以工作,但是当我尝试一次全部执行它们时,似乎只有最后一个查询被执行。

我正在尝试做的事情可能吗?任何指示我哪里出错了?抱歉,对 PHP 来说还很陌生。

4

3 回答 3

2

那是因为您每次都过度编写查询字符串。尝试以下操作:

    $updatesql = '';
    for ($i = 1; $i < 9; $i++) {
        $updatesql .= "UPDATE Pricing SET Price = '".$_POST['price' + $i]."' WHERE PriceID = {$i}; ";
    }
    $executesql = mssql_query($updatesql);
于 2012-11-09T11:18:57.257 回答
2

您正在用新查询覆盖您自己的查询,因此最后只有您创建的最后一个查询将仅在该变量中。

因此,您无法执行所有操作,要执行所有操作,您必须选择循环或 ; 用mysql的方法。

即使您可以在查询之前编写 mysql_query ,所以所有查询都将被更新。

$updatesql = mssql_query("UPDATE Pricing SET Price = '".$_POST['price1']."' WHERE PriceID = 1 ");
$updatesql = mssql_query"UPDATE Pricing SET Price = '".$_POST['price2']."' WHERE PriceID = 2 ");
$updatesql = mssql_query"UPDATE Pricing SET Price = '".$_POST['price3']."' WHERE PriceID = 3 ");
$updatesql = mssql_query"UPDATE Pricing SET Price = '".$_POST['price4']."' WHERE PriceID = 4 ");
$updatesql = mssql_query"UPDATE Pricing SET Price = '".$_POST['price5']."' WHERE PriceID = 5 ");
$updatesql = mssql_query"UPDATE Pricing SET Price = '".$_POST['price6']."' WHERE PriceID = 6 ");
$updatesql = mssql_query"UPDATE Pricing SET Price = '".$_POST['price7']."' WHERE PriceID = 7 ");
$updatesql = mssql_query"UPDATE Pricing SET Price = '".$_POST['price8']."' WHERE PriceID = 8 ");

// $executesql = mssql_query($updatesql);

有很多方法可以在单个语句中执行查询。

于 2012-11-09T11:23:50.280 回答
1

试试这个

$updatesql = "UPDATE Pricing SET Price = '".$_POST['price1']."' WHERE PriceID = 1 ;";
$updatesql.= "UPDATE Pricing SET Price = '".$_POST['price2']."' WHERE PriceID = 2 ;";
$updatesql.= "UPDATE Pricing SET Price = '".$_POST['price3']."' WHERE PriceID = 3 ;";
$updatesql.= "UPDATE Pricing SET Price = '".$_POST['price4']."' WHERE PriceID = 4 ;";
$updatesql.= "UPDATE Pricing SET Price = '".$_POST['price5']."' WHERE PriceID = 5 ;";
$updatesql.= "UPDATE Pricing SET Price = '".$_POST['price6']."' WHERE PriceID = 6 ;";
$updatesql.= "UPDATE Pricing SET Price = '".$_POST['price7']."' WHERE PriceID = 7 ;";
$updatesql.= "UPDATE Pricing SET Price = '".$_POST['price8']."' WHERE PriceID = 8 ;";

$executesql = mssql_query($updatesql);
于 2012-11-09T11:16:15.743 回答