mysql db insert id没有给出正确的值

时间:2013-09-06 16:07:59

标签: php mysqli insert-id

我有一个脚本试图获取我刚刚插入的自动增量字段的insert_id。

            $db = new mysqli("localhost", "user", "pass", "db");    

            for($xx=0;$xx<$quantity;$xx++){
                //quantity is the total number of tickets bought
            /* check connection */
            if (mysqli_connect_errno()) {
                printf("Connect failed: %s\n", mysqli_connect_error());
                //exit();
            }

            $payer_email = $db->real_escape_string($payer_email);
            $name = $db->real_escape_string($name);

            $query = "INSERT INTO `ticketsConference2013`  VALUES (NULL, '".$payer_email."', '".$name."', null)";
            $db->query($query);
            $ticketIdIs=""; 
            $ticketIdIs = $db->insert_id;

                $html.='<p style="text-align:center;line-height:100%">Ticket number'.$ticketIdIs.'</p>';

            $textIs = $name." ".($xx+1)." of ".$quantity;

            $pdf->writeHTML($html, true, false, true, false, '');

            $pdf->Cell(0, 0, 'CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9', 0, 1);
            $pdf->write1DBarcode($textIs, 'C39', '', '', '', 18, 0.4, $style, 'N');

            }
            $db->close();

它可以很好地插入到数据库中,但每次运行时,insert id只会给出初始插入号。它不反映数据库中的实际插入号。

我已经阅读了一些关于这一点,但尽管我没有能够使它工作

1 个答案:

答案 0 :(得分:0)

试试这个:

  $query = "INSERT INTO `ticketsConference2013`  VALUES (NULL, '".$payer_email."', '".$name."', null)";
               if( $db->query($query)){
                $ticketIdIs=""; 
                $ticketIdIs = $db->insert_id;
}