2017-12-07 14 views
2

私はテキストベースのオンラインゲームを作っています。私のゲームでは、バトルシステムがあります。敵を倒した後、バッジをまだ持っていなければバッジを獲得できます。しかし、私の周りを包み込むことができないいくつかの奇妙な理由から、バッジはデータベースに追加されます(バッジのテーブル行がMySQLデータベースに存在しない場合)が、バッジが表示されることがあります。私はエコーが時間の50%以下を示すと言うでしょう。ここに私のコードは次のとおりです。mysqlクエリがfalseを返す、mysqlの挿入が機能するがエコー文が表示されない

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

} 

私はエコー文は半分の時間を動作しますが、それはすべての時間を動作するはずですし、バッジが追加されているが、それに付随するテキストは」doesnの理由を私は理解できないと述べてきたようにtショー。どんな助けもありがとうございます。


いくつかのより多くのコードと一緒に遊ん後、私は時々私はバッジの画像をページの非常に高速なフラッシュを見ることができることに気付きましたので、私は上記のコードは責任があるとは思いません。私は、ユーザーからのフィードバックなしに自動的にフォームを送信するために使用している次のスクリプトと関係があると思います。彼らがお互いに干渉する理由は分かりませんが、二重投稿がどこかで起こっていることはほぼ確実です。誰かが私がここで間違っているかもしれないことを指摘できるなら、私はそれを感謝します。

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

答えて

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

} 

私はそれがすべての試合の記録のために働く必要があります願っていますカウント変数を使用してみてください。

+0

カウント変数は将来私にとって役に立ちますが、このコードの動作方法を変更していません。またはむしろ働いていない.. –

+0

すべての変数でエコー終了を確認して、データベース –

+0

でクエリを実行しようとしたときに出力をチェックし、エラー報告を試みてください。 –

0

私の問題は次のコードで修正されました。

<script type="text/javascript"> 

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

私のフォーム提出スクリプトが互いに干渉していると思われるため、これを修正しました。