syira
syira

Reputation: 29

calling data from database to make calculation (PHP)

I have problem, I want to calculate summation of height and weight after height is updated. The problem is after I click update, height is updated however weight will automatically change value to user_id (integer). So the summation will be bmi=height+id. Can you guys help solve my problem?

Below is PHP coding and form which I am using Bootstrap.

<!-- begin snippet: js hide: false -->

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

    $height = $_POST['height'];

    $weight = $_SESSION['user'];

    $bmi = $height + $weight;

    $sql = "UPDATE users 
        SET 
        height = $height, 
        weight = $weight,
        bmi = $bmi, 
        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();
}
<form method="post" role="form">

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

    </form>

Upvotes: 2

Views: 1914

Answers (2)

Rajesh kannan
Rajesh kannan

Reputation: 634

You don't have input field for Weight. May be you are noticing it or maybe I'm wrong Ok try this html code

<form method="post" role="form">
<h3> Height : <?php echo $userRow['height']; ?>&nbsp; cm</h3> 
  <input type="number" class="small" name="height" id="height" min="100" max="200" placeholder="Update Height CM"/>
  <input type="number" class="small" name="weight" id="height" min="10" max="200" placeholder="Update Weight CM"/>
  <button type="submit" class="btn btn-warning" name="updateH"> UPDATE </button>

    </form>

And in javascript use the following

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

    $height = $_POST['height'];

    $weight = $_POST['weight'];

    $bmi = $height + $weight;

    $sql = "UPDATE users SET height ='".$height."',weight ='".$weight."',bmi ='".$bmi."' 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();
}

Upvotes: 0

Rajesh kannan
Rajesh kannan

Reputation: 634

It seems like a format error in your sql statement Use this

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

If it is not updated for the above query

call mysql_commit(); after your mysql_query(); statement

Let me know if it is helpful

Upvotes: 1

Related Questions