PHP get form and use array store crazy hint notice:undefined offset

Source: Internet
Author: User

1    $answer=Array();2     $answer[0]= ' 0 ';3      for($i= 1;$i<=$QUESTION _count;$i++){ 4         $answer[$i]=$_post[(string)$i];//18th Line of error5         if($i<= ($CHOOSE _count+$FILL _count)){6             if($answer[$i]==$right _answer[$i-1]){7                 $grade=$grade+5;8                 $right _count++;9}Else{Ten                 $wrong _count++; One             } A         } -}// for

Apart, first report the wrong part of the code!

After running the following prompt:

notice:undefined offset:1 in E:\wwwroot\center.php on line 18
notice:undefined Offset:2 in E:\wwwroot\center.php on line 18
notice:undefined Offset:3 in E:\wwwroot\center.php on line 18
notice:undefined Offset:4 in E:\wwwroot\center.php on line 18
notice:undefined Offset:5 in E:\wwwroot\center.php on line 18

Reason:

First appeared on the internet also checked, a lot of people are let you go to modify php.ini, let it not show, I think this is not self-deception (although feasible simple rough), why not to look for reasons to change their own code.

Cause of error: The front-end HTML submission form when the individual input blank to cause error, so that $_post[(string) $i] assigned to the array and to use the array is an undefined phenomenon (in short, the back end does not determine whether the front-end table items are filled, such as radio not selected).

Solve:

First on the code

1      $answer=Array();2       $answer[0]= ' 0 ';3        for($i= 1;$i<=$QUESTION _count;$i++){ 4           if(!isset($_post[(string)$i])){//first decide whether to define5               $answer[$i]= ' 0 ';//manual Assignment if undefined6}Else{//defines the direct assignment of the value7              $answer[$i]=$_post[(string)$i];8           if($i<= ($CHOOSE _count+$FILL _count)){  9               if($answer[$i]==$right _answer[$i-1]){Ten                   $grade=$grade+5; One                   $right _count++;  A}Else{ -                   $wrong _count++; -              } the          } -         } -}// for

Check whether the value from the front end is undefined with isset (), and if it is not defined, it is manually assigned (the case of my Web application needs to be assigned manually), and you can feedback it to the user in a different way.

PHP get form and use array store crazy hint notice:undefined offset

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.