PHP:验证decimal Mysqli数据类型。

时间:2022-01-03 16:17:34

I have a table with the column data type like this:

我有一个类似这样的列数据类型的表:

price decimal(6,2) unsigned NOT NULL,

And my validation code is like this:

我的验证码是这样的:

if ( (!empty($_POST['price'])) && (is_numeric($_POST['price'])) && (isset($_POST['price'])) > 0 ) {
    $price = ($_POST['price']);

} else{
    $price = FALSE;
    echo '<p> Only accept number and must be higher than zero</p>';

}

I use the $_POST form for users to submit the value. But you know,

我使用$_POST表单让用户提交该值。但你知道,

1/ When the user types any non-numeric value such as a,b,c etc, it also validates well. 2/ When the user types the value zero in, it validates well too.

当用户键入任何非数值的值(如a、b、c等)时,它也能很好地验证。当用户输入值为0时,它也能很好地验证。

However, the question is that when I tested it with no value typed in. I mean that I left the value empty and hit the 'submit' button, the error message still returned as per the }else { part does, but the value was still inserted into the table column with a value of 0.00 technically at the same time.

然而,问题是当我测试它时,没有输入值。我的意思是,我将值空出来并点击“submit”按钮,错误消息仍然按“其他”部分返回,但是该值仍然被插入到表列中,同时值为0.00。

To my limited knowledge, I can guess that the problem was probably at the Mysqli data type of the table I chose, but i don't know how to correct it.

对于我有限的知识,我可以猜测问题可能是我所选择的表的Mysqli数据类型,但我不知道如何修正它。

Can you help me, please?

你能帮我一下吗?

2 个解决方案

#1


1  

I think the solution you're looking for is to simply move the inserting code to the first if statement. That way it'll only insert the value if it is numeric and not empty.

我认为您正在寻找的解决方案是将插入代码移到第一个if语句。这样,它只会插入值,如果它是数字的,而不是空的。

#2


1  

What you describe means that you've failed to stop the insert when $price===false (i.e. the problem is not the evaluation; it has given you the correct message. You've some programming logic error elsewhere)

你所描述的意思是你没有在$price===false时停止插入(即问题不是评估;它给了你正确的信息。你在其他地方有一些编程逻辑错误

#1


1  

I think the solution you're looking for is to simply move the inserting code to the first if statement. That way it'll only insert the value if it is numeric and not empty.

我认为您正在寻找的解决方案是将插入代码移到第一个if语句。这样,它只会插入值,如果它是数字的,而不是空的。

#2


1  

What you describe means that you've failed to stop the insert when $price===false (i.e. the problem is not the evaluation; it has given you the correct message. You've some programming logic error elsewhere)

你所描述的意思是你没有在$price===false时停止插入(即问题不是评估;它给了你正确的信息。你在其他地方有一些编程逻辑错误