“字段列表”中未知列“(值)”

时间:2021-04-27 22:45:03

Ok, maybe I am a fool, but I can't find out whats wrong here. :)

好吧,也许我是个傻瓜,但我不知道这里有什么问题。:)

I get this error

我得到这个错误

Unknown column '(the value user has selected from the dropdown)' in 'field list'

“字段列表”中的“未知列”(值用户已从下拉列表中选择)

Yes, I know that this question has been asked many times before, and I have tried to fix my problem with the old answers, but I can't figure it out. I'm trying to make a posting form for a forum and my error is because of the "Select topic type" dropdown menu.

是的,我知道这个问题以前被问过很多次,我试图用旧的答案来解决我的问题,但我搞不清楚。我正在尝试为一个论坛做一个发布表单,我的错误是由于“选择主题类型”下拉菜单。

 <?php
//create_topic.php
include 'connect.php';
include 'header.php';

echo '<h2>Create a topic</h2>';
if($_SESSION['signed_in'] == false)
{
    //the user is not signed in
    echo 'Sorry, you have to be <a href="/forum/signin.php">signed in</a> to create a topic.';
}
else
{
    //the user is signed in
    if($_SERVER['REQUEST_METHOD'] != 'POST')
    {   
        //the form hasn't been posted yet, display it
        //retrieve the categories from the database for use in the dropdown
        $sql = "SELECT
                    cat_id,
                    cat_name,
                    cat_description
                FROM
                    categories";

        $result = mysql_query($sql);

        if(!$result)
        {
            //the query failed, uh-oh :-(
            echo 'Error while selecting from database. Please try again later.';
        }
        else
        {
            if(mysql_num_rows($result) == 0)
            {
                //there are no categories, so a topic can't be posted
                if($_SESSION['user_level'] == 1)
                {
                    echo 'You have not created categories yet.';
                }
                else
                {
                    echo 'Before you can post a topic, you must wait for an admin to create some categories.';
                }
            }
            else
            {

                echo '<form method="post" action="">
                    Subject: <input type="text" name="topic_subject" /><br />
                    Category:'; 

                echo '<select name="topic_cat">';
                    while($row = mysql_fetch_assoc($result))
                    {
                        echo '<option value="' . $row['cat_id'] . '">' . $row['cat_name'] . '</option>';
                    }
                echo '</select><br />
                <select name="topic_type">
                <option value="Q&A">Q&A</option>
                <option value="Development">Development</option>
                <option value="Rooting and tweeking">Rooting and tweeking</option>
                <option value="Other">Other</option>
                </select><br />';   

                echo 'Message: <br /><textarea name="post_content" /></textarea><br /><br />
                    <input type="submit" value="Create topic" />
                 </form>';
            }
        }
    }
    else
    {
        //start the transaction
        $query  = "BEGIN WORK;";
        $result = mysql_query($query);

        if(!$result)
        {
            //Damn! the query failed, quit
            echo 'An error occured while creating your topic. Please try again later.';
        }
        else
        {

            //the form has been posted, so save it
            //insert the topic into the topics table first, then we'll save the post into the posts table
            $sql = "INSERT INTO 
                        topics(topic_subject,
                               topic_date,
                               topic_cat,
                               topic_by,
                               topic_type)
                   VALUES('" . mysql_real_escape_string($_POST['topic_subject']) . "',
                               NOW(),
                               " . mysql_real_escape_string($_POST['topic_cat']) . ",
                               " . $_SESSION['user_id'] . ",
                               " . mysql_real_escape_string($_POST['topic_type']) . "
                               )";

            $result = mysql_query($sql);
            if(!$result)
            {
                //something went wrong, display the error
                echo 'An error occured while inserting your data. Please try again later.<br /><br />' . mysql_error();
                $sql = "ROLLBACK;";
                $result = mysql_query($sql);
            }
            else
            {
                //the first query worked, now start the second, posts query
                //retrieve the id of the freshly created topic for usage in the posts query
                $topicid = mysql_insert_id();

                $sql = "INSERT INTO
                            posts(post_content,
                                  post_date,
                                  post_topic,
                                  post_by,
                                  post_type)
                        VALUES
                            ('" . mysql_real_escape_string($_POST['post_content']) . "',
                                  NOW(),
                                  " . $topicid . ",
                                  " . $_SESSION['user_id'] . "
                                  " . mysql_real_escape_string($_POST['post_type']) . ",
                            )";
                $result = mysql_query($sql);

                if(!$result)
                {
                    //something went wrong, display the error
                    echo 'An error occured while inserting your post. Please try again later.<br /><br />' . mysql_error();
                    $sql = "ROLLBACK;";
                    $result = mysql_query($sql);
                }
                else
                {
                    $sql = "COMMIT;";
                    $result = mysql_query($sql);

                    //after a lot of work, the query succeeded!
                    echo 'You have succesfully created <a href="topic.php?id='. $topicid . '">your new topic</a>.';
                }
            }
        }
    }
}

include 'footer.php';
?>

Table structure

表结构

posts

的帖子

Kolonne Type Null Standard Kommentarer post_id int(8) Nei
post_content text Nei
post_date datetime Nei
post_topic int(8) Nei
post_by int(8) Nei
post_type varchar(255) Nei

Kolonne类型为空标准Kommentarer post_id int(8) Nei post_content text Nei post_date datetime Nei post_topic int(8) Nei post_type varchar(255) Nei

topics

主题

Kolonne Type Null Standard Linker til Kommentarer topic_id int(8) Nei
topic_subject varchar(255) Nei
topic_date datetime Nei
topic_cat int(8) Nei categories -> cat_id
topic_by int(8) Nei
topic_type varchar(100) Nei

(8) Nei topic_subject varchar(255) Nei topic_date datetime () Nei topic_cat int(8) Nei topic_id topic_id topic_id (8) Nei topic_type varchar(100) Nei

(In Norwegian, but I think you will understand) Thanks :)

(用挪威语,但我想你会理解的)谢谢

1 个解决方案

#1


1  

by the way...

顺便说一下…

topic_type seems to be a string so you should put in quotes in your insert query:

topic_type似乎是一个字符串,所以应该在插入查询中加上引号:

 $sql = "INSERT INTO 
           topics(topic_subject,
                   topic_date,
                   topic_cat,
                   topic_by,
                   topic_type)
          VALUES('" . mysql_real_escape_string($_POST['topic_subject']) . "',
                  NOW(),
                  " . mysql_real_escape_string($_POST['topic_cat']) . ",
                  " . $_SESSION['user_id'] . ",
                  '" . mysql_real_escape_string($_POST['topic_type']) . "'
          )";

Maybe thats the reason for your errror.

也许这就是你犯错的原因。

I saw post_type is a string, too. So do the same in the second insert query!

我看到post_type也是一个字符串。所以在第二个插入查询中也要这样做!

#1


1  

by the way...

顺便说一下…

topic_type seems to be a string so you should put in quotes in your insert query:

topic_type似乎是一个字符串,所以应该在插入查询中加上引号:

 $sql = "INSERT INTO 
           topics(topic_subject,
                   topic_date,
                   topic_cat,
                   topic_by,
                   topic_type)
          VALUES('" . mysql_real_escape_string($_POST['topic_subject']) . "',
                  NOW(),
                  " . mysql_real_escape_string($_POST['topic_cat']) . ",
                  " . $_SESSION['user_id'] . ",
                  '" . mysql_real_escape_string($_POST['topic_type']) . "'
          )";

Maybe thats the reason for your errror.

也许这就是你犯错的原因。

I saw post_type is a string, too. So do the same in the second insert query!

我看到post_type也是一个字符串。所以在第二个插入查询中也要这样做!