0

我正在使用带有 foreach 循环的 if else 条件来检查和插入新标签。

但无论 mysql 找到的 id 是否等于或不等于 foreach 发布的 ID,都同时应用了两个条件(if 和 alse)。请帮忙

$new_tags = $_POST['new_tags'];   //forget the mysl security for the time being   

foreach ($new_tags as $fnew_tags)
{
    $sqlq = mysqli_query($db3->connection, "select * from  o4_tags limit 1");
    while($rowq = mysqli_fetch_array($sqlq)) {
        $id = $rowq['id'];

        if($id == $fnew_tags) {    //if  ID  of the tag is matched then do not insert  the new tags but only add the user refrence to that  ID
            mysqli_query($db3->connection, "insert into user_interests(uid,tag_name,exp_tags) values('$session->userid','$fnew_tags','1')");
        } 
        else 
        {   //if  ID  of the tag is not  matched then  insert  the new tags as well as  add the user refrence to that  ID
            $r = mysqli_query($db3->connection, "insert into o4_tags(tag_name,ug_tags,exp_tags) values('$fnew_tags','1','1')");
            $mid_ne = mysqli_insert_id($db3->connection);
            mysqli_query($db3->connection, "insert into user_interests(uid,tag_name,exp_tags) values('$session->userid','$mid_ne','1')");

        }
    }
}
4

2 回答 2

0

我想你在插入

 $r = mysqli_query($db3->connection, "insert into o4_tags(tag_name,ug_tags,exp_tags)
 values('$fnew_tags','1','1')");$mid_ne = mysqli_insert_id($db3->connection);

然后你正在使用 while($rowq = mysqli_fetch_array($sqlq))

现在有你刚刚插入的记录,因此你的 if 被执行

于 2013-04-10T03:22:19.527 回答
0

我很确定select下面的查询将始终返回相同的记录。

$sqlq = mysqli_query($db3->connection, "select * from  o4_tags limit 1");

我认为大多数时候它会else执行 2 插入。你不应该像下面这样写查询吗?

select * from  o4_tags where id = $fnew_tags limit 1
于 2013-04-10T03:42:26.293 回答