Jump to content

Info not adding to database despite a success Message


davidjones1990

Recommended Posts

Hi everyone,

 

I have some code where you enter your height and weight and it calculates your BMI (body mass index) then if you are logged in a button appears where you can add that value to your profile.

 

The problem I am having is the value is not being added to the database and I do not get an error message. I think the problem may be the IF statement. Oh yeh btw you've probably guessed im a beginner!

 

Thanks in advance for any feedback

 

[attachment deleted by admin]

Sorry, here it is

 

<?php
include_once ("scripts/checkuserlog.php");
?>
<?php
include_once ("scripts/connectToMysql.php");

$height = '';
$weight = '';	
$heightSq = ''; 
$bmi = '';	
$correctingFactor = '';	
$bmiAlt = '';
$bmiResult = '';
$errorMsg = '';
$error_msg = '';
$id = $logOptions_id;

if(isset($_POST['height'])){
$height = $_POST['height'];
$weight = $_POST['weight'];

if(!$height){
	$errorMsg .= 'Please enter your height</br>';
}
if(!$weight){
	$errorMsg .= 'Please enter your weight</br>';
} else {


$heightSq = pow($height,2); 
$bmi = $weight/$heightSq;
$correctingFactor = '703';
$bmiCalc = $bmi*$correctingFactor;
$bmiAlt = round($bmiCalc,1);

$bmiResult = '<span class="profileUsername">Your BMI is: ' . $bmiAlt . '</span><br />';

if(isset($_SESSION['idx'])){
	$bmiResult .= '<span class="blackText">Add your BMI result to your profile?</span><form action="calculate_bmi.php" method="post" enctype="multipart/form-data"><input name="postBmi" type="submit" id="postBmi" value="Yes Please!" /></form>';
}

}

}
if(isset($_POST['postBmi'])){
$update = $bmiAlt;

$bmiUpdate = mysql_query("UPDATE members SET bodyMassIndex='$update' WHERE id='$id' LIMIT 1") or die (mysql_error());

if ($bmiUpdate){
        $successMsg = '<span class="errorMsg">BMI has been updated successfully.</span>';
    } else {
	$error_msg = '<span class="errorMsg">Problems arose during the information exchange, please try again later.</span>';
    }
}
?>
<!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=utf-8" />
<title>Calculate your BMI</title>
<link href="style/layout.css" rel="stylesheet" type="text/css" />
<link href="style/main.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="container">
  <?php include_once "bannerFiles/bannerTemplate.php"; ?>
  <div id="content">
  <div id="bmiInfo">
  <p><span class="blackText">BMI stands for Body Mass Index. It is a tool used to compare a persons height against their weight to evaluate whether or not the person is under or over weight. A persons BMI is calculated by dividing their body weight by the square of their height.</span></p>
  <p><span class="blackText">Here is what your body mass index means:</span>
  <li><span class="blackText">Underweight: &#60 18.5</span></li>
  <li><span class="blackText">Normal Weight: 18.5-24.9</span></li>
  <li><span class="blackText">Overweight: 25-29.9</span></li>
  <li><span class="blackText">Obese: &#62 30</span></li></p>
  </div>
  <span class="errorMsg"><?php echo "$errorMsg";?></span>
  <table width="700px" align="center">
  <form action="calculate_bmi.php" method="post" enctype="multipart/form-data">
  	<tr>
    <td align="right"><span class="blackText">Height (in inches):</span></td><td><input name="height" type="text" maxlength"5" size="50"/></td></tr>
    <tr><td align="right"><span class="blackText">Weight (in pounds):</span></td><td><input name="weight" type="text" maxlength"5" size="50"/></td></tr>
    <tr><td></td><td><input name="bmi" type="submit" id="bmi" value="Calculate" /></td></tr>
  </form>
  </table>
  <?php echo "$bmiResult"; ?><br />
  <?php echo "$successMsg"; ?><?php echo "$error_msg"; ?><br />
  <p><span class="blackText">Want to improve your BMI?<br />
  Visit our <a href="forum/index.php">forums</a> for advice</span></p>
  </div>
  <?php include_once "footerFiles/footerTemplate.php";?>
</div>
</body>
</html>

when you create the button to add to database you're generating a form like this:

<form action="index.php" method="post" enctype="multipart/form-data"><input name="postBmi" type="submit" id="postBmi" value="Yes Please!" /></form>

 

but $bmiAlt does not exist in the form, you need to add a hidden element with it's value or store the calculated value in a session variable.

try this:

 

After this line:

$bmiResult = '<span class="profileUsername">Your BMI is: ' . $bmiAlt . '</span><br />';

add:

$_SESSION['bmiAlt'] = $bmiAlt;

 

then change this line:

$update = $bmiAlt;

 

to:

$update = $_SESSION['bmiAlt'];

 

that should grab the correct value to be stored in the database.

 

hope this helps.

Just want to point out the following:

 

You're including scripts/connectToMysql.php at the top, so whatever happens you're always connecting to a mysql database, even though you only need this when someone wants to add the result to their profile. Maybe it would be a good idea to put your connection in a function, and call that function before it's needed instead of always connecting.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.