我有几个表格来记录users
体重,他们的旧体重weight_hisotry
以及他们可以在points_history
表中得分的分数。
我遇到的问题是计算线,对我来说似乎很好,但它没有给我正确的结果
这是我的PHP代码:
<?php
include_once("php_includes/check_login_status.php");
$username = "";
$weight = "";
$height = "";
$weighthist = "";
$id = "";
if(isset($_GET["u"])){
$username = preg_replace('#[^a-z0-9]#i', '', $_GET['u']);
}
//Fetch all users information This is were WEIGHT is stored.
$user_query=mysqli_query($db_conx,"SELECT * FROM users WHERE username='$username' AND activated='1' LIMIT 1");
while ($row = mysqli_fetch_array($user_query)) {
$id = $row ["id"];
$username = $row ["username"];
$weight = $row["weight"];
$points = $row['point_hist'];
} /* END OF WHILE LOOP $user_query */
//Fetch all from points_hisotry this is where Point_hist is stored
$q12 = mysqli_query($db_conx,"SELECT * FROM points_history WHERE id='$id'");
while ($row = mysqli_fetch_array($q12)) {
$id = $row ["id"];
$points = $row['point_hist'];
}
// submit this information upon change of weight
if (isset($_POST['submit'])){
$id = $_POST["id"];
$username = $_POST['username'];
$weight = $_POST['weight'];
$weighthist = $_POST['weighthist'];
$point_hist = $_POST['point_hist'];
$exists = mysqli_query($db_conx,"SELECT * FROM users WHERE username='$username'");
if (mysqli_num_rows($exists)!=0) {
/* update the description in the database */
在这里,我有存储在数据库中的点,因此例如,如果用户已经获得 10 分并且他们多获得 10 分,则他们应该有 10 + 10,等于 20 分。 但不知何故,当我将信息更新为points_history时,我最终只会得到更新的 10 分。
我还测试了我的代码,回显了正确的现有分数。
/* calculate score */
$calweight = $weight - $weighthist;
$point_hist = (($calweight * 10) + $points);
// insert the post values into weigghthistory
mysqli_query($db_conx,"INSERT INTO weighthistory (id, weighthist, date) VALUES ('$id','$weighthist',now())");
// Update weight in the users table.
mysqli_query($db_conx,"UPDATE users SET weight='$weighthist' WHERE id='$id'");
// update points in points_history to the new weight.
mysqli_query($db_conx,"UPDATE points_history SET point_hist='$point_hist' WHERE id='$id'");
} /* END OF ELSE IF $exists NOT 0 */
else { echo "the name does not exist"; }
} /* END OF ISSET SUBMIT */
?>
以下是 HTML 代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Profile Update: <?php echo $u; ?></title>
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="style.css">
<script src="js/main.js"></script>
<script src="js/javascript.js"></script>
<script src="js/ajax.js"></script>
<script type="text/javascript">
function validateForm()
{
var x=document.forms["newweight"]["weighthist"].value;
if (x==null || x=="")
{
alert("First name must be filled out");
return false;
}
}
</script>
<style type="text/css">
#updateform{ margin-top:24px;}
#updateform > div { margin-top: 12px;}
#updateform > input {width: 200px; padding: 3px;background: #F3F9DD;}
</style>
</head>
<body>
<?php include_once("template_pageTop.php"); ?>
<div id="pageMiddle">
<div id="usernamecss"> Username: <?php echo $username; ?></div>
<form action="newweight.php" method="POST" id="newweight" name="newweight" onsubmit="return validateForm()">
<input id="id" type="hidden" name="id" value="<?php echo $id?>" maxlength="16" readonly/>
<input id="username" type="hidden" name="username" value="<?php echo $username?>" maxlength="16" readonly/>
<br />
<div><b>Current Weight: </b>KG <?php echo $weight?> </div>
<input id="weight" type="hidden" name="weight" value="<?php echo $weight?>" readonly/>
<br />
<div><b>Current Points: </b> <?php echo $points?> </div>
<input id="points" type="hidden" name="points" value="<?php echo $points?>" readonly/>
<br />
<input id="weighthist" type="text" name="weighthist" placeholder="Enter New Weight" value="<?php echo $weighthist?>" >
<br />
<input type="submit" name="submit" id="submit" value="Update Description"></p>
<a href="user.php<?php echo "?u=",$username;?>">Go to Profile</a>
</form>
</div>
<?php include_once("template_pageBottom.php"); ?>
</body>
我还希望页面显示分数,并在提交时以正确的分数刷新页面。 谁能建议我如何做到这一点 我是编码新手。
以下内容应该为您指明正确的方向。未经测试。在适当的时候评论。
<?php
include_once 'php_includes/check_login_status.php';
$username='';
$weight='';
$height='';
$weighthist='';
$id='';
isset($_GET['u']) ? $username=$_GET['u'] : $username=$_POST['username'];
$username=preg_replace('#[^a-z0-9]#i','',$username);
//Concatenate both of your queries with a JOIN
$user_query=mysqli_query($db_conx,"SELECT users.*,points_history.point_hist FROM users JOIN points_history ON users.id=points_history.id WHERE users.username='$username' AND users.activated=1 LIMIT 1;");
if($row=mysqli_fetch_array($user_query)){
$id=$row['id']; //Empty if no result from query
$username=$row['username'];
$weight=$row['weight'];
$points=$row['point_hist']; //Existing points
}
if($id && isset($_POST['submit'])){ //Also fail if $id is empty
$username=$_POST['username'];
$weighthist=$_POST['weighthist'];
// update the description in the database
// calculate score
$calweight=$weight-$weighthist;
$points+=$calweight*10; //$a+=5 is equivalent to $a=$a+5
// insert the post values into weighthistory
mysqli_query($db_conx,"INSERT INTO weighthistory (id,weighthist,date) VALUES ($id,$weighthist,now());");
// Update weight in the users table.
mysqli_query($db_conx,"UPDATE users SET weight=$weighthist WHERE id=$id;");
// update points in points_history to the new weight.
mysqli_query($db_conx,"UPDATE points_history SET point_hist=$points WHERE id=$id;");
} /* END OF ISSET SUBMIT */
//...
echo $points; //$points will always be up-to-date because of the above. Use where needed.
?>
http://dev.mysql.com/doc/refman/5.0/en/join.htmlhttp://www.php.net/manual/en/language.operators.assignment.php
网页表单:
<div id="pageMiddle">
<div id="usernamecss"> Username: <?php echo $username;?></div>
<form action="newweight.php" method="POST" id="newweight" name="newweight" onsubmit="return validateForm()">
<input id="username" type="hidden" name="username" value="<?php echo $username?>" />
<br />
<div><b>Current Weight: </b>KG <?php echo $weight;?></div>
<br />
<div><b>Current Points: </b> <?php echo $points;?></div>
<br />
<input id="weighthist" type="text" name="weighthist" placeholder="Enter New Weight" value="<?php echo $weighthist?>" >
<br />
<input type="submit" name="submit" id="submit" value="Update Description" />
<a href="user.php?u=<?php echo $username;?>">Go to Profile</a>
</form>
</div>
<?php include_once("template_pageBottom.php"); ?>
</body>