undefined variable when displaying data

2019-08-20 08:12发布

问题:

Here is a demo DEMO

In demo, select an assessment, submit it, then when student and question drop down menu appear, click the other submit button. You will see an undefined variable appear. What I am trying to do there is echo the assessment name, data and time from the Assessment drop down menu. But how can this be done?

Below is relevant code:

   function PickSession() 
        {
            //ASSESSMENT DROP DOWN MENU:

            //Get data from database
            $sessionquery = "
                SELECT s.SessionId, SessionName, SessionDate, SessionTime, SessionActive, Complete
                FROM Session s
                INNER JOIN Session_Complete sc ON sc.SessionId = s.SessionId
                WHERE
                (Complete = ?)
                ORDER BY SessionName 
                ";
            $complete = 1;

            global $mysqli;
            $sessionqrystmt=$mysqli->prepare($sessionquery);
            // You only need to call bind_param once
            $sessionqrystmt->bind_param("i",$complete);//it doesn't recognse $userid
            // get result and assign variables (prefix with db)
            $sessionqrystmt->execute(); 
            $sessionqrystmt->bind_result($dbSessionId,$dbSessionName,$dbSessionDate,$dbSessionTime, $dbSessionActive, $dbComplete);
            $sessionqrystmt->store_result();

            ?>

            <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post"> 
                   <p>
                <strong>Asessments:</strong>
                <select name="session" id="sessionsDrop">
                    <option value="">Please Select</option>
                    <?php
                        while ( $sessionqrystmt->fetch() ) {
                            $sv = $dbSessionId;
                            if(isset($_POST["session"]) && $sv == $_POST["session"]) 
                                echo "<option selected='selected' value='$sv'>" . $dbSessionName . " - " . date('d-m-Y',strtotime($dbSessionDate)) . " - " . date('H:i',strtotime($dbSessionTime)) . "</option>" . PHP_EOL;
                            else
                                echo "<option value='$sv'>" . $dbSessionName . " - " . date('d-m-Y',strtotime($dbSessionDate)) . " - " . date('H:i',strtotime($dbSessionTime)) . "</option>" . PHP_EOL;
                        }
                    ?>
                </select>
                </p>
                <input id="sessionSubmit" type="submit" value="Submit Assessments" name="sessionSubmit" />
            </form>
        <?php
        }

    --------------------------------------------------------------------

        function SessionIsSubmitted()
        {
                if(isset($_POST["session"]) && empty($_POST["session"])) // We picked the "Please select" option
                { ?>

                        Please Select an Assessment

                <?php 
                    return false;
                }
            else if(!isset($_POST["session"]))
            {
                return false;
            }
            else // All is ok
            {
                return true;
            }
            return false;

        }

    --------------------------------------------------------------------

        function ShowAssessment()
        {   

        //STUDENT AND QUESTION DROP DOWN MENU GO HERE
        //button - name='answerSubmit' goes here

        }

    --------------------------------------------------------------------

        function StudentAnswersIsSubmitted()
        {

        if(!isset($_POST["answerSubmit"]))
            {
                return false;
            }
            else // All is ok
            {
                return true;
            }
            return false;

        }

    --------------------------------------------------------------------

        function StudentAnswers()
        {

        echo $dbSessionName . " - " . $dbSessionDate . " " . $dbSessionTime;

        }

        ?>

Below is functions structure:

    <?php


            PickSession(); // Show the thing to pick session
            if(SessionIsSubmitted()) // When session is picked
            {
              ShowAssessment(); // Show students and questions information
              if(StudentAnswersIsSubmitted()) // Student Answers button is submitted
                {
                  StudentAnswers();
                }

            }



    ?>

回答1:

you can't print one selected element by putting array variable $dbSessionName. All assessment are getting stored in this array and you want to print the selected assessment by putting the array variable. Try to print only that element of array which has been selected. Do like this:

<select name="session[]" id="sessionsDrop">
if (isset($_POST['Submit']) == 1) {

    $session_name = $_POST['session'];
        echo $session;
    }


标签: php mysqli