简体   繁体   English

mysql查询返回false,mysql insert工作但echo语句不显示

[英]mysql query returns false, mysql insert works but echo statement doesn't show up

I am making a text-based online game, in my game, there is a battle system. 我正在制作基于文本的在线游戏,在我的游戏中,有一个战斗系统。 After defeating the enemy you can be awarded a badge if you do not already have said badge. 击败敌人后,如果您还没有徽章,可以获得徽章。 However, for some strange reason that I can not wrap my head around, the badge is added to the database as it should be (if the table row for the badge does not exist on the MySQL database) but the text that should accompany earning the badge shows up sometimes. 但是,由于一些奇怪的原因,我无法绕过头,徽章被添加到数据库应该是(如果徽章的表行在MySQL数据库中不存在),但应该伴随的文本徽章有时出现。 I would say the echo shows 50% of the time or less. 我会说回声显示50%的时间或更少。 Here is my code: 这是我的代码:

$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);

}

As I've said the echo statement works half the time, it should work every time and I can not figure out why the badge is being added but the text that accompanies it doesn't show. 正如我所说,echo语句的工作时间是一半,它应该每次都有效,我无法弄清楚为什么要添加徽章但是附带的文本没有显示。 Any help is appreciated. 任何帮助表示赞赏。


After playing with the code some more I've noticed that sometimes I can see a VERY fast flash of the page with the image of the badge, so I don't think the above code is to blame. 在更多地使用代码后,我注意到有时候我可以看到带有徽章图像的页面非常快速的闪光,所以我不认为上面的代码是罪魁祸首。 I think it has to do with the following scripts I am using to submit the forms automatically without user feedback. 我认为这与我用于在没有用户反馈的情况下自动提交表单的以下脚本有关。 I don't know why they would interfere with each other, but I'm almost certain that there is a double post submission going on somewhere. 我不知道为什么他们会互相干扰,但我几乎可以肯定在某个地方有一个双重帖子提交。 If anyone could help point out what I might be doing wrong here I would appreciate it. 如果有人可以帮助指出我在这里做错了什么我会很感激。

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>";
}
$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);

}

Try using count variable i hope it should work for every match records. 尝试使用count变量我希望它应该适用于每个匹配记录。

I fixed my issues with the following code. 我用以下代码修复了我的问题。

<script type="text/javascript">

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

As I suspected my form submit scripts were interfering with each other, this fixed it right up. 因为我怀疑我的表单提交脚本互相干扰,所以它修正了它。

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

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