我在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很新。
答案 0 :(得分:2)
那是因为你每次都要过度编写查询字符串。请尝试以下方法:
$updatesql = '';
for ($i = 1; $i < 9; $i++) {
$updatesql .= "UPDATE Pricing SET Price = '".$_POST['price' + $i]."' WHERE PriceID = {$i}; ";
}
$executesql = mssql_query($updatesql);
答案 1 :(得分: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 ");
// $ executionql = mssql_query($ updatesql);
有很多方法可以在一个语句中执行查询。
答案 2 :(得分: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);