keep quantity after refresh

Asked

Viewed 53 times

0

So I add the product:

 if (isset($_GET['acao'])) {
    if ($_GET['acao'] == 'add') {
    $id = intval($_GET['id']);
  if (!empty($_SESSION['shop'][$id]))
    //if (!isset($_SESSION['shop'][$id]))
        $_SESSION['shop'][$id] = 1;
    } else {
        $_SESSION['shop'][$id] += 1;
    }  e aqui o input :   echo'<td><input rel="'.$linha['id'].'" type="number" step="1" min="1" style="width:50px; font-family: Tahoma; font-size: 20px;" name="prod[' . $id . ']" value="' . $qtd . '"></td>';

I wonder how I can keep up the amount after a refresh. For example: if I choose 2 products, when refreshing the page, returns the quantity of 1 product.

1 answer

0

Just simple error of logic

Suggestion for correction

 if (
     isset($_GET['acao'])
     && $_GET['acao'] == 'add'
 ) {
     $id = intval($_GET['id']);
     if (
         isset($_SESSION['shop'][$id])
         || empty($_SESSION['shop'][$id])
     ) {
         $_SESSION['shop'][$id] = 1;
     } else {
         $_SESSION['shop'][$id] += 1;
     }
 }

Note: Be aware that in a page refresh/Reload, values will be constantly incremented.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.