2013-04-10 59 views
0

我正在使用foreach循環來檢查並插入新標籤。爲什麼這個if else條件無法正常工作?

但是兩個條件(if和alse)同時應用,而不管mysql發現的id是否等於foreach發佈的ID。 PLZ幫助

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

     } 
    } 
} 
+0

我不認爲if/else的兩個部分都實際執行。嘗試僅在控制檯而不是查詢中打印某些內容,這很可能是if/else內部代碼中的錯誤。 – sashkello 2013-04-10 03:21:05

+0

嘗試在檢查前打印$ id $ fnew_tags值 – PSR 2013-04-10 03:22:21

回答

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))

現在有記錄你的當執行

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 
相關問題