未定义的索引通知和未定义的变量通知


Undefined index Notice and Undefined variable Notice

Notice: Undefined index: post_catagory in /Users/darceymckelvey/Desktop/php/includes/classinsert.php on line 9

Notice: Undefined variable: category in /Users/darceymckelvey/Desktop/php/includes/classinsert.php on line 13

代码:

<?php
  require_once('classdb.php');
  if(!class_exists('INSERT')){
    class INSERT {
      public function post($postdata){
        global $db;
        $catagory = serialize($postdata['post_catagory']);
        $query = "
                  INSERT INTO posts(post_title, post_content, post_category)
                  VALUES ('$postdata[post_title]', '$postdata[post_content]', '$category')
              ";
        return $db->insert($query);
      }
    }
  }
  $insert = new INSERT;
?>

问题:

输出的结果是post_titlepost_content工作,但post_category根本没有,留空。

你犯了一个错误,你的代码中存在拼写错误问题,因此你没有得到类别值。

试试这个代码:

<?php
  require_once('classdb.php');
  if(!class_exists('INSERT')){
    class INSERT {
      public function post($postdata){
        global $db;
        $category = serialize($postdata['post_category']);
        $query = "
                  INSERT INTO posts(post_title, post_content, post_category)
                  VALUES ('$postdata['post_title']', '$postdata['post_content']', '$category')
              ";
        return $db->insert($query);
      }
    }
  }
  $insert = new INSERT;
?>