【问题标题】:PHP update MySQL loopingPHP更新MySQL循环
【发布时间】:2012-07-20 18:53:49
【问题描述】:

我有以下代码,每当用户“喜欢”比赛(这是一个帖子)时,我都会使用用户 ID、值(喜欢或不喜欢)和提醒日期来更新我的数据库。

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

userContests 表存储用户 ID、值(喜欢或不喜欢)、竞赛 ID 和提醒日期。

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

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

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

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

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

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

谁能帮忙?

【问题讨论】:

  • 你需要调试你的代码:在每次INSERT之前使用echo,以了解执行了哪些查询,执行了多少次,以何种顺序...
  • 查看我修改后的问题。我收到 4 4 3 4 4 的输出。
  • 你的 $value 永远不会改变,你将它设置为 1 并且它始终保持为 1。
  • $value 在本示例中设置为 1。 $value 实际上是在用户单击按钮时设置的。 $value = 1 表示帖子被点赞。
  • 等一下。处理$getfreq 查询时选择了多少行?在上面做一个mysql_num_rows() 以找出答案。

标签: php mysql arrays loops insert


【解决方案1】:

这变得过于混乱,但我正在猜测。

您想在结果集中搜索 '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

}

【讨论】:

  • 很高兴知道xD但是在未来的伙伴中,你应该用更好的术语来解释你的问题。我开始认为这是巨魔发布的那些最终问题之一,只是为了惹恼这里愿意提供帮助的善良用户......
  • 好的。但我确实提到我认为 in_array() 是解决方案,没有人接受它。你的方法更简单,代码行更少。
  • 好吧,你一解释这个问题,我就停止阅读这个问题。太公平了。
  • @navnav:哈哈,我不知道可以将所有结果放到一个数组中并在其中搜索。太棒了。甚至可以学习其他人的问题:)
  • @KarelBílek 不错。不过要小心! array_search() 可以欺骗你。如何?如果它在数组中找到值,它不会返回true,而是返回找到该值的元素的键。所以假设你有这个数组:$arr=array('nav','bob'),你使用array_search()来抓取'nav',它将返回0,因为0 是找到值('nav')的元素的索引键。但是,还有什么== 0? false。因此,如果您执行if(array_search('nav',$arr) == false),则即使找到匹配项,该条件也会成立。 (始终使用 === 和 array_search)
【解决方案2】:

我认为您在循环中没有错误。

您永远不会更新插入 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
}

【讨论】:

  • 没错。我想知道如何避免使用循环。相反,我想查看 $getfreq 内部是否包含 345。如果包含,则执行分支 3。如果它不执行分支 4。
  • 是的,我知道你在说什么。我正在寻求一种方法来完全避免使用循环,而是使用 in_array()。
  • 哦,但这完全改变了情况。等等,我再编辑一次
  • 一个建议:下次你问的时候最好解释一下你真正想要什么。
  • 不要把它看成个人的 :) 但只是从另一边看看你的问题,从一个不知道你的代码是什么以及它想要做什么的人那里.你解释得越多越好。祝你好运
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-28
  • 1970-01-01
相关资源
最近更新 更多