2013-03-09 72 views
0

這是我的代碼看起來像執行的語句:停止代碼,如果從當錯誤發生時

if($allthevaluesarenotempty) { 
    if($pictureexist) { 
     // upload picture code 
    } 
    else 
    { 
     $message = "error"; 
    } 

    // sql query here 
} 

我如何阻止時$message設置執行SQL查詢?

+0

[你有什麼試過?](http://whathaveyoutried.com /) – 2013-03-09 23:40:27

+0

你錯誤地表達了你的目標 - 如果發生消息,你不應該停止,但是當你停止時你應該顯示消息。 – Voitcus 2013-03-09 23:41:33

+0

@JohnVanDeWeghe當發生錯誤時,我嘗試重定向頁面\t'header('Location:'。$ _SERVER ['HTTP_REFERER']); '但我正在尋找更好的解決方案。 – shnisaka 2013-03-09 23:41:58

回答

3
  • 如果在這樣一段時間,並foreach循環的情況下,你可以使用continue;逃脫迭代和break;結束循環。

  • 在函數return false;將停止執行。

  • 在PHP文檔中,exit()將終止腳本的進一步執行。

所以,使用哪個是合適的。

+1

'return true;'? :) – 2013-03-10 00:03:52

+0

@AkramBerkawy,ha ha – Starx 2013-03-10 03:09:40

1

檢查$message,看看是否出現了錯誤:

if($allthevaluesarenotempty) { 

    $message = '';  

    if ($pictureexist) { 

     // upload picture code 

    } else { 

     $message = "error"; 

    } // if $pictureexist 

} // if $allyourvalues... 

// later in your code check $message 

if ($message == 'error') { 

    // do something, there was an error... 

} // if $message 
+0

'if($ message =='error')''可能會返回'warning'或注意'$ allthevaluesarenotempty'是否爲false。 – 2013-03-10 10:41:18

1

你可以檢查變量$message,看看它是否是euql爲「錯誤」:

if($allthevaluesarenotempty) { 
    if($pictureexist){ 
     // upload picture code 
    } 
    else 
    { 
     $message = "error"; 
    } 
    if (@$message != "error")) { 
     //sql query here 
    } 
} 

,或者你可以把你的SQL在if區塊中查詢

if($allthevaluesarenotempty) { 
    if($pictureexist){ 
     // upload picture code 

     //sql query here 
    } 
    else 
    { 
     $message = "error"; 
    } 
} 
相關問題