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

时间:2016-04-26 07:15:21

标签: php

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完全没有,并且留空。

1 个答案:

答案 0 :(得分:1)

您犯了一个错误,您的代码中存在拼写错误,因为您没有获得类别值。

试试这段代码:

<?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;
?>