douyi8315 2018-08-14 14:19
浏览 89

SQLite正在保存错误的整数?

I have a small PHP webform to store items and prices in a SQLite database.

I store prices in integers, after PHP increases its value *100 (so I indirectly have a rounded to the 2nd decimal price) this way:

$form_Price = intval($form_Price*100);
$query = "insert into items values ($form_Name,$form_Price)";
$results = $db->querySingle($query);

It usually works fine, no issues until today... I got an item with a $2.28 price tag. When I store it using the PHP form, the field show the value = "227" instead of "228". I tried myself different times, I thought there was a rounding mistake somewhere, but it seems I cannot find this bug.

If I insert manually the value into the database as "228" integer, the form using this code

$select_Price = intval($select_Price)/100;
print "Price: $select_price";

shows the correct price "2.28". So I think the problem is somewhere between the PHP form saving the price and the sqlite dabase accepting the query.

I know I should validate and sanitize the forms and the SQL query, but this "thing" is only used by myself as an excercise and it's unreacheable from the internet.

EDIT: As noted by @IncredibleHat replacing intval() with round() actually fixes my problem.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)