简体   繁体   中英

how to stop items with the same ID being added twice in a session?

A quick questions about sessions, I have a session with the following code

<?php
session_start();
$_SESSION['event_orders'][] = $_POST['event_id'];
?>

problem is once I add an item to the session, it can still re-add it again, therefore duplicating it.

How would one preform a check, something like "if ID exists in session, show (remove) instead of (add) button"?

Use in_array function:

<?php
session_start();

if(!in_array($_POST['event_id'], $_SESSION['event_orders'])) {
    $_SESSION['event_orders'][] = $_POST['event_id'];
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM