开发者

Need input with adding second MySql Update in conditional IF

just trying to add a second Update inside the second if (if $amount == "0.00"

It all works perfectly, until I try to add this bit in below:

Bit to add:

$updateSQL = "UPDATE brick_codes 
              SET number_of_uses= '$number_of_uses' 
              WHERE code= 'bricks_type_0005'";

My code:

if ($amount != "0.00")  :
$updateSQL = "UPDATE bricks 
              SET payment_page_completed = '$payment_page_completed'
                , discount = '$discount'
                , discount_code_used = '$discount_code_used'
                , access_period = '$access_period'
                , gift_certificate = '$gift_certificate'
                , bricks_price = '$bricks_price' 
              WHERE brick_id = '$brick_id'";

elseif( $amount == "0.00" ) : 

$updateSQL = "UPDATE bricks 
              SET payment_page_completed = '$payment_page_completed'
                , discount='$discount'
                , discount_code_used='$discount_code_used'
                , access_period='$access_period'
                , gift_certificate='$gift_certificate'
                , bricks_price='$bricks_price'
                , payment_date='$payment_date'
                , bricks_paid='$bricks_paid'"; 

// THIS BIT TRYING TO ADD BUT NOW WORKING

 $updateSQL = "UPDATE brick_codes 
               SET number_of_uses = '$number_of_uses' 
               WHERE code = 'bricks_type_0005'";

// END NEW BIT

 WHERE bricks_id = '$bricks_id'"; 
 endif;

Ideas? Been looking at it too开发者_运维知识库oo long..


If you use this syntax:

if ($var = something) :

You can also execute one statement after the if. I would advice you to never use this syntax and use brackets { ... } instead, whatever you place inside the brackets is executed, be it 1 or 100 statements.

You code will look something like this:

if ($amount != "0.00") {
  $updateSQL1 = "UPDATE bricks 
          SET payment_page_completed = '$payment_page_completed'
            , discount = '$discount'
            , discount_code_used = '$discount_code_used'
            , access_period = '$access_period'
            , gift_certificate = '$gift_certificate'
            , bricks_price = '$bricks_price' 
          WHERE brick_id = '$brick_id'";
  $updateSQL2 = //insert second UPDATE statement here.
} else { //no need for an explicit test, just use an else.
  $updateSQL1 = //insert update statement here.
  $updateSQL2 = "SELECT 1"; //empty statement, or another UPDATE, whatever you want.
}

The indentation makes the structure of the program clear, every time you have an opening { you indent with 2 spaces, every time you have a closing } you unindent two spaces.

Always use the brackets, even if you have only a single statement inside them, because that way you cannot get errors if you want to add another statement later.


you need to format your if statements better!

if ($amount != "0.00")
{

    $updateSQL = "UPDATE bricks SET payment_page_completed='$payment_page_completed',discount='$discount',discount_code_used='$discount_code_used', access_period='$access_period', gift_certificate='$gift_certificate',bricks_price='$bricks_price' WHERE brick_id='$brick_id'";
}
elseif( $amount == "0.00" )
{

    $updateSQL = "UPDATE bricks SET payment_page_completed='$payment_page_completed',discount='$discount', discount_code_used='$discount_code_used',access_period='$access_period', gift_certificate='$gift_certificate',bricks_price='$bricks_price', payment_date='$payment_date', bricks_paid='$bricks_paid'";

    // Execute this one...

    $updateSQL = "UPDATE brick_codes SET number_of_uses='$number_of_uses' WHERE   code='bricks_type_0005'";

    // Then execute this one...

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜