简体   繁体   English

AJAX-> PHP无法持续更新MySQL数据库

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

So this is my early attempt at a Facemash style site in which the user will select one of two images, scoring a hit with the chosen image (the winner) and a miss with the unselected image (the loser) - both of which are recorded in a MySQL database. 因此,这是我在Facemash风格的站点上的早期尝试,在该站点中,用户将选择两个图像之一,对选中的图像(获胜者)和未选中的图像(失败者)进行打分-均记录下来在MySQL数据库中。

The selected image is determined using javascript and uses jquery AJAX to notify a PHP script (backend.php) which updates the database. 选定的图像使用javascript确定,并使用jquery AJAX通知更新数据库的PHP脚本(backend.php)。

This works absolutely correctly for updating the "hits" field. 这对于更新“命中”字段绝对正确。 However, the "misses" are not consistently recorded. 但是,“未命中”不是一致记录的。 By this I mean that when the user clicks one image, the fact the other image has not been clicked is only sometimes shown in the database. 我的意思是,当用户单击一个图像时,未单击另一图像的事实有时仅显示在数据库中。 As far as I can tell there is no pattern as to when the "miss" is and is not recorded, making it difficult to pinpoint where the problem lies. 据我所知,关于“未命中”的记录时间以及没有记录的时间,没有任何模式,因此很难查明问题出在哪里。

I've checked the code over and over again and cannot understand why this is happening or what would be responsible for it, so I thought it would be best to post everything. 我已经一遍又一遍地检查了代码,无法理解为什么会发生这种情况或者是什么原因造成的,所以我认为最好发布所有内容。 I appreciate it's a lot to ask, but any explaination as to why I'm having this problem would be hugely appreciated, thanks. 我很高兴提出很多问题,但是对于我为什么会有这个问题的任何解释,我们将不胜感激,谢谢。

<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: 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);
?>

Thanks again. 再次感谢。

Couple things. 夫妻的事。

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

This doesn't look like it will always guarantee they aren't the same person. 这看起来并不总是可以保证他们不是同一个人。 The result of the second rand() could again return the same value as $personA 第二个rand()的结果可能再次返回与$ personA相同的值

Instead of doing two queries to first select the misses and then increment it, why not make it one query?: 与其先进行两次查询以选择未命中然后再增加未命中,不如不进行一次查询?

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

Lastly, in backend.php, instead of updating winners and losers even if you have only received one of the params, do an if else: 最后,在backend.php中,即使您只收到其中一个参数,也不要更新获胜者和失败者,请执行if否则:

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

I think this will solve your problems. 我认为这可以解决您的问题。

As a matter of optimization, you should also combine your two POSTs into one. 作为优化的问题,您还应该将两个POST合并为一个。

Try changing your two functions to this and seeing if it will work. 尝试将您的两个功能更改为此,然后查看是否可以运行。 (If it doesn't I will just delete my answer.) (如果没有,我将删除我的答案。)

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

This probably doesn't cause the problem, but you should only do one $.post and don't duplicate the same functionality in both click handlers. 这可能不会引起问题,但是您只应执行一个$ .post,并且不要在两个单击处理程序中重复相同的功能。

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