2012-12-28 51 views
2

如果數字低於1,我發現用逗號將數字轉換爲點號會出現一個問題。如果數字高於1,則一切正常。當數字低於1時,將逗號轉換爲點號

我用這個替換腳本:

$price = str_replace(",", ".", $_POST['PRICE']); 
  • 如果我發佈0.5 - 一切正常
  • 如果我發佈0,5(用逗號),然後我收到此錯誤:

Warning: mysql_query() expects at least 1 parameter, 0 given in....

該字段PRICE是DOUBLE格式。

所有其他高於1的數字都以逗號接受。

+4

你可能也不會逃避或引用的內容。逃脫並引用它。爲了良好,*驗證*用戶輸入。 – Charles

+4

你在哪裏mysql_query行? –

+1

@Tufan'mysql_' !! ??沒有! 'PDO ::' –

回答

2
$_price = str_replace(",", ".", $_POST['PRICE']); // convert to applicable format 
$PRICE = is_numeric($_price) ? (float)$_price : null; // for consistency that there will be smth. similar to number, but not sneaky piece of script from kind user 
1

在運行SQL查詢之前,您應該使用settype()

$PRICE = str_replace(",", ".", $_POST['PRICE']);  
settype($PRICE, 'float'); 
相關問題