PHP更新MySQL循环


PHP update MySQL looping

我有以下代码,每当用户"喜欢"比赛(这是一篇帖子)时,它就会用userID、值(喜欢或不喜欢)和提醒日期更新我的数据库。

$query查看我的userContests表并存储它的全部内容。

userContests表存储userID、值(喜欢或不喜欢)、contestID和提醒日期。

$getfreq是一个数组,包含值123234345456567

我希望代码能解释清楚,我已经尽了最大努力为您评论每一部分。这里的基本点是,如果$getfreq包含值345,则插入或更新我的数据库,并使用提醒日期。

$userID = 1;
$value = 1;
$contestID = 1737;
if (($userID > 0) && ($contestID > 0) && ($value < 2)){
    $query = mysql_query("SELECT * FROM userContests WHERE userID='$userID' AND contestID='$contestID';") or die(mysql_error()); 
    $getfreq = mysql_query("
            SELECT wp_term_relationships.term_taxonomy_id 
            FROM wp_term_relationships 
            WHERE wp_term_relationships.object_id = $contestID
        "); 
    while ($row = mysql_fetch_assoc($getfreq)) {
        if ($value == 1){ // If the contest is liked
            if (mysql_num_rows($query) > 0) {  //if a value matching the userID and contest ID already exists in database
                 if ($row[term_taxonomy_id] == 345) { // if Daily entry, update the row with reminder date
                    echo "1";
                    mysql_query("UPDATE userContests SET value='$value', reminder= DATE_ADD(CURDATE(),INTERVAL 1 DAY) WHERE userID='$userID' AND contestID='$contestID';") or die(mysql_error());
                    $frequency = 'Daily';
                } else { // if anything other than above, insert the row with current date
                    echo "2";
                    mysql_query("UPDATE userContests SET value='$value', reminder= CURDATE() WHERE userID='$userID' AND contestID='$contestID';") or die(mysql_error());
                }
            } else { // if there is no previous row in database matching userID and contestID
                if ($row[term_taxonomy_id] == 345) { // if Daily entry, insert the row with reminder date
                    echo "3";
                    mysql_query("INSERT INTO userContests (userID, contestID, value, reminder) VALUES ('$userID', '$contestID', '$value', 'DATE_ADD(CURDATE(),INTERVAL 1 DAY)') ") or die(mysql_error());
                } else { // if anything other than above, insert the row with current date
                    echo "4";
                    mysql_query("INSERT INTO userContests (userID, contestID, value, reminder) VALUES ('$userID', '$contestID', '$value', CURDATE()) ") or die(mysql_error());
                }
            }
        } else if ($value == 0){ // if the value is disliked
            if (mysql_num_rows($query) > 0) {  //if a value matching the userID and contest ID already exists in database, simply update the row without reminder
                echo "5";
                mysql_query("UPDATE userContests SET value='$value' WHERE userID='$userID' AND contestID='$contestID';") or die(mysql_error());
            } else { // if there is no previous row in database matching userID and contestID, simply insert the row without reminder
            echo "6";
                mysql_query("INSERT INTO userContests (userID, contestID, value) VALUES ('$userID', '$contestID', '$value') ") or die(mysql_error());
            }
        }
    }
}

我的代码有效。有点。我的问题是插入代码在我的数据库中插入了5次。我只想插入一次。

代码中的回声用于调试。我正在接收4 4 3 4 4作为回声。

我知道这个问题是因为循环,但我不知道如何解决这个问题。

我推断,它之所以插入代码5次,是因为当代码循环时,它会检查$row是否为345。由于前两次和最后两次都没有,代码按照echo 4插入,而在第三次循环中,由于存在匹配,它按照echo 3插入。

现在,我知道in_array(),但不知道如何在mysql查询的上下文中使用它。我怀疑这可能是解决方案。。。

有人能帮忙吗?

这变得过于令人困惑,但我只是猜测而已。

你想在结果集中搜索"345"条目,如果找到了,就做点什么,如果没有,就做其他事情,对吧?

不管怎样,以下是如何使用array_search();:

$resultSet= mysql_get_assoc($getfreq);
if ( array_search(345,$resultSet) !== false )
{
# - 345 Is here!!! -
//do what you gotta do...

}
else
{
# - 345 is not in $resultSet -
//do something else here
}

我认为循环中没有错误。

您永远不会更新插入SQL中的任何变量。

我的意思是,你说$getfreq中有5个值,第三个值是345,因此带有"3"的分支会发生,在所有其他情况下,带有"4"的分支都会发生。其他变量都没有改变。

你进入这个分支

           if ($row[term_taxonomy_id] == 345) { // if Daily entry, insert the row with reminder date
                echo "3";
                mysql_query("INSERT INTO userContests (userID, contestID, value, reminder) VALUES ('$userID', '$contestID', '$value', 'DATE_ADD(CURDATE(),INTERVAL 1 DAY)') ") or die(mysql_error());
            } else { // if anything other than above, insert the row with current date
                echo "4";
                mysql_query("INSERT INTO userContests (userID, contestID, value, reminder) VALUES ('$userID', '$contestID', '$value', CURDATE()) ") or die(mysql_error());
            }

在第三种情况下,它进入"3"分支并被插入。

然而,在所有其他情况下,该行执行

                mysql_query("INSERT INTO userContests (userID, contestID, value, reminder) VALUES ('$userID', '$contestID', '$value', CURDATE()) ") or die(mysql_error());

因为它在else分支中。

如果你根本不想做循环,只想检查一次,你就必须做这样的事情。

$getfreq = mysql_query("
        SELECT COUNT(taxonomy_id) AS freq
        FROM wp_term_relationships 
        WHERE wp_term_relationships.object_id = $contestID
         AND wp_term_relationshis.taxonomy_id = 345
    ");    
$data = mysql_fetch_assoc($getfreq);
if ($data['freq'] > 1) {
    //do something
} else {
    //do something else
}

我希望我写得正确,因为我是从我的头顶上写的:)

如果你想检查多个值,试试这个:

$getfreq = mysql_query("
        SELECT DISTINCT term_taxonomy_id
        FROM wp_term_relationships 
        WHERE wp_term_relationships.object_id = $contestID
    ");    
while ($row = mysql_fetch_assoc($getfreq)) {
    $taxonomy_id = $row[term_taxonomy_id];
    $seen[$taxonomy_id] = 1;
}
if ($seen[334]) {
   //something
} else if ($seen[456]) {
   //something else
} else {
   //last branch
}