簡體   English   中英

AJAX-> PHP無法持續更新MySQL數據庫

[英]AJAX -> PHP not updating MySQL database consistently

因此,這是我在Facemash風格的站點上的早期嘗試,在該站點中,用戶將選擇兩個圖像之一,對選中的圖像(獲勝者)和未選中的圖像(失敗者)進行打分-均記錄下來在MySQL數據庫中。

選定的圖像使用javascript確定,並使用jquery AJAX通知更新數據庫的PHP腳本(backend.php)。

這對於更新“命中”字段絕對正確。 但是,“未命中”不是一致記錄的。 我的意思是,當用戶單擊一個圖像時,未單擊另一圖像的事實有時僅顯示在數據庫中。 據我所知,關於“未命中”的記錄時間以及沒有記錄的時間,沒有任何模式,因此很難查明問題出在哪里。

我已經一遍又一遍地檢查了代碼,無法理解為什么會發生這種情況或者是什么原因造成的,所以我認為最好發布所有內容。 我很高興提出很多問題,但是對於我為什么會有這個問題的任何解釋,我們將不勝感激,謝謝。

<html>
<head>
<title>Facemash</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.js"></script>
</head>
<body>
<?php
// Make a MySQL Connection
mysql_connect("localhost", "admin", "admin") or die(mysql_error());
mysql_select_db("facemash") or die(mysql_error());

// Select two random people
$personA = rand(1, 28);
$personB = rand(1, 28);

// Ensure that it is not the same person
if ($personB == $personA) {
   $personB = rand(1, 28);
}

// Function to return path of photo
function photoPath ($person){

$query = mysql_query("SELECT photo FROM people WHERE id=$person");
$result  = mysql_fetch_row($query);
$result = $result[0];

echo $result;
}
?>

<!--Image for personA-->
<div id=photoA identity="<?php echo $personA ?>"><img src="<?php photoPath($personA);?>"/></div>

<!--Image for personB-->
<div id=photoB identity="<?php echo $personB ?>"><img src="<?php photoPath($personB);?>"/></div>

<script type="text/javascript">
    $('#photoA').click(function() {
        var hit = $('#photoA[identity]').attr('identity');
        var miss = $('#photoB[identity]').attr('identity');
        $.post ("backend.php", {winner: hit} );
        $.post ("backend.php", {loser: miss} );
        location.reload(true);
    });

    $('#photoB').click(function() {
        var hit = $('#photoB[identity]').attr('identity');
        var miss = $('#photoA[identity]').attr('identity');
        $.post ("backend.php", {winner: hit} );
        $.post ("backend.php", {loser: miss} );
        location.reload(true);
    });
</script>
</body>
</html>

backend.php:

<?php
    // Make a MySQL Connection
    mysql_connect("localhost", "admin", "admin") or die(mysql_error());
    mysql_select_db("facemash") or die(mysql_error());

    // Recieve id of winner from index.php
    $winner = $_POST['winner'];
    // Recieve id of loser from index.php
    $loser = $_POST['loser'];

    // Lookup hits for winner and update by adding 1
    function updateHits ($winner) {
    $query = mysql_query("SELECT hits FROM people WHERE id=$winner");
    $result  = mysql_fetch_row($query);
    $result = $result[0];

    $result++;

    mysql_query("UPDATE people SET hits = $result WHERE id=$winner");
    }

    //Lookup misses for loser and update by adding 1
    function updateMisses ($loser) {
    $query = mysql_query("SELECT misses FROM people WHERE id=$loser");
    $result  = mysql_fetch_row($query);
    $result = $result[0];

    $result++;

    mysql_query("UPDATE people SET misses = $result WHERE id=$loser");
    }

    updateHits($winner);
    updateMisses($loser);
?>

再次感謝。

夫妻的事。

// Select two random people
$personA = rand(1, 28);
$personB = rand(1, 28);

// Ensure that it is not the same person
if ($personB == $personA) {
   $personB = rand(1, 28);
}

這看起來並不總是可以保證他們不是同一個人。 第二個rand()的結果可能再次返回與$ personA相同的值

與其先進行兩次查詢以選擇未命中然后再增加未命中,不如不進行一次查詢?

mysql_query("UPDATE people SET misses = misses + 1 WHERE id=$loser");

最后,在backend.php中,即使您只收到其中一個參數,也不要更新獲勝者和失敗者,請執行if否則:

if($winner) {
  updateHits($winner);
} else if ($loser) {
  updateMisses($loser);
}

我認為這可以解決您的問題。

作為優化的問題,您還應該將兩個POST合並為一個。

嘗試將您的兩個功能更改為此,然后查看是否可以運行。 (如果沒有,我將刪除我的答案。)

    // Lookup hits for winner and update by adding 1
    function updateHits ($winner) {
    mysql_query("UPDATE `people` SET `hits` = hits + 1 WHERE `id`= '$winner'");
    }

    //Lookup misses for loser and update by adding 1
    function updateMisses ($loser) {
    mysql_query("UPDATE `people` SET `misses` = misses + 1 WHERE `id` = '$loser'");
    }

這可能不會引起問題,但是您只應執行一個$ .post,並且不要在兩個單擊處理程序中重復相同的功能。

JS:

$('#photoA, #photoB').click(function() {
    var hit = $('#photoA[identity]').attr('identity'),
        miss = $('#photoB[identity]').attr('identity');
    $.post("backend.php", { winner: hit, loser: miss } );
    location.reload(true);
});

暫無
暫無

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

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