简体   繁体   中英

Executing Multiple MSSQL Queries with PHP at once

I am having trouble with executing numerous ms_sql queries at once in PHP:

Below is a simplified version of what I am trying to do:

$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);

When I go through them one by one they all work, however when I try to execute them all at once, only the LAST query seems to get executed.

Is what I'm trying to do possible? Any pointers where Im going wrong? Sorry fairly new to PHP.

Thats because your over writing the query string each time. Try the following:

    $updatesql = '';
    for ($i = 1; $i < 9; $i++) {
        $updatesql .= "UPDATE Pricing SET Price = '".$_POST['price' + $i]."' WHERE PriceID = {$i}; ";
    }
    $executesql = mssql_query($updatesql);

You are overwriting your own query with the new one, so at last only the last query that you have created will be in that variable only.

So you cannot able to execute that all, to execute all you have to choose either looping or ; method with mysql.

Even though you can write mysql_query before the queries so all the query will be updated.

$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);

There are lot of ways to execute queries in a single statement.

Try this

$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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM