-5

when I run this code I get Notice: Undefined offset: 3 On line 12 and 18. 12 and 18 with in front.

$_SESSION['basket'][3];

if(isset($_GET['action']) && $_GET['action'] == "add")
{
    if(isset($_GET['product_id']))
    {
        **$_SESSION['basket'][3] += 4;**
    }
    else
    {
        $_SESSION['basket'][3] += 1;

    }
}
Manwal
  • 23,450
  • 12
  • 63
  • 93
kristian
  • 213
  • 1
  • 4
  • 12

2 Answers2

1

Try to initialize value at 3rd index.

$_SESSION['basket'][3] = 0;

Or the better solution is

$_SESSION['basket'][3] = isset($_SESSION['basket'][3]) ? $_SESSION['basket'][3] : 0;

Instead of only

$_SESSION['basket'][3];
Manwal
  • 23,450
  • 12
  • 63
  • 93
-1

Thing is that you are not initialising the session

Try initialise it like this

$_SESSION['baslet'][3] = 0;

now for

$_SESSION['basket'][3] += 4;

try it like this

$_SESSION['basket'][3] = intval($_SESSION['basket'][3]) + 4;

better always convert data into integer type or float type before mathematical opearations

Smruti Singh
  • 565
  • 1
  • 4
  • 14