0

我正在创建一个页面,用户可以在其中计算他们的 BMI,然后将其保存到他们的个人资料中。首先,我验证 BMI。但是,当我现在执行此计算时,它会将“0.99”插入到数据库中,而不是正确的 BMI 值。我一直在调试并认为它与与数据库的连接有关(已尝试将其移至脚本的其他区域,但没有任何区别)。

代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
</head>
<link rel="stylesheet" href="css.css" type="text/css" />
<body>
    <?php

    error_reporting(E_ALL &~ E_NOTICE);

    // Start the session
    session_start();


    // Make sure the browser is transmitting in UTF-8
    header('Content-type: text/html; charset=utf-8');

        // Clear the error message
    $error_msg = "";

    if (isset($_SESSION['user_id']))
        echo $_SESSION['user_id'];

    $dbc = mysqli_connect('localhost', 'root', 'root', 'help_me_be_healthy') or die("Error " . mysqli_error($dbc));
    mysqli_set_charset($dbc, "utf8");


    if(isset($_POST['submit']))
    {
        $bmi=0;
        $kg=$_POST['kg'];
        $mt=$_POST['mt'];

        if(empty($kg) || empty($mt))
        {
            echo "<label class='err'><center>All fields are required</center></label>";
            include("index.php");
        }

        else if(!is_numeric($kg) && !is_numeric($mt) )
        {
            echo "<label class='err'>Please enter valid data.</label>";
            include("index.php");
        }
        else
        {
            $bmi = $kg/($mt*$mt);
            $bmi=round($bmi,2);
            if ( $bmi <= 18.5 ) 
            {
                echo "Your BMI is " .$bmi."  which means you are underweight";

            }

            else if ( $bmi>18.5 && $bmi <= 24.9) {
                echo  "Your BMI is ".$bmi." which means you are normal";
            }

            else if ( $bmi>29.9 && $bmi> 24.9 ) {
                echo "Your BMI is ".$bmi." which means you are overweight";
            }

            else if ( $bmi >29.9 && $bmi<=39.9 ) {
                echo "Your BMI is ".$bmi." which means you are obese";

            }
            else
            {
                echo "You are morbidly obese.";

            } 
            include("index.php");

            //$query = "UPDATE `users` SET `user_bmi`= '$bmi' 
            //WHERE `user_id` = ($_SESSION = ['user_id'])";

            //echo $_SESSION['user_id'];



            //if (!isset($_GET['user_id'])) {
            //$query = "SELECT * FROM 'users'";
            $query = "UPDATE `users` SET `user_bmi`= '$bmi' WHERE `user_id` = '" . $_SESSION['user_id'] . "'";
            //}
            //else {
            //  $query = "UPDATE `users` SET `user_bmi`= '$bmi' WHERE `user_id` = '" . $_GET['user_id'] . "'";
            //}

            $data = mysqli_query($dbc, $query);
            $row = mysqli_fetch_assoc($data);
            print "\n----\nLookup:\n";
            print "Num rows: " . mysqli_num_rows($data);
            print "\n";
            print_r($row);
            print '</pre>';
            return;

        }
    }
    ?>
</body>
</html>

任何帮助将非常感激:)

莎拉

4

1 回答 1

0

假设 BMI 被正确计算/显示,并且user_bmi列是浮点数/双精度数/数字/十进制,则UPDATE不需要在值周围加上引号,因此 SQL 应该是:

$query = "UPDATE `users` SET `user_bmi`=$bmi WHERE `user_id`=".
    $_SESSION['user_id'];

user_id我也不需要转义,取决于它是数字还是字符串)

此外,您无法从 UPDATE SQL 命令获取查询结果 (fetch_assoc)...如果您想知道受影响的行数,请使用mysqli_affected_rows

print "Num rows: ".mysqli_affected_rows($dbc);

是什么让您认为 BMI 输入不正确,您确定user_id是正确的,您如何查看数据库以确定存储的内容?

更新的代码打印输出代码:

if (!mysqli_query($dbc,$query)) {
   echo "Failed to store";
}
echo "<pre>\n----\nLookup:\nQuery:$query\n".
  "Num rows:".mysqli_affected_rows($dbc)."\n</pre>\n";
于 2013-10-31T22:22:41.077 回答