我正在制作基于文本的在线游戏,在我的游戏中,有一个战斗系统。击败敌人后,如果您还没有说徽章,则可以授予徽章。但是,出于某种奇怪的原因,我无法将头缠绕,徽章被添加到数据库中(如果徽章的表行不存在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>";
}
$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);
}
尝试使用计数变量,我希望它应该适用于每个匹配记录。
我用以下代码修复了我的问题。
<script type="text/javascript">
window.onload=function(){
window.setTimeout('document.victorious.submit()', 4000)
}
</script>
当我怀疑我的表单提交脚本正在互相干扰时,这将其修复。