2011-04-20 18 views
2

因此,這是我在Facemash風格網站的早期嘗試,用戶將在其中選擇兩個圖像中的一個,對所選圖像(獲勝者)進行打擊評分,未經選擇的圖像(失敗者) - 兩者都記錄在MySQL數據庫中。AJAX - > PHP不一直更新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); 
?> 

感謝。

回答

1

夫婦的事情。

// 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); 
} 

這看起來並不像它會始終保證他們是不是同一個人。第二蘭特()的結果可能再次返回相同的值$ PERSONA

而不是做兩個查詢先選擇失誤,然後增加它的,爲什麼不讓它一個查詢?:

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

最後,在backend.php,而不是更新的贏家和輸家,即使你只收到了則params的一個,做一個,如果其他人:

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

我認爲這將解決您的問題。

作爲優化的問題,您還應該將兩個POST組合成一個。

+0

)謝謝,這與mofle的答案結合起來似乎已經完成了(儘管我認爲查詢的改變做了它),但是我沒有使用if語句,點擊和未命中需要更新。再次感謝所有幫助。:) – Josh 2011-04-20 01:29:27

+0

哎呀,好的。它看起來像是因爲你在單獨的請求中發送參數贏者和輸者,你永遠不會同時運行updateHits和updateMisses。 – VNO 2011-04-20 01:36:09

+0

@Josh,奇怪的是他的查詢和我的查詢完全一樣,你說我的工作沒有成功。但Vibhu對他的所有評論都是正確的。 – Flipper 2011-04-20 02:05:16

0

嘗試改變你的兩個功能,看看它是否會工作。 (如果它不,我會刪除我的答案。)

// 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'"); 
    } 
+0

沒有,同樣的問題,很遺憾。在10次命中中,只記錄了6次錯失:( – Josh 2011-04-20 00:38:23

0

這可能不會導致問題,但您應該只執行一個$ .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); 
}); 
+0

這更好。謝謝! – Josh 2011-04-20 01:30:49