简体   繁体   中英

PHP $_SESSION getting PHP Notice: Undefined index

I have the following PHP code which gets data from SQL via PDO:

  $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    $tableName = 'categories';

    ob_start();
    session_start(); 


    //Get values from table
    $sqlprimaryCategory = $dbh->prepare("SELECT * FROM $tableName GROUP BY primary_category");
    $sqlprimaryCategory->execute();
    echo '<form id="form1" action="" method="post">';
    echo '<select name="primary_category" size = "4" onChange="document.getElementById(\'form1\').submit();"> ';
    while ($resultprimary = $sqlprimaryCategory->fetch()) {
        echo '<option value="';
        echo $resultprimary['primary_category'];
        echo '">';
        echo $resultprimary['primary_category'];
        echo '</option>'; 
    }
    echo '</select>'; 
    //echo '<input type="submit" name="primary_category_button" id="primary_category_button" value="Submit">';
    echo '</form>'; 


    if (isset($_POST['primary_category'])) {
        $_SESSION['primary_category'] = $_POST['primary_category'];
        //unset($_SESSION['secondary_category']);
        //unset($_SESSION['tertiary_category']);

    }

    //Get values from table
    $sqlSecondaryCategory = $dbh->prepare("SELECT * FROM $tableName WHERE primary_category = :primary_category GROUP BY secondary_category");
    $sqlSecondaryCategory->execute(array(':primary_category'=>$_SESSION['primary_category']));
    echo '<form id="form2" action="" method="post">';
    echo '<select name="secondary_category" size = "4" onChange="document.getElementById(\'form2\').submit();"> ';
    while ($resultSecondary = $sqlSecondaryCategory->fetch()) {
        echo '<option value="';
        echo $resultSecondary['secondary_category'];
        echo '">';
        echo $resultSecondary['secondary_category'];
        echo '</option>'; 
    }
    echo '</select>'; 
    //echo '<input type="submit" name="secondary_category_button" id="secondary_category_button" value="Submit">';
    echo '</form>'; 


    if (isset($_POST['secondary_category'])) {
        $_SESSION['secondary_category'] = $_POST['secondary_category'];
        //unset($_SESSION['tertiary_category']);

    }


    //Get values from table
    $sqlTertiaryCategory = $dbh->prepare("SELECT * FROM $tableName WHERE secondary_category = :secondary_category GROUP BY tertiary_category");
    $sqlTertiaryCategory->execute(array(':secondary_category'=>$_SESSION['secondary_category']));
    echo '<form id="form3" action="" method="post">';
    echo '<select name="tertiary_category" size = "4" onChange="document.getElementById(\'form3\').submit();"> ';
    while ($resultSecondary = $sqlTertiaryCategory->fetch()) {
        echo '<option value="';
        echo $resultSecondary['tertiary_category'];
        echo '">';
        echo $resultSecondary['tertiary_category'];
        echo '</option>'; 
    }
    echo '</select>'; 
    //echo '<input type="submit" name="tertiary_category_button" id="tertiary_category_button" value="Submit">';
    echo '</form>'; 


    if (isset($_POST['tertiary_category'])) {
        $_SESSION['tertiary_category'] = $_POST['tertiary_category'];
        unset($_SESSION['tertiary_category']);

    }





    if (isset($_POST['primary_category']) OR isset($_POST['secondary_category']) OR isset($_POST['tertiary_category'])) {
        echo $_SESSION['primary_category'];
        echo $_SESSION['secondary_category'];
        echo $_SESSION['tertiary_category'];
    }

The above is a drop-down menu. The second html dropdown shows the content based on the selection of the first selection, while the thrid dropdown shows the content based on the selection of the second dropdown. How can I fix my code to remove the error?

If second category isn't posted it's not in the session , so you won't be able to access it.

You can fix it checking if $_SESSION['secondary_category'] isset before using it in your third request statement

if (isset($_POST['secondary_category'])) {
    $_SESSION['secondary_category'] = $_POST['secondary_category'];
    unset($_SESSION['tertiary_category']);
}elseif (!isset($_SESSION['secondary_category'])){
    $_SESSION['secondary_category'] = null;
}
if (isset($_POST['tertiary_category'])) {
    $_SESSION['tertiary_category'] = $_POST['tertiary_category'];
    //unset($_SESSION['tertiary_category']); < why unset right after assigning? (COMMENT OUT)

}




//Change OR's to AND's because the echo's will only work if all of them are set.
if (isset($_POST['primary_category']) AND isset($_POST['secondary_category']) AND isset($_POST['tertiary_category'])) {
    echo $_SESSION['primary_category'];
    echo $_SESSION['secondary_category'];
    echo $_SESSION['tertiary_category'];
}
// If you want to print out the ones that exist, make separate if statements for each category to see if they exist, or give them a blank value instead of unsetting them.

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