0

Problem: Notice: Undefined index: ID in C:\wamp64\www\index1.php on line 14

I have been finding solutions for the problem, so far none could solve my problem. I use mySQL database also, but the only problem I am having is the undefined index and all the variables are named exactly as how it is in the MySQL table. Could someone help be solve this problem. Below is the code

if (isset($_POST['add_to_cart'])) {

    if (isset($_SESSION['cart'])) {

        $session_array_id = array_column($_SESSION['cart'], "ID");

        
        if (!in_array($_GET['ID'], $session_array_id)) {
            $session_array = array(
                'ID' => $_GET['ID'],
                "Name" => $_POST['Name'],
                "Price" => $_POST['Price'],
                "quantity" => $_POST['quantity']
            );

            $_SESSION['cart'][] = $session_array;
        } 

    }else{ 
        $session_array = array(
            'ID' => $_GET['ID'],
            "Name" => $_POST['Name'],
            "Price" => $_POST['Price'],
            "quantity" => $_POST['quantity']
        );

        $_SESSION['cart'][] = $session_array;
    }
}
aynber
  • 20,647
  • 8
  • 49
  • 57
  • The error isn't coming from MySQL (especially since there is no MySQL code here), it's coming from `$_GET['ID']`. That means ID is not in your GET request/URL. Using both `$_POST` and `$_GET` in the same script isn't recommended practice. Try making sure ID is in your form and grab it from `$_POST` instead. – aynber May 23 '22 at 19:45

0 Answers0