【问题标题】:mysql query returns false, mysql insert works but echo statement doesn't show upmysql 查询返回 false,mysql insert 有效,但 echo 语句不显示
【发布时间】:2017-12-07 10:08:04
【问题描述】:

我在做一个文字类的网络游戏,在我的游戏里,有一个战斗系统。击败敌人后,如果您还没有徽章,您可以获得徽章。但是,由于一些我无法理解的奇怪原因,徽章按原样添加到数据库中(如果徽章的表行在 MySQL 数据库中不存在),但是应该伴随获得的文本徽章有时会出现。我会说回声显示 50% 或更少的时间。这是我的代码:

$gettrainerbadge = mysqli_query($conn, "SELECT * FROM t_badges where 
badge='$badge' AND trainer='$getuser3[username]'");
$gettrainerbadge2 = mysqli_fetch_assoc($gettrainerbadge);


if(!$gettrainerbadge2)
{
    //This echo shows up maybe 50% of the time the form is submitted.

    echo "<font face='verdana' size='2'><br><br><br><img src='images/badges/$get_gym_leader3[badge].png' style='padding: 10px;'><br>By defeating $getuser3[battle_trainer] you have also earned a(n) $get_gym_leader3[badge] Badge!";


    //This INSERT works as expected when the badge doesn't exist it adds the badge to the database if the badge already exists it does nothing

    $addbadge = "INSERT INTO t_badges (`trainer`, `badge`) VALUES ('$getuser3[username]', '$get_gym_leader3[badge]')"; 
    mysqli_query($conn, $addbadge);

}

正如我所说的 echo 语句有一半的时间都有效,它应该每次都有效,我无法弄清楚为什么要添加徽章,但伴随它的文本没有显示。任何帮助表示赞赏。


在玩了一些代码之后,我注意到有时我可以看到带有徽章图像的页面非常快速地闪烁,所以我不认为上面的代码是罪魁祸首。我认为这与我用来在没有用户反馈的情况下自动提交表单的以下脚本有关。我不知道他们为什么会互相干扰,但我几乎可以肯定在某处有一个双重帖子提交。如果有人能帮助指出我在这里可能做错了什么,我将不胜感激。

if($get_remaining_opponents3 > '0')
{

print "<form action='index.php?action=battle' method='POST' 
name='attackselect' id='attackselect' style='margin: 0px;'><td width='100%' 
height='50px' style='background: #e6e6e6; border:2px solid #e6e6e6; border-
radius: 25px;'>

<center>

<font face='verdana' size='2'>

<label style='margin-top: 0px;' id='battle_text'>

<input type='radio' name='continue' value='continue' checked>

<i>Resuming battle ... (<span id='continue'> </span>)</i>
</input>
</label>
</form>
";
?>


<script>

var milisec= 0;  
var seconds=4;      

document.getElementById("continue").innerHTML ='4';   
continue_battle() ; 


function continue_battle(){ 
if (milisec<=0){ 
milisec=9; 
seconds-=1; 
} 
if (seconds<=-1){ 
milisec=0; 
seconds+=1; 
} 
else
{ 
milisec-=1; 
document.getElementById("continue").innerHTML =seconds;
setTimeout("continue_battle()",100); 
}
if(document.getElementById("continue").innerHTML == '0')
{
document.getElementById("attackselect").submit();
}
}
</script>

<?php
print "</form>";
}
else
{

print "<form action='index.php?action=battle' method='POST' name='victory' 
id='victorious' style='margin: 0px;'><td width='100%' height='50px' 
style='background: #e6e6e6; border:2px solid #e6e6e6; border-radius: 25px;'>

<center>

<font face='verdana' size='2'>

<label style='margin-top: 0px;' id='battle_text'>

<input type='radio' name='victory' value='$getuser3[opp_level]' checked>

<i>Resuming battle ... (<span id='victory'> </span>)</i>
</input>
</label>
</form>
";
?>


<script>

var milisec= 0;  
var seconds=4;      

document.getElementById("victory").innerHTML ='4';   
victorious() ; 


function victorious(){ 
if (milisec<=0){ 
milisec=9; 
seconds-=1; 
} 
if (seconds<=-1){ 
milisec=0; 
seconds+=1; 
} 
else
{ 
milisec-=1; 
document.getElementById("victory").innerHTML =seconds;
setTimeout("victorious()",100); 
}
if(document.getElementById("victory").innerHTML == '0')
{
document.getElementById("victorious").submit();
}
}
</script>

<?php
print "</form>";
}

【问题讨论】:

    标签: javascript php mysqli insert echo


    【解决方案1】:
    $gettrainerbadge = mysqli_query($conn, "SELECT * FROM t_badges where badge='".$badge."' AND trainer='".$getuser3['username']."'");
    $gettrainerbadge2 = mysqli_fetch_assoc($gettrainerbadge);
    
    if(count($gettrainerbadge2) == 0)
    {
    
         echo "<font face='verdana' size='2'><br><br><br><img src='images/badges/".$get_gym_leader3['badge'].".png' style='padding: 10px;'><br>By defeating ".$getuser3['battle_trainer']." you have also earned a(n) ".$get_gym_leader3[badge]." Badge!";
    
        //This INSERT works as expected when the badge doesn't exist it adds the badge to the database if the badge already exists it does nothing
    
    
        $addbadge = "INSERT INTO t_badges (`trainer`, `badge`) VALUES ('".$getuser3['username']."', '".$get_gym_leader3['badge']."')"; 
        mysqli_query($conn, $addbadge);
    
    }
    

    尝试使用计数变量,我希望它适用于每个匹配记录。

    【讨论】:

    • count 变量将来会对我有用,但是,它并没有改变这段代码的工作方式。或者说不工作..
    • 请检查每个变量的 echo exit 并检查你得到的输出尝试在数据库中运行查询
    • 我已经用 exit 运行了查询,查询没有错误,就像我说的那样,就寻找徽章表行而言,一切正常,如果它不存在,它会添加徽章到数据库。唯一不能按预期工作的部分是文本输出回显部分。
    • 它几乎就像认为它检查徽章一样,没有找到它,添加它,然后在完全跳过 echo 语句的同时再次重新检查。
    • 现在代码只是检查徽章,如果找到它会显示徽章,如果没有找到它只会插入徽章并只显示文本而不是图像。
    【解决方案2】:

    我用以下代码解决了我的问题。

    <script type="text/javascript">
    
    window.onload=function(){
    window.setTimeout('document.victorious.submit()', 4000)
      }
    </script>
    

    因为我怀疑我的表单提交脚本相互干扰,所以马上解决了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多