mysql/php 上两个表的加法和减法

Addition and substraction from two tables on mysql/php

我正在尝试建立一个积分系统,您可以在其中 select 从 1 到 5

  <form class='form-horizontal' action='../api/points.php' method='post'><fieldset>
       <input type='radio' value='1' name='points' /> 1 </input>
                 <input type='radio' value='2' name='points'  /> 2 </input>
                 <input type='radio' value='3' name='points'  /> 3 </input>
                 <input type='radio' value='4' name='points'  /> 4 </input>
                 <input type='radio' value='5' name='points'  /> 5 </input>
                 <br />
                 <input name='Submit' type='submit' value='give points' />
                 <input name='userid' type='hidden' value='<? echo $row['user_id'] ?>' />
                 <input name='postid' type='hidden' value='<? echo $row['id'] ?>;' />
                 <input name='currentpoints' type='hidden' value='<? echo $row['postpoints']; ?>' />
                 <input name='user2id' type='hidden' value='<? echo $loggedInUser->user_id; ?>' />
                 </fieldset>
                 </form>

有两个 ID,给予积分的用户 ID 和接收积分的用户 ID。

我必须存储它们:(1) 在 posts table 上,它记录了 post 已给出的总点数。 (2) 点数应从给予的用户中减去,并添加到 post编辑它的用户。

它给 post 加分,但不会增加或减少用户 table

$pointsvar = htmlentities($_POST['points']);
$userid = htmlentities($_POST['userid']);
$user2id = htmlentities($_POST['user2id']);
$postid = htmlentities($_POST['postid']);
$currentpoints = htmlentities($_POST['currentpoints']);
$suma1 = $currentpoints+$pointsvar;

$sql = "UPDATE ft_posts SET postpoints=(postpoints + $pointsvar) WHERE id='$postid'";

if ($conn->query($sql) === TRUE) {
    echo "post points updated successfully";
    echo "<br />";
    echo "current post points+points variable: ";
    echo ($suma1);
        echo "<br />";
} else {
    echo "Error updating record: " . $conn->error;
}

$sql2 = "UPDATE ft_users SET points='(points + $pointsvar)' WHERE id='$userid'";

if ($conn->query($sql2) === TRUE) {
    echo "User given points updated successfully";
        echo "<br />";
} else {
    echo "Error updating record 2: " . $conn->error;
}

      $sql3 = "UPDATE ft_users SET points='(points - $pointsvar)' WHERE id='$user2id'";

if ($conn->query($sql2) === TRUE) {
    echo "User taken points updated successfully";
        echo "<br />";
} else {
    echo "Error updating record 3: " . $conn->error;
}$conn->close();

删除 points='(points + $pointsvar)'
中的引号 SET points='(points - $pointsvar)'

关于这一行:

if ($conn->query($sql2) === TRUE) { 

您将其与 $sql3 结合使用,因此应该是:

if ($conn->query($sql3) === TRUE) {