PHP更新MySQL循环

PHP更新MySQL循环,php,mysql,arrays,loops,insert,Php,Mysql,Arrays,Loops,Insert,我有下面的代码,每当用户“喜欢”一场比赛(这是一篇文章)时,它都会用userID、value(喜欢或不喜欢)和提醒日期更新我的数据库 $query查看我的usercontents表并存储其全部内容 UserConferences表存储用户ID、值(喜欢或不喜欢)、ConferenceID和提醒日期 $userID = 1; $value = 1; $contestID = 1737; if (($userID > 0) && ($contestID > 0) &

我有下面的代码,每当用户“喜欢”一场比赛(这是一篇文章)时,它都会用userID、value(喜欢或不喜欢)和提醒日期更新我的数据库

$query查看我的usercontents表并存储其全部内容

UserConferences表存储用户ID、值(喜欢或不喜欢)、ConferenceID和提醒日期

$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());
            }
        }
    }
}
$getfreq是一个数组,包含值123、234、345、456、567

我希望代码能够自我解释,我已经尽了最大努力为您评论了每个部分。这里的基本要点是,如果$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());
            }
        }
    }
}
$userID=1;
$value=1;
$ID=1737;
如果($userID>0)和($contestID>0)和($value<2)){
$query=mysql\u query(“从userID='$userID'和contestID='$contestID';”的userContests中选择*)或die(mysql\u error());
$getfreq=mysql\u查询(“
选择wp\术语\关系。术语\分类法\ id
从wp_术语_关系
其中wp_term_relationships.object_id=$contesticId
"); 
while($row=mysql\u fetch\u assoc($getfreq)){
如果($value==1){//如果喜欢比赛
if(mysql_num_rows($query)>0){//如果数据库中已经存在与userID和contest ID匹配的值
如果($row[term\u taxonomy\u id]==345){//if Daily entry,则使用提醒日期更新该行
回声“1”;
mysql_query(“更新usercontestics SET value='$value',提醒=DATE_ADD(CURDATE(),INTERVAL 1天),其中userID='$userID'和contesticsid='$contesticsid';”)或die(mysql_error());
$frequency=‘每日’;
}else{//如果不是上述内容,请插入包含当前日期的行
回声“2”;
mysql_query(“updateusercontests SET value='$value',rements=CURDATE(),其中userID='$userID'和contestID='$contestID';”)或die(mysql_error());
}
}else{//如果数据库中没有匹配userID和contesticId的前一行
如果($row[term\u taxonomy\u id]==345){//if Daily entry,则插入带有提醒日期的行
呼应“3”;
mysql_query(“插入到userContests(userID、contestID、value、提醒)值(“$userID”、“$contestID”、“$value”、“DATE_ADD(CURDATE(),INTERVAL 1 DAY)”)或die(mysql_error());
}else{//如果不是上述内容,请插入包含当前日期的行
呼应“4”;
mysql_query(“插入到userContests(userID、contestID、value、提醒)值('$userID','$contestID','$value',CURDATE()))或die(mysql_error());
}
}
}else if($value==0){//如果不喜欢该值
如果(mysql_num_rows($query)>0){//如果数据库中已经存在与userID和contest ID匹配的值,只需更新该行而无需提醒
回声“5”;
mysql_query(“updateusercontests SET value='$value',其中userID='$userID'和contestID='$contestID';”)或die(mysql_error());
}else{//如果数据库中没有与userID和contesticId匹配的前一行,只需插入该行,而无需提醒
回声“6”;
mysql_query(“插入到userContests(userID,contestID,value)值('$userID','$contestID','$value'))中)或die(mysql_error());
}
}
}
}
我的代码有效。某种程度上。我的问题是插入代码在我的数据库中插入了5次。我只希望它插入一次

代码中的回音用于调试。我收到了4个回音

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

我推断它插入代码5次的原因是因为代码循环时,它会检查$row=345。由于前两次和后两次不匹配,代码按照echo 4插入,而在第三个循环中,由于存在匹配,代码按照echo 3插入

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


有人能帮忙吗?

我想你的循环中没有错误

您只是从不更新插入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
}

这变得过于混乱了,但我猜得很准

你想在结果集中搜索“345”条目,如果找到了,做些什么,如果没有,做些别的,对吗

不管是哪种方式,下面介绍如何使用
array_search()


您需要调试代码:在每次插入之前使用echo,以了解执行了哪些查询、执行了多少次、顺序……请参阅我修改的问题。我收到4的输出。您的$值永远不会更改,您将其设置为1,并且始终保持为1。在本例中,$值设置为1。$值实际上是在用户单击按钮时设置的$value=1表示该帖子很受欢迎。请稍候。处理
$getfreq
查询时选择了多少行?对它执行一个
mysql\u num\u rows()
,以找到正确的答案。我想知道