从更新的数据库中检索数据
retrieve data from database that is updated
我已经计算了 BMI,如果用户变高或变矮,我可以更新身高(很快就会更新体重)。问题是,在我已经更新身高后,我无法更新 BMI 和 BMI STATUS(体重过轻、正常、肥胖),这对我来说是个大问题..
这是我对 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();
}
}
这是我正在使用的表格bootstrap
<form method="post" action="<?php $_PHP_SELF ?>">
<h3> Height : <?php echo $userRow['height']; ?> 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>
您似乎正试图从 SESSION 中获取有关用户的所有信息,但我认为该信息不存在。但是,您从数据库中读取了用户的当前数据,因此在 $userRow
中所有现有值都可供您使用,因此使用这些值,您的计算可能会起作用。
旁注:确保会话已经启动,并且对于所有使用会话的页面。
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.
您的更新声明应为:
$sql = "UPDATE users ".
"SET height = $height ".
" , bmi = $bmi".
" , bmiresult = $bmiresult".
"WHERE user_id=".$_SESSION['user'];
$result=mysql_query($sql);
我已经计算了 BMI,如果用户变高或变矮,我可以更新身高(很快就会更新体重)。问题是,在我已经更新身高后,我无法更新 BMI 和 BMI STATUS(体重过轻、正常、肥胖),这对我来说是个大问题..
这是我对 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();
}
}
这是我正在使用的表格bootstrap
<form method="post" action="<?php $_PHP_SELF ?>">
<h3> Height : <?php echo $userRow['height']; ?> 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>
您似乎正试图从 SESSION 中获取有关用户的所有信息,但我认为该信息不存在。但是,您从数据库中读取了用户的当前数据,因此在 $userRow
中所有现有值都可供您使用,因此使用这些值,您的计算可能会起作用。
旁注:确保会话已经启动,并且对于所有使用会话的页面。
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 themysqli_
orPDO
extensions. See here for help deciding which Its quite a good read.
您的更新声明应为:
$sql = "UPDATE users ".
"SET height = $height ".
" , bmi = $bmi".
" , bmiresult = $bmiresult".
"WHERE user_id=".$_SESSION['user'];
$result=mysql_query($sql);