0

I am still new to learning how MySQL works and PhP and I have been trying to get my Array values which is part of a session into a MySQL database but I can't figure out how to or what I am doing wrong. I can't get the query to grab certain values out of the array in the function checkouttodatabase().

I've tried following multiple tutorials and posts regarding getting a session array posted into a MySQL database but to no avail. This seems to "function" but doesn't add the item name to the database but instead puts down "Array" and the values "0"

//Adding the item to the array

if (isset($_POST["add_to_cart"])) {

if (isset($_SESSION["shopping_cart"])) {
    $item_array_id = array_column($_SESSION["shopping_cart"], "item_id");
    if (!in_array($_GET["id"], $item_array_id)) {
        $count = count($_SESSION["shopping_cart"]);
        $item_array = array(

            'item_id' => $_GET["id"],
            'item_name' => $_POST["hidden_name"],
            'item_price' => $_POST["hidden_price"],
            'item_quantity' => $_POST["quantity"]
        );
        array_push($_SESSION['shopping_cart'], $item_array);
    } else {


        echo '<script>alert("Item Already Added")</script>';
        echo '<script>window.location="shoppingcart.php"</script>';
    }
} else {
    $item_array = array(

        'item_id' => $_GET["id"],
        'item_name' => $_POST["hidden_name"],
        'item_price' => $_POST["hidden_price"],
        'item_quantity' => $_POST["quantity"]
    );
    $_SESSION["shopping_cart"][0] = $item_array;
}
}

//Function to grab array values and add it to database

function checkouttodatabase() {

$connect = mysqli_connect("localhost", "root", "", "precisionmice");
mysqli_select_db($connect, 'precisionmice');


foreach($_SESSION['shopping_cart'] as $row => $id){

    $sql="INSERT INTO orders (product, quantity, totalprice)
          VALUES ('item_id','item_quantity','item_price')";
}

    if(mysqli_query($connect, $sql)){
        echo "Betaling bevestigen";
        header("refresh:2; url=checkout.php");
    } else {
        echo "Update mislukt";
    }
}

The function checkouttodatabase() is called when the player is redirected to a seperate webpage. Now what I would love as a result is so let's say there are two items added to the shoppingcart array. For each item added I would like it to send the variables to the MYSQL database. Except I also struggle for the total price. I would like to do it so that the quantity is multiplied by the individual item_price which is added in the MYSQL column total_price.

For example: Database results when submitted:

Item_id: Productname 1 , Item_quantity: 3, Total_price: 45.00$

Item_id: Productname 2, Item_quantity: 1, Total_price: 15.00$

2
  • 1
    You're inserting static values like 'item_id' as a string. Instead define that as ?,?,? and use bind_param to set the values. Commented Jan 28, 2019 at 16:16
  • Why do you build the same string $sql each and every time, but only insert the last occurence into the database? And why don't you use variables within that query? Commented Jan 28, 2019 at 16:31

1 Answer 1

0

There are a few things to talk about in this code example, but to address the problem of getting an array into a database, @tadman gave you the answer in the comment above. You can't treat an array as though it were a string when composing SQL statements; you need to work with individual elements of the array instead.

If you really wanted to save an entire array into a single field of MySQL database table, you would have to serialize the array somehow, e.g., by using json_encode(). But you normally shouldn't be stuffing multiple values into a single database column without a compelling reason.

Sign up to request clarification or add additional context in comments.

2 Comments

I've followed the page @tadman linked except now I get this error: WARNING: MYSQLI_STMT_BIND_PARAM() EXPECTS PARAMETER 1 TO BE MYSQLI_STMT, BOOLEAN GIVEN IN D:\XAMPP\HTDOCS\PRECISIONMICE\STORE\SHOPCARTFUNCTIONS.PHP ON LINE 107. WARNING: MYSQLI_STMT_EXECUTE() EXPECTS PARAMETER 1 TO BE MYSQLI_STMT, BOOLEAN GIVEN IN D:\XAMPP\HTDOCS\PRECISIONMICE\STORE\SHOPCARTFUNCTIONS.PHP ON LINE 108. Which is the part " mysqli_stmt_bind_param($stmt, $itemname, $itemprice, $itemquantity); mysqli_stmt_execute($stmt); " Sorry if I am doing it wrong, I find MySQL and PhP rather difficult to get working.
My now code: ` function checkouttodatabase() { $connect = mysqli_connect("localhost", "root", "", "precisionmice"); mysqli_select_db($connect, 'precisionmice'); foreach($_SESSION['shopping_cart'] as $row => $id){ $itemname = "item_name"; $itemprice = "item_price"; $itemquantity = "item_quantity"; $stmt = mysqli_prepare($connect, "INSERT INTO orders VALUES (?, ?, ?)"); mysqli_stmt_bind_param($stmt, $itemname, $itemprice, $itemquantity); mysqli_stmt_execute($stmt); } }`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.