How to enter more than one value into one database column

I am trying to get a completed order to save to a table called orders in a database. In the database, the orders table only shows the last product, but I need it to show all the products on the order.

In the code below ($query2) it grabs all the ids of the products that are in the users cart.(There are two products in the cart)

I then use a while loop to display them, but if I try to INSERT them into the orders table($query3) using the $pro_id variable, it only grabs the last product in the cart and enters it into the database.

How do I get both products to enter into the orders table? Do I need to use an array somehow? Or is there a different way to do this?

Thanks!

        $ip = getIp();
        $query = "SELECT * FROM cart WHERE ip='$ip'";
        $result = mysqli_query($conn, $query); 

        if(mysqli_num_rows($result) > 0){
            while($row = mysqli_fetch_array($result)){
                $id     = $row['id']; 
                $pro_qty    = $row['quantity'];

                $query2 = "SELECT * FROM product WHERE id='$id'";
                $result2 = mysqli_query($conn, $query2);     

                if(mysqli_num_rows($result2) > 0){   
                    while ($row = mysqli_fetch_array($result2)){
                        $pro_id = $row['id'];
                    }
                }
            }
        } 

        if(isset($_POST['placeOrder'])){
            $query3 = "INSERT INTO orders (productId) VALUES ('$pro_id')";
            $result3 = mysqli_query($conn, $query3);
        }

    } 

Answers


I found someone online that showed me a different way to do what I was trying to do. Instead of using an array to save both products to one column; I created two tables in the database ("orders" and "orderproducts"). Using the code below, I made it so that if the "placeOrder" button was clicked it would INSERT the customer's order into the "orders" table (with a unique number that I created and saved in the variable "$orderId").

I then got the id of the products that were in the customers cart and created a while loop that would run through the products in the cart and using "$query3" INSERT each product individually into its own row in the "orderproducts" table with the "orderId='$orderId'" ($orderId is the unique number I created) and "productId='$productId'".

if(isset($_POST['placeOrder'])){
    $query = "INSERT INTO orders (orderId) VALUES ('$orderId')";
    $result = mysqli_query($conn, $query);

    $ip = getIp();
    $query1 = "SELECT * FROM cart WHERE ip='$ip'";
    $result1 = mysqli_query($conn, $query1); 

    if(mysqli_num_rows($result1) > 0){
        while($row = mysqli_fetch_array($result1)){
            $id      = $row['id']; 

            $query2 = "SELECT * FROM product WHERE id='$id'";
            $result2 = mysqli_query($conn, $query2);     

            if(mysqli_num_rows($result2) > 0){   
                while ($row = mysqli_fetch_array($result2)){
                    $productId = $row['id'];

                    $query3 = "INSERT INTO orderproducts (orderId, productId) VALUES ('$orderId', '$productId')";
                    $result3 = mysqli_query($conn, $query3);
                }
            }   
        }
    } 
}

Then to grab that order, I made a query like this:

    $orderNumber = 5578;(This is a fake order number)

    $query = "SELECT * FROM orders WHERE orderId='$orderNumber'"; 
    $result = mysqli_query($conn, $query);

    if($result){
        $query1 = "SELECT * FROM orderproducts WHERE orderId='$orderNumber'"; 
        $result1 = mysqli_query($conn, $query1);
    }

You can modify your variable $pro_id and make it an array type like this: $pro_id = array(); and then use it to store your ids in this array.


Need Your Help

Clang's libc++: using long long in the definition of std::chrono::duration

c++ c++11 clang libc++

In libc++ on 32-bit platforms, int64_t is defined as alias of long long. On 64-bit platforms: long.

Marshalling struct with embedded pointer from C# to unmanaged driver

c# struct pinvoke pointers marshalling

I'm trying to interface C# (.NET Compact Framework 3.5) with a Windows CE 6 R2 stream driver using P/Invoked DeviceIoControl() calls . For one of the IOCTL codes, the driver requires a DeviceIoCon...