AJAX→PHP不同步更新MySQL数据库


AJAX -> PHP not updating MySQL database consistently

所以这是我在一个Facemash风格的网站上的早期尝试,用户将在两个图像中选择一个,选择的图像命中(赢家)和未选择的图像未命中(输家)-两者都记录在MySQL数据库中。

选择的图像是使用javascript确定的,并使用jquery AJAX通知PHP脚本(backend.php)更新数据库。

对于更新"hits"字段来说,这绝对正确。然而,"失误"并没有被记录下来。我的意思是,当用户单击一个图像时,另一个图像没有被单击的事实只是有时在数据库中显示。据我所知,关于"失误"何时被记录或未被记录,没有任何模式,因此很难确定问题出在哪里。

我一遍又一遍地检查代码,不明白为什么会发生这种情况,也不明白是什么造成的,所以我想最好把所有的东西都贴出来。我知道这是一个很多的问题,但任何解释为什么我有这个问题将非常感激,谢谢。

<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 else:

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

这可能不会导致问题,但您应该只执行一个$。在两个点击处理程序中使用相同的功能,但不要重复。

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