2015-02-17 22 views
1

我试图做一个点系统,你可以选择从1到5加法和减法从MySQL的/ PHP的两个表

<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)在帖子表上它记录发布的点数的总数。 (2)这些积分应该从发布并添加到发布用户的用户中减去。

这是给的职位之分,但没有从用户表中添加或

。减去
$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(); 
+1

删除'points ='(points + $ pointsvar)''中的引号即使是'points = points + $ pointsvar'也不需要括号。减法的情况也是如此。检查错误会显示一个错误,你没有告诉我们错误是错误的。 – 2015-02-17 21:23:36

+0

旁注:如果你打算使用短标签,你可以减少你的<?回声'下降到'<?='*甜心?* – 2015-02-17 21:31:01

+0

好吧,它开始工作的另一面(接受积分的用户),但它不减...谢谢你的提示:) – 2015-02-17 21:59:36

回答

1

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

至于这一行:

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

您正在使用它与$sql3所以应该是:

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