Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Warning: Undefined array key in php for unknown reason

Hello everyone
I was practicing php arrays and my editor cannot detect an error I have with my php however when it runs I get the error message

Warning: Undefined array key 3 in php

while still printing the output correctly.
my html form is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


    <!DOCTYPE html>
    <html>
        <head><title>Number of Students</title></head>
        <body>
            <form action="Q4.php">
                <table>
                    <tr>
                        <td>Number of Students:</td>
                        <td><input type="text" name="num" size="5"></td>
                        <td><input type="submit" value="Submit"></td>
                    </tr>
                </table>
            </form>
        </body>
    </html>

my php form is:


    <!DOCTYPE html>
    <html>
        <head><title>Number of Students</title></head>
        <body>
            <form action="Q4index.php">
            <table border=1 cellspacing=0 cellpadding=3>
                <tr><th>Student</th><th>Mark</th></tr>
            <?php
            $num = $_GET["num"];
            for($i=1; $i<=$num; $i++){
            echo"<tr><td><input type=text name=stud[] size=7></td>
            <td><input type=text name=mark[] size=5></td></tr>";
            }
            ?>
            <tr><td><input type="submit" value="Submit"></td><td><input type="Reset" value="Reset"</td></tr>
            </table>
            </form>
        </body>
    </html>

and the php program file to run it all is:


    <!DOCTYPE html>
    <html>
        <head><title></title></head>
        <body>
            <h3>The students who passed the exam:</h3>
            <table border="1" cellspacing="0" cellpadding="3">
                <tr><th>Name</th><th>Total Mark</th></tr>
            <?php
            //Declaration of Arrays
            $name=$_GET["stud"];
            $mark=$_GET["mark"];
            //Loop and Conditions
            for($i=0; $i<=count($name); $i++){ 
                if($mark[$i]>=50){
            //Printing        
                echo"<tr><td>$name[$i]</td><td>$mark[$i]</td></tr>";}
            }
            ?>
            </table>
        </body>
    </html>

After entering sample data in the forms and trying to execute the code this error message appears:
Screenshot of error

Can someone please tell me what my code is missing? Thank you in advance and I’m beyond grateful for your patience with my questions 🙂

>Solution :

Remove ‘=’ in condition "$i<=count($name)".
Write "$i < count($name)".

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading