使用数组将数据插入数据库

时间:2014-01-07 20:03:00

标签: php mysql sql

我不知道为什么查询不起作用,可能是我错过的一个小错误。我试图将数据插入表列,具体取决于它是否匹配。我知道数据在我的数组中,但我怀疑我写的那个查询给了我一个错误。

这是我的代码:

$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'

2 个答案:

答案 0 :(得分:0)

你需要把你的$querytwo放在for()循环中,然后它会对你有用

答案 1 :(得分:0)

应该是

foreach($ player_fromsite as $ 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 {

    }
}

}