2012-03-24 89 views
0

我想驗證我的php表單使用異常,但不知何故它不起作用。如果用戶在「nameg」中輸入任何不是字符串的字符,並在「amountg」中輸入任何非整數的字符,都會拋出異常。應例外,即使在這種情況下使用:php腳本不會拋出異常

if(!empty($_POST['nameg']) && !empty($_POST['amountg'])) 
{ 
    $user="rootdummy"; 
    $pass="password"; 
    $db="practice"; 
    $nameg=$_POST['nameg']; 
    $amountg=$_POST['amountg']; 

    try{ 
     if(!is_int($amountg) || !is_string($nameg)){ 
      throw new Exception("This is the exception message!"); 
     } 
    } 
    catch (Exception $e){ 
     $e->getMessage(); 
    } 

    mysql_connect('localhost',$user,$pass) or die("Connection Failed!, " . mysql_error()); 
    $query="INSERT INTO practable (name,given) VALUES('$nameg',$amountg) ON DUPLICATE KEY UPDATE name='$nameg', given=IFNULL(given + $amountg,$amountg)"; 
    mysql_select_db($db) or die("Couldn't connect to Database, " . mysql_error()); 
    mysql_query($query) or die("Couldn't execute query! ". mysql_error()); 

    mysql_close() or die("Couldn't disconnect!"); 
    include("dbclient.php"); 
    echo "<p style='font-weight:bold;text-align:center;'>Information Added!</p>"; 
} 
+0

您的代碼很容易受到SQL注入和mysql_ *輕輕地棄用。 – Paul 2012-03-24 14:07:36

+0

你作爲'$ _POST ['amountg']'發佈了什麼,值? – safarov 2012-03-24 14:08:38

+0

是$ _POST ['amountg']是用戶通過表單輸入的值。 – tutak 2012-03-24 14:15:29

回答

3

想必你想輸出異常?這樣做:

echo $e->getMessage();

編輯:在回答您的評論以後就結束腳本,就把MySQL的查詢try塊。

編輯2:響應您的意見更改驗證。

if(!empty($_POST['nameg']) && !empty($_POST['amountg'])) 
{ 
    $user="rootdummy"; 
    $pass="password"; 
    $db="practice"; 
    $nameg=$_POST['nameg']; 
    $amountg=$_POST['amountg']; 

    try{ 

     if(!ctype_numeric($amountg) || !ctype_alpha($nameg)){ 
      throw new Exception("This is the exception message!"); 
     } 

     mysql_connect('localhost',$user,$pass) or die("Connection Failed!, " . mysql_error()); 
     $query="INSERT INTO practable (name,given) VALUES('$nameg',$amountg) ON DUPLICATE KEY UPDATE name='$nameg', given=IFNULL(given + $amountg,$amountg)"; 
     mysql_select_db($db) or die("Couldn't connect to Database, " . mysql_error()); 
     mysql_query($query) or die("Couldn't execute query! ". mysql_error()); 

     mysql_close() or die("Couldn't disconnect!"); 
     include("dbclient.php"); 
     echo "<p style='font-weight:bold;text-align:center;'>Information Added!</p>"; 

    } 
    catch (Exception $e){ 
     echo $e->getMessage(); 
    } 
} 
+0

只需修改代碼並嘗試,它確實有效!但現在它會拋出異常,即使「nameg」包含字符串,「amountg」包含整數。每次都會拋出!我是否喜歡例外? ( – tutak 2012-03-24 14:32:59

+1

)在catch塊中,放入'var_dump($ amountg)'和'var_dump($ nameg)'並向我們顯示輸出。 – MichaelRushton 2012-03-24 14:34:58

+0

這就是我得到的:這是異常消息!string(2)「45」字符串(7)「testone」 – tutak 2012-03-24 14:37:46

1

你正在追趕它,表演的是幾乎沒有任何的聲明。

$e->getMessage();只是將它作爲一個字符串並將其拋出而不回顯它。

要麼呼應,或重新拋出或者,如果你只是想在這一點上退出,不抓在所有異常(你可以同時刪除try和catch塊)。

+0

的確,我剛剛補充說。不過,如果引發異常,我希望腳本中斷。我補充了休息;在catch塊,但它給了我一個錯誤:致命錯誤:無法打破/繼續1級在c:\ 是我用表單驗證異常的方式正確或應該如果/其他條件使用像初始檢查,看看是否表單是空的? – tutak 2012-03-24 14:12:37

+0

我想你只是想拋出,所以我編輯了它。拋出沒有捕獲將導致異常向上冒泡直到它被捕獲或它到達任何set_exception_handler。 – Paul 2012-03-24 14:16:48

+0

嗯,我想這樣的事情: 嘗試{} catch {echo「enter again!」; include(「form.php」); break;} 類似的東西顯示窗體再次帶有消息,拋出沒有捕獲該例外給我一個異常錯誤。 – tutak 2012-03-24 14:20:55

1

它的確如此,但除了捕獲它外,你對你的異常什麼都不做。

嘗試

echo $e->getMessage()