将数据插入到购物车的数据库中

I am doing a shopping cart and I am not sure where or rather which page do I code my INSERT INTO statement.

viewProducts.php

 <?php
            if (isset($_SESSION['cartCity'])) {
                $sql = "SELECT * FROM productsc WHERE id_product IN (";
                foreach ($_SESSION['cartCity'] as $id => $value) {
                    $sql .= $id . ",";
                }
                $sql = substr($sql, 0, -1) . ") ORDER BY id_product ASC";
                $query = mysql_query($sql);
                if (!empty($query)) {
                    while ($row = mysql_fetch_assoc($query)) {
                        ?>
                        <p><?php echo $row['name']; ?><?php echo " x " . $_SESSION['cartCity'][$row['id_product']]['quantity']; ?></p>

                        <?php
                    }
                   } else {
                    echo "<i>You need to add an item to your cart for it to be visible here</i><br />";
                }
            } else {
                echo "<p>Your cart is empty. <br/> Please add some products</p>";
            }
            echo "<a href='viewProductsCity.php?page=cartCity'>Go to Cart</a>";
            echo "<a href='checkout.php'>Checkout</a>";
            ?>

or should i add in cart or viewAdd(this is where the codes for when the customer clicks on add to cart button runs) page?

I would create a separate page to do that.

Then, I would include it just by redirecting the user there or by calling it with AJAX. When user got the cart with something, then yeah, we would redirect him to the checkout, and ask him if he wants to buy now or keep shopping.