0

I've already made calculation for BMI and I can update height(will update weight soon) if user getting taller or shorter. The problem is, after I already update the height I cannot update BMI and BMI STATUS(underweight, normal, obese) which is a big problem for me..

This is my coding for php

if(!isset($_SESSION['user']))
{
    header("Location: PHOME.php");
}

$res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);

if(isset($_POST['updateH']))
{

    $height = $_POST['height'];
    $weight = $_SESSION['user'];
    $sex = $_SESSION['user'];
    $bmiresult = $_SESSION['user'];
    $bmi = $weight/(($height/100)*($height/100));

    if ($sex=="female")
    {
        if ($bmi <= 19)
            $bmiresult="underweight!";
        else if ($bmi>19 && $bmi<= 25)
            $bmiresult="normal";
        else if ($bmi > 25 && $bmi<= 30)
            $bmiresult="overweight!";
        else if ($bmi>30) 
            $bmiresult="OBESE!";
    }
        else if ($sex=="male")
    {
        if ($bmi <= 20)
            $bmiresult="underweight!";
        else if ($bmi>20 && $bmi<= 25)
            $bmiresult="normal";
        else if ($bmi > 25 && $bmi<= 30)
            $bmiresult="overweight!";
        else if ($bmi>30) 
            $bmiresult="OBESE!";
    }

    $sql = "UPDATE users SET height = $height, weight = $weight,
                             bmi = $bmi, bmiresult = '$bmiresult' 
            WHERE user_id=" . $_SESSION['user'];

    $result=mysql_query($sql); 

    // if successfully insert data into database, displays message "Successful". 
    if($result){ 
        echo "<script type='text/javascript'>alert('Update Successfully!')</script>";
    } else { 
        echo mysql_error();
    }
}

This is my form which I am using bootstrap

<form method="post" action="<?php $_PHP_SELF ?>">

<h3> Height : <?php echo $userRow['height']; ?>&nbsp; cm</h3> 
    <input type="text" class="small" name="height" id="height" placeholder="Update Height CM"/>
<button type="submit" class="btn btn-warning" name="updateH"> UPDATE </button>

Cœur
  • 37,241
  • 25
  • 195
  • 267
syira
  • 29
  • 8
  • Why cant you update? Whats wrong? Does the form run? Does the script run? Do you get errors? WHATS THE ACTUAL PROBLEM – RiggsFolly Aug 25 '15 at 08:28
  • After I click update button, it just refresh the page. Nothing happen. Height, BMI and bmistatus are not updated too. – syira Aug 25 '15 at 10:31
  • Did you see my answer? – RiggsFolly Aug 25 '15 at 10:34
  • oh yes. I already try but it still does not work,. There is an error at **$sex = $userRow['user'];** it write **Notice: Undefined index: user** – syira Aug 25 '15 at 10:40
  • Check again, I made a silly mistake. should be `$sex = $userRow['sex']` Would have thought you could have worked that out for yourself. _As you are in your final year._ – RiggsFolly Aug 25 '15 at 10:44
  • I already change and there is no error but the data doesn't update and it just refresh after I click update button – syira Aug 25 '15 at 10:52
  • What datatypes are the columns height, bmi, bmiresult and user_id – RiggsFolly Aug 25 '15 at 10:54
  • height and bmi decimal, bmiresult text, user_id integer – syira Aug 25 '15 at 10:57
  • Ahh I missed the SQL query mistakes completely, and the lack of error message being sent to the page. Have another look now – RiggsFolly Aug 25 '15 at 10:58
  • oh, it works now!! I really really appreciate – syira Aug 25 '15 at 11:09
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Aug 25 '15 at 11:40

2 Answers2

0

You appear to be trying to get everything about the user from the SESSION and I assume that information is not there. However, you read the users current data from the database, so all the existing values are available to you in $userRow so use those and your calculation will probably work.

Sidenote: Make sure the session has been started, and for all pages using sessions.

if(!isset($_SESSION['user']))
{
    header("Location: PHOME.php");
    exit; // stop further execution
}

$res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
if ( ! $res ) {
    echo 'cannot find user ' . mysql_error();
}
$userRow=mysql_fetch_array($res);

if(isset($_POST['updateH']))
{
    // if new data is supplied in $_POST use it, 
    // otherwise use the existing userRow values

    $height = isset($_POST['height']) ? $_POST['height'] : $userRow['height'];
    $weight = isset($_POST['weight']) ? $_POST['weight'] : $userRow['weight'];

    $sex = $userRow['sex'];
    $bmiresult = $userRow['bmiresult '];

    //recalc BMI
    $bmi = $weight/(($height/100)*($height/100));

    if ($sex=="female")
    {
        if ($bmi <= 19)
            $bmiresult="underweight!";
        else if ($bmi>19 && $bmi<= 25)
            $bmiresult="normal";
        else if ($bmi > 25 && $bmi<= 30)
            $bmiresult="overweight!";
        else if ($bmi>30) 
            $bmiresult="OBESE!";
    }
        else if ($sex=="male")
    {
        if ($bmi <= 20)
            $bmiresult="underweight!";
        else if ($bmi>20 && $bmi<= 25)
            $bmiresult="normal";
        else if ($bmi > 25 && $bmi<= 30)
            $bmiresult="overweight!";
        else if ($bmi>30) 
            $bmiresult="OBESE!";
    }

    $sql = "UPDATE users ".
       "SET height = $height ".
       "SET bmi = $bmi".
       "SET bmiresult = $bmiresult".
       "WHERE user_id=".$_SESSION['user'];
    $result=mysql_query($sql); 

    // if successfully insert data into database, displays message "Successful". 
    if($result){ 
        echo "<script type='text/javascript'>alert('Update Successfully!')</script>";
    } else {
        echo mysql_error();
    }
}

As you are obviously just starting out on your PHP journey, please do not waste your time learning the mysql_ database extension as it is soon to be removed from PHP. Instead, spend your time learning either the mysqli_ or PDO extensions. See here for help deciding which Its quite a good read.

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • ohh wow , it quiet scary though, since I am using mysql_ for my final year project. Thanks for inform me – syira Aug 25 '15 at 10:43
0

Your update statement should read:

  $sql = "UPDATE users ".
       "SET height = $height ".
       " , bmi = $bmi".
       " , bmiresult = $bmiresult".
       "WHERE user_id=".$_SESSION['user'];
    $result=mysql_query($sql); 
James Anderson
  • 27,109
  • 7
  • 50
  • 78