dro80463 2014-08-05 03:07
浏览 93
已采纳

SQL触发器删除异常值

I am sending values to a php script on the same server as a MySQL database. A php script sends the values to the database. However, somewhere along the path sometimes a value loses a decimal and I end up with an extremely high value.

I am monitoring PH and a few other things so the number is relatively low (between 4-12). Sometimes I end up with 720 instead of 7.20. Should this be corrected through a trigger? If so how? Or should it be handled on the PHP side? How can I not accept values or render them null when entering? Thank you for your time.

<!DOCTYPE html>
<html> 
    <body>
        <h1></h1>
        <?php
        // Create connection
        $con = mysqli_connect("localhost", "***", "***", "***");
        // Check connection
        if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

        $temp1 =  ( $_GET["1"]  * 9/5)+32;
        $temp2 =  ( $_GET["3"] *   9/5)+32;

        if ($_SERVER["REQUEST_METHOD"] == "GET") {
            $sql = "INSERT INTO `ANALOG_SENSORS` (`TEMPERATURE`, `HUMIDITY`, `TEMPERATURE2`, `HUMIDITY2`,`PH`,`DATE_TIME`)
            VALUES (" . $temp1 . ", " . $_GET["2"] . ", " . $temp2 . ", " . $_GET["4"] . ", " . $_GET["5"] . ", 
            NOW()- INTERVAL 7 HOUR)"; 
            $result = mysqli_query($con, $sql);
        }


    </body>
</html>
  • 写回答

1条回答 默认 最新

  • duanfeng7756 2014-08-05 03:45
    关注

    Floating-point variables (float / double) should be handled as floating-point variable.

    In MySQL and PHP, valid floating-point number are numbers with a dot as separator between integer and decimal part.

    var_dump(floatval('13.37')) => double(13.37)
    var_dump(floatval('13,37')) => double(13)
    

    As you can see, with a coma, the decimal part is cut off, MySQL act exactly like this.

    You have to be sure that your input data is formatted as is has to be.

    $ph = str_replace(',', '.', $_GET['5']);
    

    In a MySQL query, a coma is reserved character, if your data include a coma and isn't in a string (like in your case), the coma will be interpreted as separator, and add a new column to the insert query.

    SELECT 1.2   => 1.2
    SELECT 1,2   => [1, 2]
    

    Also, make sure that your MySQL table fields' type are correctly set to float/double.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大