简体   繁体   English

PHP MySQL和计算错误

[英]PHP MySQL and Calculation Errors

I have a few tables to record users weight, their old weight in weight_hisotry and their points they can score in the points_history table. 我有一些表来记录users体重,他们的旧体重( weight_hisotry以及可以在points_history表中得分的分数。

the problem occurs with me is with the calculation line, it seems to be fine to me but it does not give me the correct result 我的问题出在计算线上,对我来说似乎很好,但不能给我正确的结果

here is my php code: 这是我的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 */

Here I have points where its stored in the database, so for example if user has 10 points alreadys and they score 10 more points they should have 10 + 10 which equals to 20 points. 在这里,我有将其存储在数据库中的点,因此,例如,如果用户已经有10点,并且他们又获得了10分,则他们应该有10 + 10,等于20点。 but somehow when I update the information into points_history I only end up with the updated 10. 但是以某种方式将信息更新为points_history时,我只会得到更新的10。

I also tested my code with echoing the existing score which comes out correct. 我还通过回显正确的现有分数来测试我的代码。

/* 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 */

?>  

Here is the HTML code: 这是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>

I also want the page to display score and upon submit to refresh the page with the correct score. 我还希望页面显示分数,并在提交后刷新具有正确分数的页面。 can anyone suggest how I can go by doing this I am new to coding. 谁能建议我可以做到这一点,我是编码的新手。

The following should point you in the right direction. 以下内容将为您指明正确的方向。 Untested. 未经测试。 Commented where appropriate. 在适当的地方进行了评论。

<?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.html http://www.php.net/manual/en/language.operators.assignment.php http://dev.mysql.com/doc/refman/5.0/en/join.html http://www.php.net/manual/zh/language.operators.assignment.php

HTML form: HTML形式:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM