我检查过我的代码,与我网站上其他代码的代码相比,我看不到任何不一致,但由于某些原因,记录正在使用空白数据输入数据库,下面是我的代码。将空白数据插入数据库的字段
<?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
如果我还没有提供充分的资料,请让我知道。
欢迎来到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
我没有看到您的插入语句($ cat_id,$ sub_cat_name,$ sub_desc)中的变量是从POST数据设置的。 – bitfiddler
不仅您的代码使用了不推荐使用的'mysql_ *'函数,而且通过直接将变量直接包含到查询中而无需任何形式的转义,就极易受到SQL注入攻击。 – jcsanyi