PHP error: Notice: Undefined index:

后端 未结 14 1545

I am working on a shopping cart in PHP and I seem to be getting this error \"Notice: Undefined index:\" in all sorts of places. The error refers to the similar bit of coding

相关标签:
14条回答
  • 2020-12-03 15:40

    it just means that the array, $_POST in this case, doesn't have an element named what is undefined in your error. PHP issues a NOTICE instead of a WARNING of FATAL ERROR.

    You can either log less events via editing php.ini or deal with it by first checking if the items is indeed initialized already by using isset()

    0 讨论(0)
  • 2020-12-03 15:43

    This are just php notice messages,it seems php.ini configurations are not according vtiger standards, you can disable this message by setting error reporting to E_ALL & ~E_NOTICE in php.ini For example error_reporting(E_ALL&~E_NOTICE) and then restart apache to reflect changes.

    0 讨论(0)
  • 2020-12-03 15:44

    apparently, the GET and/or the POST variable(s) do(es) not exist. simply test if "isset". (pseudocode):

    if(isset($_GET['action'];)) {$action = $_GET['action'];} else { RECOVER FROM ERROR CODE }
    
    0 讨论(0)
  • 2020-12-03 15:45

    I did define all the variables that was the first thing I checked. I know it's not required in PHP, but old habits die hard. Then I sanatized the info like this:

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
      if (empty($_POST["name1"])) {
        $name1Err = " First Name is a required field.";
      } else {
          $name1 = test_input($_POST["name1"]);
        // check if name only contains letters and whitespace
          if (!preg_match("/^[a-zA-Z ]*$/",$name1)) {
          $name1Err = "Only letters and white space allowed";
    

    of course test_input is another function that does a trim, strilashes, and htmlspecialchars. I think the input is pretty well sanitized. Not trying to be rude just showing what I did. When it came to the email I also checked to see if it was the proper format. I think the real answer is in the fact that some variables are local and some are global. I have got it working without errors for now so, while I'm extremely busy right now I'll accept shutting off errors as my answer. Don't worry I'll figure it out it's just not vitally important right now!

    0 讨论(0)
  • 2020-12-03 15:49

    You're attempting to access indicies within an array which are not set. This raises a notice.

    Mostly likely you're noticing it now because your code has moved to a server where php.ini has error_reporting set to include E_NOTICE. Either suppress notices by setting error_reporting to E_ALL & ~E_NOTICE (not recommended), or verify that the index exists before you attempt to access it:

    $month = array_key_exists('month', $_POST) ? $_POST['month'] : null;
    
    0 讨论(0)
  • 2020-12-03 15:50

    Obviously $_POST['month'] is not set. Maybe there's a mistake in your HTML form definition, or maybe something else is causing this. Whatever the cause, you should always check if a variable exists before using it, so

    if(isset($_POST['month'])) {
       $month = $_POST['month'];
    } else {
       //month is not set, do something about it, raise an error, throw an exception, orwahtever
    }
    
    0 讨论(0)
提交回复
热议问题