2014-01-07 57 views
1

我不知道查詢無法正常工作的原因,可能是我錯過的一個輕微錯誤。我嘗試將數據插入表列,具體取決於匹配與否。我知道數據在我的數組中,但我懷疑它正在寫出的查詢給我一個錯誤。使用數組將數據插入數據庫

這裏是我的代碼:

$querytwo = 'INSERT INTO `' . $tablename . '` ' . ' (`' . $match_player_in_game . '`) ' . 'VALUES' . '(' . 'yes' . ')'; 

foreach ($player_fromsite as $match_player_in_game) { 

    for ($a = 0; $a < 11; $a++) { 
     if ($match_player_in_game == $home_players[$a]) { 
      // Insert a row of information into the table "example" 
      mysql_query($querytwo) or die(mysql_error()); 

     } else { 

     } 
    } 
} 

該消息被返回'Undefined variable: match_player_in_game'

+0

您正在使用變量_before_定義它。把查詢放在循環中,而不是在 –

+2

之外,你需要把你的$ querytwo放到for()循環裏面,那麼它就可以爲你工作 – frosty11x

+0

@ frosty11x你的評論應該是一個答案 – MonkeyZeus

回答

0

你需要把你的$querytwo的爲()循環內,然後它會爲你工作

+0

我仍然收到一條錯誤消息,說明'字段列表'中的未知列'是'。你認爲這是查詢 – Procode

+0

大部分查詢看起來很好,但我會這樣做:''(''。'''''')';'這可能會爲您解決 – frosty11x

+0

請使用此查詢'$ querytwo =「INSERT INTO \'$ tablename \'(\'$ match_player_in_game \')VALUES('yes')」;' – MonkeyZeus

0

應該

的foreach($ player_fromsite爲$ match_player_in_game){

$querytwo = 'INSERT INTO `' . $tablename . '` ' . ' (`' . $match_player_in_game . '`) ' . 'VALUES' . '(' . 'yes' . ')'; 

    for ($a = 0; $a < 11; $a++) { 
    if ($match_player_in_game == $home_players[$a]) { 
     // Insert a row of information into the table "example" 
     mysql_query($querytwo) or die(mysql_error()); 

    } else { 

    } 
} 

}