簡體   English   中英

mysql查詢返回false,mysql insert工作但echo語句不顯示

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

我正在制作基於文本的在線游戲,在我的游戲中,有一個戰斗系統。 擊敗敵人后,如果您還沒有徽章,可以獲得徽章。 但是,由於一些奇怪的原因,我無法繞過頭,徽章被添加到數據庫應該是(如果徽章的表行在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);

}

嘗試使用count變量我希望它應該適用於每個匹配記錄。

我用以下代碼修復了我的問題。

<script type="text/javascript">

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

因為我懷疑我的表單提交腳本互相干擾,所以它修正了它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM