如何知道先前插入的sql语句的ID?

问题描述 投票:0回答:1

为什么每次将$ orderid每次增加2?应该如何解决?

从表单插入客户信息

<?php 
session_start();
include 'db_connect.php';

$orderdate = date("Y/m/d");
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$address = $_POST['streetaddress'];
$bldg = $_POST['bldg'];
$floor = $_POST['floor'];
$grandTotal = $_POST['grandtotal'];


$sql="INSERT INTO orders (order_date, client_firstname, client_lastname, client_phone, client_city, client_address, client_bldg, client_floor, grandtotal) 
VALUES  ('$orderdate', '$firstname', '$lastname', '$phone', '$city', '$address', '$bldg', '$floor', '$grandTotal');";
$r=$connect->prepare($sql);
$r->execute();

插入订单商品信息(产品ID,产品价格...)

foreach($_SESSION as $key => $values){

    if(strpos($key, "pid") == true)
    {

        $sql="select * from products LEFT JOIN category on products.category_id = category.category_id where product_id = $_SESSION[$key]";
        $result = mysqli_query($connect, $sql);
            $r = mysqli_fetch_assoc($result);

            $w = "weight";
            $w .= (string) $r['product_id'];
            $weight = $_SESSION[$w];
            $total = $r['product_price']*$weight;

        $orderItemSql ="INSERT INTO order_item (order_id, product_id, product_price, product_weight, total)
       VALUES ('1',''".$r['product_id']."', '".$r['product_price']."', '$weight','$grandTotal')";
        $g=$connect->prepare($orderItemSql);
        $g->execute();


    }

}

$order_id;
if($connect->query($sql) === true) {
   $order_id = $connect->insert_id;
   $valid['order_id'] = $order_id;  
}

$ orderid一次加2,这将如何解决?

和$ orderItemSql无法运行,也许是因为$ orderid?我试图在语句中插入$ orderid一个标准数字,但是它仍然无法正常工作。无法弄清楚您的帮助将不胜感激!谢谢

mysql sql forms session-variables
1个回答
0
投票

此有效

© www.soinside.com 2019 - 2024. All rights reserved.