I would like to insert articles in the database which are in the shopping cart, and if one goes back to the article list and comes back to the shopping cart, the shopping cart in the database an update should take place. How can you do that?

I've already interviewed google.

<?php
session_start();
include("database.php");
if(isset($_SESSION['warenkorb'])){
    $warenkorb = $_SESSION['warenkorb'];
}else{

    $warenkorb = array();
}
$array = serialize($warenkorb);
if(!$id = $pdo->lastInsertId()){

$sth = $pdo->prepare("INSERT INTO warenkorb (id, array) VALUES ('', :artikel)");

$sth->execute(array('artikel' => $array));
$id = $pdo->lastInsertId();
}else{
    $statement = $pdo->prepare("UPDATE warenkorb SET array = ? WHERE id = ?");
$statement->execute(array($array, $id));
}

?>

1 Answers

0
Daan Oostindiƫn On

Well. In an ideal world the Shopping Cart would be an object containing references to its contents. ie:

$cart->hasItem($item);

But since you are trying to build it yourself there are two options. You can query the database if the item exist, which is not a terrible thing to do. Or use https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html. The insert-on-duplicate can cause scalability or replication issues in the future.