根据值更新数据库

时间:2014-09-25 14:15:29

标签: php mysqli

我有一些代码应该根据某些值更新数据库。这是一场比赛,在我的比赛中我有“攻击”和“防守”。我也有排名(在数据库中称为“playerrank”)。我希望排名根据玩家拥有的“攻击”和“防守”来改变。听起来很简单不是吗?嗯,这不是:(反正对我来说..

这就是我的意思

1到1000之间的攻击,等级为1 攻击在1001和2000之间,等级为2 等

问题是,无论我有多少攻击/防御,等级都会保持为1,我没有收到任何错误消息!

这是相关代码

if ($attack AND $defense >1 && $attack AND $defense < 1000){
$update_rank = mysqli_query($con,"UPDATE stats SET stats.playerrank = 1 WHERE stats.id=$id4");

}elseif ($attack AND $defense >1001 && $attack AND $defense < 2000){
$update_rank2 = mysqli_query($con,"UPDATE stats SET stats.playerrank = 2 WHERE stats.id=$id4");

}elseif ($attack AND $defense >2001 && $attack AND $defense < 3000){
$update_rank6 = mysqli_query($con,"UPDATE stats SET stats.playerrank = 3 WHERE stats.id=$id4");

}elseif ($attack AND $defense >3001 && $attack AND $defense < 4000){
$update_rank5 = mysqli_query($con,"UPDATE stats SET stats.playerrank = 4 WHERE stats.id=$id4");

}elseif ($attack AND $defense >4001 && $attack AND $defense < 5000){
$update_rank4 = mysqli_query($con,"UPDATE stats SET stats.playerrank = 5 WHERE stats.id=$id4");

}elseif ($attack AND $defense >5001 && $attack AND $defense < 6000){
$update_rank3 = mysqli_query($con,"UPDATE stats SET stats.playerrank = 6 WHERE stats.id=$id4");

}

这是完整的代码

<?php
include("connect.php");
include("header.php");
$username = $_SESSION['userlogin'];
$getattack = "SELECT stats.id, stats.attack, users.id, users.username FROM stats, users WHERE users.username = '$username' AND stats.id = users.id";
$getdefense = "SELECT stats.id, stats.defense, users.id, users.username FROM stats, users WHERE     users.username = '$username' AND stats.id = users.id";
$gotattack = mysqli_query($con,$getattack);
$gotdefense = mysqli_query($con,$getdefense);
$attack = mysqli_fetch_assoc($gotattack);
$defense = mysqli_fetch_assoc($gotdefense);

$rankstatsget = "SELECT stats.id, stats.playerrank, users.id, users.username FROM stats, users WHERE         users.username = '$username' AND stats.id = users.id";
$rankstatsgot = mysqli_query($con,$rankstatsget);

$id_get = mysqli_query($con,"SELECT stats.id,users.id, users.username FROM stats, users WHERE     users.username = '$username' AND stats.id = users.id");
$gotid = mysqli_fetch_array($id_get);
$id4 = $gotid["id"]; 

while($row = mysqli_fetch_array($rankstatsgot, MYSQL_ASSOC))
{
echo "Current Rank: {$row['playerrank']}  <br> ".

     "--------------------------------<br>";
} 

if ($attack AND $defense >1 && $attack AND $defense < 1000){
$update_rank = mysqli_query($con,"UPDATE stats SET stats.playerrank = 1 WHERE stats.id=$id4");

}elseif ($attack AND $defense >1001 && $attack AND $defense < 2000){
$update_rank2 = mysqli_query($con,"UPDATE stats SET stats.playerrank = 2 WHERE stats.id=$id4");

}elseif ($attack AND $defense >2001 && $attack AND $defense < 3000){
$update_rank6 = mysqli_query($con,"UPDATE stats SET stats.playerrank = 3 WHERE stats.id=$id4");

}elseif ($attack AND $defense >3001 && $attack AND $defense < 4000){
$update_rank5 = mysqli_query($con,"UPDATE stats SET stats.playerrank = 4 WHERE stats.id=$id4");

}elseif ($attack AND $defense >4001 && $attack AND $defense < 5000){
$update_rank4 = mysqli_query($con,"UPDATE stats SET stats.playerrank = 5 WHERE stats.id=$id4");

}elseif ($attack AND $defense >5001 && $attack AND $defense < 6000){
$update_rank3 = mysqli_query($con,"UPDATE stats SET stats.playerrank = 6 WHERE stats.id=$id4");

}

Echo "Your rank is up to date!";

?>

3 个答案:

答案 0 :(得分:0)

您必须指定大于小于每个变量的值

if ($attack >1 AND $defense >1 && $attack < 1000 AND $defense < 1000){
$update_rank = mysqli_query($con,"UPDATE stats SET stats.playerrank = 1 WHERE stats.id=$id4");

等...

if (($attack >1 OR $defense >1) && ($attack < 1000 OR $defense < 1000)){
$update_rank = mysqli_query($con,"UPDATE stats SET stats.playerrank = 1 WHERE stats.id=$id4");

答案 1 :(得分:0)

您将AND&&混合在一起,没有括号。这两个运算符具有不同的binding precedence,因此您的if()测试无疑与您认为的执行方式不同。

e.g。

$attack AND $defense >1 && $attack AND $defense < 1000

将被解析为

$attack && (($defense > 1) && $attack) && ($defense < 1000)

答案 2 :(得分:0)

发现自己的问题

我需要把

if ($attack['attack'] + $defense['defense'] >1 && $attack['attack'] + $defense['defense'] < 1000){
$update_rank = mysqli_query($con,"UPDATE stats SET stats.playerrank = 1 WHERE stats.id=$id4");

而不是

if ($attack AND $defense >1 && $attack AND $defense < 1000){
$update_rank = mysqli_query($con,"UPDATE stats SET stats.playerrank = 1 WHERE stats.id=$id4");

因为查询没有为IF语句返回正确的答案类型

Noob错误:/