因此,這是我在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);
?>
感謝。
)謝謝,這與mofle的答案結合起來似乎已經完成了(儘管我認爲查詢的改變做了它),但是我沒有使用if語句,點擊和未命中需要更新。再次感謝所有幫助。:) – Josh 2011-04-20 01:29:27
哎呀,好的。它看起來像是因爲你在單獨的請求中發送參數贏者和輸者,你永遠不會同時運行updateHits和updateMisses。 – VNO 2011-04-20 01:36:09
@Josh,奇怪的是他的查詢和我的查詢完全一樣,你說我的工作沒有成功。但Vibhu對他的所有評論都是正確的。 – Flipper 2011-04-20 02:05:16