-1

I've a script that tries to get the insert_id for the autoincrement field I've just inserted.

            $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();

It inserts fine into the database but each time it's run the insert id only gives the initial insert number. It doesn't reflect the actual insert number in the database.

I've read a bit about this but despite fidling I can't make it work

user1616338
  • 557
  • 1
  • 8
  • 24

1 Answers1

0

Try with this one:

  $query = "INSERT INTO `ticketsConference2013`  VALUES (NULL, '".$payer_email."', '".$name."', null)";
               if( $db->query($query)){
                $ticketIdIs=""; 
                $ticketIdIs = $db->insert_id;
}
Suvash sarker
  • 3,140
  • 1
  • 18
  • 21