Jump to content

[SOLVED] function not completing


samoht

Recommended Posts

<?php
function saveOrder($CPC)
{
    $orderId       = 0;
    $shippingCost  = 5;
    $requiredField = array('hidShippingFirstName', 'hidShippingLastName', 'hidShippingAddress1', 'hidShippingCity', 'hidShippingPostalCode',
                           'hidPaymentFirstName', 'hidPaymentLastName', 'hidPaymentAddress1', 'hidPaymentCity', 'hidPaymentPostalCode');
           
    if (checkRequiredPost($requiredField)) {
        extract($_POST);

        // make sure the first character in the
        // customer and city name are properly upper cased
        $hidShippingFirstName = ucwords($hidShippingFirstName);
        $hidShippingLastName  = ucwords($hidShippingLastName);
        $hidPaymentFirstName  = ucwords($hidPaymentFirstName);
        $hidPaymentLastName   = ucwords($hidPaymentLastName);
        $hidShippingCity      = ucwords($hidShippingCity);
        $hidPaymentCity       = ucwords($hidPaymentCity);
        //for clients with accounts.
        $ClientId                    =(int)($hidClientId);
        $ShipAddressId                =(int)($hidShipAddressId);
        $BillAddressId                =(int)($hidBillAddressId);
        $Email                         =($hidEmail);
                
        $cartContent = getCartContent($CPC);
        $numItem     = count($cartContent);
        
        // save order & get order id
        $sql = "INSERT INTO `orders` (
        `ClientId`, `ShipAddressId`, `BillAddressId`, `Email`, `OrderDate`,
        `OrderStatus`, `AssignedTo`, `DateAssigned`, `Location`, `FlagStatus`, `FlagStatus2`, `DateStatus`)
        VALUES (
        '$ClientId', '$ShipAddressId', '$BillAddressId', '$Email', NOW(),
        'New', NULL, NOW(), 'WS', 'N', NULL, NOW())";

        $result = dbQuery($sql) or die('no good order info');
        
        // get the order id
        $orderId = dbInsertId();
        
        if ($orderId) {
            // save order items
            for ($i = 0; $i < $numItem; $i++) {
                $sql = "INSERT INTO orderitems(OrderId, ProductId, OrderQty)
                        VALUES ($orderId, {$cartContent[$i]['ProductId']}, {$cartContent[$i]['qty']})";
                $result = dbQuery($sql);                    
            }
        
            
            // update product stock
            for ($i = 0; $i < $numItem; $i++) {
                $sql = "UPDATE product
                        SET QtyInStock = QtyInStock - {$cartContent[$i]['qty']}
                        WHERE ProductId = {$cartContent[$i]['ProductId']}";
                $result = dbQuery($sql);                    
            }
            
            
            // then remove the ordered items from cart
            for ($i = 0; $i < $numItem; $i++) {
                $sql = "DELETE FROM cart
                        WHERE CartId = {$cartContent[$i]['CartId']}";
                $result = dbQuery($sql);                    
            }                            
        }                    
    }
    
    return $orderId;
}

 

This function used to be working fine  - but now I keep getting a $orderId of 0???

 

but when I insert the sql directly into phpMyAdmin - it updates the table just fine?  - I have also tested each of my variables and they work fine. Any idea where I could be missing something?

 

let me know if you need more code.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.