Help ! Shopping Cart Not Insert Array

can you help me , insert multi row now work

I var_dump($max); , this render 2 row but them I button submit , saveRows() not insert array , that only one worth taking .

array(2) {

[0]=>

array(2) {

["productid"]=>


string(1) "3"


["qty"]=>


int(1)

}

[1]=>

array(2) {

["productid"]=>


string(1) "1"


["qty"]=>


int(1)

}

}


 

    public function saveRows() {

        $max = count($_SESSION['cart']);

        for ($i = 0; $i < $max; $i++) {

            $orderid = Yii::app()->db->getLastInsertID();

            $detailid = Yii::app()->db->getLastInsertID();

            $product_id = $_SESSION['cart'][$i]['productid'];

            $quantity = $_SESSION['cart'][$i]['qty'];

            $price = $_POST['orderprice'];

            $result = ("insert into order_detail values($detailid,$orderid,$product_id,$quantity,$price)");

        }

        $command = Yii::app()->db->createCommand($result);

        if ($command->execute()) :

            return TRUE;

        endif;

        return FALSE;

    }



phamdoan,

First of all, you should use CDbCommandBuilder or CActiveRecord for inserting rows.

Do not write the insert query yourself as it could be a security issue.

Now, as for your problem. You only get one row inserted because the Insert command execution is outside the for loop. Put it inside the loop and it should work fine.