2016-04-26 96 views
-2

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根本沒有,並留空。

+2

嗯,你的'$ postdata'陣列沒有叫'post_category'的索引。做'print_r($ postdata);'看看你在你的數組中有什麼。 – Rizier123

+2

有一個輸入錯誤:查詢中的'$ category'和變量'$ catagory',請檢查它@Darcey Mckelvey – Nehal

回答

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