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.

    $warenkorb = $_SESSION['warenkorb'];

    $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();
    $statement = $pdo->prepare("UPDATE warenkorb SET array = ? WHERE id = ?");
$statement->execute(array($array, $id));


1 Answers

Daan Oostindiƫn On

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


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.