简体   繁体   中英

php MySql global variable

I'm trying to get the last order id and set it in variable so I'll be able to set this id in another Order_Items table

$generatedId=0;
    if(isset($_POST['newOrder'])){
        $costumerID = $_POST['CostumerID'];
        $orderDateID =   $_POST['orderDateID'];
        $deliveryDateID =   $_POST['deliveryDateID'];
        $orderRemeraks =   $_POST['orderRemeraks'];
        $orderType =   $_POST['orderType'];

        echo $costumerID;
        $query = "INSERT INTO `orders` (`costumerName`,`dateOfOrder`,`dateOfDelivery`,`remarks`,`type`,`status`)
                VALUES ('$costumerID','$orderDateID','$deliveryDateID','$orderRemeraks','$orderType', 'open');";
        $insertRow = $mysqli->query($query) or die ($mysqli->error.__LINE__);
        $generatedId = $mysqli->insert_id; 
        if($insertRow){
             $GLOBALS['generatedId'] = $mysqli->insert_id; 
            $msg = 'New Order was added ';          
            echo  $msg;
            echo    $generatedId;   
        }

    }

    if(isset($_POST['newItem'])){
        $item_Number = $_POST['item_Number'];
        $itemQty = $_POST['itemQty'];
        $itemKg = $_POST['itemKg'];
        $itemRemarks = $_POST['itemRemarks'];   
        echo   $generatedId;    
        $query = "INSERT INTO `item_number` (`order_id`, `item_number`, `quantity`,`quantity_kg`,`  remarks`,`filling_status`)
                VALUES('$generatedId', '$item_Number','$itemQty','$itemKg','$itemRemarks','not',);";
                $insertRow = $mysqli->query($query) or die ($mysqli->error.__LINE__);

        if($insertRow){ 
            echo  'New Item was added ' ;
        }
    }

The problem is the $genertedId is printed with the initial 0 and there for I can't put it in the order_items table, but in the first time(right after echo $msg , when printing it it's returning the true value;

thank you

$generatedId = mysqli_insert_id($mysqli); 
if($insertRow){
    $GLOBALS['generatedId'] = $generatedId; 
    $msg = 'New Order was added ';
    echo  $msg;
    echo    $generatedId;   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM