lastInsertId = 0

时间:2017-08-09 12:08:48

标签: php mysql pdo

我试图插入我的数据库工作正常,但LastInsertId总是出现0,任何想法?

$stmt = $db->prepare('INSERT INTO players_results (matchweek_id, matchweek, game_id, username, homeresult, awayresult, goals, submit, matchweek_session) Values (:matchweekid, :matchweek, :gameid, :username, :homeresult, :awayresult, :goals, :submit, :matchweeksession)');

          for ($gi = 1; $gi <= 10; $gi++) {
            $stmt->execute(array(
            ':matchweekid' => '1',
            ':matchweek' => 'Matchweek 1',
            ':gameid' => $gi,
            ':username' => $_POST['username'],
            ':homeresult' => NULL,
            ':awayresult' => NULL,
            ':goals' => NULL,
            ':submit' => 'n',
            ':matchweeksession' => 'open'   
          ));
          $id = $db->lastInsertId('member_id');
        }

1 个答案:

答案 0 :(得分:3)

//更改

$id = $db->lastInsertId();

$id = $stmt->lastInsertId();