2013-07-05 64 views
0

我在下面的代碼的標題中看到錯誤消息,該頁面用於將子類別添加到我的論壇。列計數與第1行錯誤值計數不匹配

<?php 

include '../includes/connect.php'; 
include '../header.php'; 

echo '<h2>Create a Sub category</h2>'; 
if($_SESSION['signed_in'] == false | $_SESSION['user_level'] != 1) 
{ 
//the user is not an admin 
echo 'Sorry, you do not have sufficient rights to access this page.'; 
} 
else 
{ 
//the user has admin rights 
if($_SERVER['REQUEST_METHOD'] != 'POST') 
{ 
    //the form hasn't been posted yet, display it 
    echo '<form method="post" action=""> 
     Category name: '; 
     $sql = "SELECT cat_id, cat_name, cat_description FROM categories"; 
     $result = mysql_query($sql); 
    echo '<select name="topic_cat">'; 
       while($row = mysql_fetch_assoc($result)) 
       { 
        echo '<option value="' . $row['cat_id'] . '">' . $row['cat_name'] . '</option>'; 
       } 
      echo '</select><br />'; 


    echo 'Sub category name: <input type="text" name="sub_cat_name" /><br /> 
     Sub category description:<br /> <textarea name="sub_desc" /></textarea><br /><br /> 
     <input type="submit" value="Add Sub Category" /> 
    </form>'; 
} 
else 
{ 
    //the form has been posted, so save it 
    $sql = "INSERT INTO subcategories(c_id, sub_cat_name, sub_desc) 
     VALUES('" . $cat_id . ", " . $sub_cat_name . ", " . $sub_desc . "')"; 
    $result = mysql_query($sql) or die (mysql_error()); 
    echo 'The sub category <b>" . $sub_cat_name . "</b> has been added under the main category <b>" . $cat_name . "</b>'; 
    if(!$result) 
    { 
     //something went wrong, display the error 
     echo 'Error' . mysql_error(); 
    } 
    else 
    { 
     echo 'New Sub category succesfully added.'; 
    } 
} 
} 
; ?> 

類別表的結構,像這樣..

  • CAT_ID
  • cat_desc

小類表的結構,像這樣..

  • ID(AI)
  • C_ID
  • sub_cat_name
  • sub_desc

如果我還沒有提供充分的資料,請讓我知道。

+0

您還沒有提供錯誤消息 – zerkms

+0

對不起,錯誤信息在標題中。 第1行的列數不符合值計數 – user2542256

回答

0

你不報的事情正確的位置:

VALUES('" . $cat_id . ", " . $sub_cat_name . ", " . $sub_desc . "')"; 

你需要

VALUES('" . $cat_id . "', '" . $sub_cat_name . "', '" . $sub_desc . "')"; 

注意額外的單引號。

0

您錯過了一些報價。更改

VALUES('" . $cat_id . ", " . $sub_cat_name . ", " . $sub_desc . "')"; 

VALUES('" . $cat_id . "', '" . $sub_cat_name . "', '" . $sub_desc . "')"; 
+0

謝謝!修復它,現在我需要弄清楚爲什麼它將空白數據插入數據庫> _ <感謝您對此問題的幫助。 – user2542256

+0

您似乎沒有將$ _POST變量讀入要放入查詢的變量中。 – 2013-07-05 02:53:01

相關問題