【问题标题】:msqli db insert id not giving correct valuemsqli db insert id 没有给出正确的值
【发布时间】:2013-09-10 19:20:12
【问题描述】:

我有一个脚本试图获取我刚刚插入的自动增量字段的 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();

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

我已经阅读了一些关于此的内容,但尽管摆弄我无法使其工作

【问题讨论】:

    标签: php mysqli insert-id


    【解决方案1】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-31
      • 2020-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-10
      相关资源
      最近更新 更多