-1

When the form is submitted the following error:

PHP Notice: Undefined offset: 1 in E:\php\learning2\increase.php on line 10

PHP Notice: Undefined index: quantity in E:\php\learning2\increase.php on line 10

Form:

<form action="increase.php" method="post">
    <input type="hidden" name="productId" value="1">
    <input type="number" name="productQuantity" value="1">
    <input type="submit" name="submit" value="Add to basket">
</form>

increase.php

session_start();

if (isset($_POST['submit'])) {
    $productId = $_REQUEST['productId'];

    $productQuantity = $_REQUEST['productQuantity'];

    $_SESSION['cart'][$productId]['quantity'] += $productQuantity;

    header('Location: http://localhost:8000/');
}

How can it be solved?

devpro
  • 16,184
  • 3
  • 27
  • 38

1 Answers1

1

These are notices, which are designed to give you insight into why you're code may not be functioning in the way you intended:

$_SESSION['cart'][$productId]['quantity'] += $productQuantity;

Here: $productId (The number evaluted) is not a part of the array $_SESSION['cart'], and you're trying to treat it like an array. PHP will automatically initialize it as an array, and then set ['quantity'] of that array to $productQuantity. Because PHP is making this assumption (Because you're trying to treat it as an array, and it's not), it will throw a NOTICE Exception.

You can fix it 2 ways. First, you can just disable notice, and assume this is working as intended:

error_reporting(E_ALL & ~E_NOTICE);

or, fix the error that's causing it, by explicitly initializing the array(s):

if ( !isset($_SESSION['cart']) )
{
    $_SESSION['cart'] = array();
}
if ( !isset($_SESSION['cart'][$productId]) )
{
    $_SESSION['cart'][$productId] = array('quantity' => 0);
}
$_SESSION['cart'][$productId]['quantity'] += $productQuantity;
Blue
  • 22,608
  • 7
  • 62
  • 92
  • Thank you very much for your detailed answer and help! I appreciate it. – RuTrash Channel Oct 21 '16 at 15:23
  • This answer will surely help you. @RuTrashChannel. *Go Ahead* – Nana Partykar Oct 21 '16 at 15:24
  • Please don't suggest suppressing notices or warnings. The only **correct** way to deal with them is to _fix_ the problem, not to ignore it. When writing new code, always use `error_reporting=-1` (or `error_reporting=E_ALL|E_STRICT` where `E_STRICT` is not required for PHP 5.4 and up) – Elias Van Ootegem Oct 21 '16 at 15:25
  • @EliasVanOotegem I respectfully disagree. PHP's internal functionality is to initialize it as an array if it's undefined, and you're trying to access an array key. In this case, the functionality he's looking for is what PHP does automatically (albeit slopply). You should always *encourage* fixing code, but to simply not suggest that it's a valid approach would be an incomplete answer. – Blue Oct 21 '16 at 15:30
  • @FrankerZ: It's not about relying on the initialization mechanism. Notices can, and in fact do, significantly slow larger codebases down. Most companies I've worked at, at some point or another, have decided to spend time (and money) to have devs clear out notices from the system. It clutters the logs (making debugging a pain), in turn slowing down development and debugging, and it takes up disk space. [cf this answer](http://stackoverflow.com/a/1869185/1230836) to get an idea of how much impact notices can have – Elias Van Ootegem Oct 21 '16 at 15:37
  • @EliasVanOotegem It appears to be an ancient article. I'm curious to know if PHP 7 has the same performance implications. – Blue Oct 21 '16 at 15:39
  • @FrankerZ: I'd be surprized if the notice/warning implementation was completely overhauled since I last looked at the internals (release PHP7, we're at PHP7.1 now), but there's little difference between php 5.x and 7 WRT notices. The main difference actually is that PHP7 generates *more* notices when you enable strict mode. Arrays still are hashtables, and the lookup mechanisms haven't changed much. I'd even argue that, given the memory optimizations, it's likely to have a bigger impact in certain cases – Elias Van Ootegem Oct 21 '16 at 15:44