1

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.

2
  • You are overwriting each query by the next one. Commented Nov 9, 2012 at 11:16
  • Also add ; inside each query at the end. Commented Nov 9, 2012 at 11:18

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

That would not execute the queries in once. Also, that would only work if the queries are always incremented like that
2

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.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.