简体   繁体   中英

How to make a comparison between prices saved as text in DB and HTML form using PHP?

In my mySQL db, under price I have values like x.xxx as text. for example 3.000, 4.350, 18.400 and so on.

In my HTML form I have some values like

<option value="4.000">4000 €</option>
<option value="5.000">5000 €</option>
<option value="6.000">6000 €</option>

and when I submit I use cast(price as signed) to make a <= comparison.

Sometimes, the result is not correct, as I tried to understand is when there is not a round price saved in DB like 5.360 instead of 5.300

I guess by changing this with something I do not know, it will be fixed.

What do you suggest me for a solution?

if (!empty($price)) $conditions[] = "cast(price as signed) <= '".mysql_real_escape_string($price)."'";

Should this work? 5.360 treat as 5.3

$conditions[] = "FLOOR(cast(price as signed),1) <= '".mysql_real_escape_string($price)."'";

If you want 5.360 to be 5.4 (round up) use:

$conditions[] = "ROUND(cast(price as signed),1) <= '".mysql_real_escape_string($price)."'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM