-1

我在另一个问题中讨论了这个问题,但它偏离了原来的问题,所以我认为最好再提出一个问题。

下面是我的代码,所有内容都正确插入到 c_id 接受的子类别表中,每个条目都作为 0 插入到我的数据库中,这是代码。

<?php
//create_cat.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();

    }
}
}
; ?>
4

1 回答 1

0
$sql = "INSERT INTO subcategories(c_id, sub_cat_name, sub_desc)
       VALUES('" . $_POST['categories.cat_id'] . "', '" . $_POST['sub_cat_name'] . "', '" . $_POST['sub_desc'] . "')";

应该

$sql = "INSERT INTO subcategories(c_id, sub_cat_name, sub_desc)
       VALUES('" . $_POST['topic_cat'] . "', '" . $_POST['sub_cat_name'] . "', '" . $_POST['sub_desc'] . "')";

因为类别 id 的 post 字段是 topic_cat

并尽量避免使用mysql_*函数,因为它们已被弃用。而是使用mysqli_*语句或PDO语句

于 2013-07-05T05:52:24.353 回答