2013-07-05 121 views
0

我检查过我的代码,与我网站上其他代码的代码相比,我看不到任何不一致,但由于某些原因,记录正在使用空白数据输入数据库,下面是我的代码。将空白数据插入数据库的字段

<?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('" . $_POST['categories.cat_id'] . "', '" . $_POST['sub_cat_name'] . "', '" . $_POST['sub_desc'] . "')"; 
    $result = mysql_query($sql) or die (mysql_error()); 
      echo 'The sub category <b>' . $row['sub_cat_name'] . '</b> has been added under the main category <b>' . $row['cat_name'] . '</b>'; 
    if(!$result) 
    { 
     //something went wrong, display the error 
     echo 'Error' . mysql_error(); 

    } 
} 
} 
; ?> 

类别表的结构,像这样..

  • CAT_ID
  • cat_desc

小类表的结构,像这样..

  • ID(AI)
  • C_ID
  • sub_cat_name
  • sub_desc

如果我还没有提供充分的资料,请让我知道。

+0

欢迎来到StackOverflow。 ** [请不要将mysql_ *函数用于新代码。](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)**不再维护并正式弃用。请参阅** [红盒子](http://php.net/manual/en/function.mysql-connect.php)**?您可以使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/mysqli) - [本页](http://php.net/manual/en /mysqlinfo.api.choosing.php)可以帮助你决定使用哪一个。 – jcsanyi

+2

我没有看到您的插入语句($ cat_id,$ sub_cat_name,$ sub_desc)中的变量是从POST数据设置的。 – bitfiddler

+0

不仅您的代码使用了不推荐使用的'mysql_ *'函数,而且通过直接将变量直接包含到查询中而无需任何形式的转义,就极易受到SQL注入攻击。 – jcsanyi

回答

1

您似乎没有将$ POST变量读入您在查询中使用的变量中。你可能想是这样的:

$sub_cat_name = mysql_real_escape_string($_POST['sub_cat_name']); 
// repeat for other variables. 
+0

+1用于解决sql注入问题。 – jcsanyi

+0

@jcsanyi我知道你会评论,如果我没有! – 2013-07-05 03:00:45

+0

谢谢,添加了来自cat_id的c_id的所有字段。 – user2542256

0

在我看来,那$cat_id$sub_cat_name$sub_desc未在任何地方定义。

而且,你在这里失踪的管道:

if($_SESSION['signed_in'] == false || $_SESSION['user_level'] != 1) 
// --------------------------------^ 

最后,我要指出的是,mysql_*功能已被弃用。你应该真的使用mysqli或PDO

0
if($_SESSION['signed_in'] == false || $_SESSION['user_level'] != 1) 
------------------------------------^ (OR) 

我也看不到你设置变量的位置。('" . $cat_id . "' and etc...)

你应该将它们存储到一个variable像这样:

$cat_id = mysql_real_escape_string($_POST['name_of_the_input']); //and etc.. 

或者在你insert query做到这一点:(根据不同的价值观,你是否需要逃避它像上面)

'".$_POST['name_of_input']."', 
+0

我试过$ _ POST。建议,它会输入除类别表以外的所有字段到子类别表中的所有字段。 $ SQL = “INSERT INTO子类别(C_ID,sub_cat_name,sub_desc) \t \t VALUES('”。$ _POST [ 'CAT_ID']。 “ ''”。$ _POST [ 'sub_cat_name']“。 '' “。$ _POST ['sub_desc']。”')“; \t \t $ result = mysql_query($ sql)或die(mysql_error()); – user2542256

+0

更改这个'echo'';'对此:echo“'”;'如果这样不能解决问题,请尝试将id存储在变量然后做到这一点。 '$ catId = $ row ['cat_id']; $ catName = $ row ['cat_name']; “;'我认为您的主要问题是您的表单中没有该值的猫ID – Chris78

+0

不适用于我:(不知何故,我没有选择类别中的信息表插入到子分类表中似乎我可以添加到子分类表中,但它不会将cat_id中继到子分类表中的c_id中 – user2542256