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:
<!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 🙂
Advertisement
Answer
Remove ‘=’ in condition “$i<=count($name)”. Write “$i < count($name)”.