表格主题&分类

时间:2018-02-22 20:56:10

标签: php sql database forms forum

我正在尝试创建一个包含类别和主题的论坛。我无法弄清楚如何在创作时将主题纳入某些类别。我不太清楚会给你什么,所以这里是我的文件:

create_topic.php:

<?php
session_start();
//create_cat.php
include 'connect.php';
include 'header.php';

 $topic_subject = $_POST['topic_subject'];
 $category = $_POST['category'];

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="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 = mysqli_query($conn, $sql);

        if(!$result)
        {
            //the query failed, uh-oh :-(
            echo 'Error while selecting from database. Please try again later.';
        }
        else
        {
            if(mysqli_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" />
                Category:<input type="dropdown" name="category" />';

            echo '<select name="topic_cat">';
                while($row = mysql_fetch_assoc($result))
                {
                    echo '<option value="' . $row['cat_id'] . '">' . $row['cat_name'] . '</option>';
                }
            echo '</select>'; 

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

    if(!$result)
    {
        //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_cat,
                           topic_by,
                           topic_date,)
               VALUES('$topic_subject', '$category', '$_SESSION[user_id]')";

        $result = mysqli_query($conn, $sql);
        if(!$result)
        {
            //something went wrong, display the error
            echo 'An error occured while inserting your data. Please contact Olivia Boismier or try again later.';
            $sql = "ROLLBACK;";
            $result = mysqli_query($conn, $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)
                    VALUES
                        ('" . mysql_real_escape_string($_POST['post_content']) . "',
                              NOW(),
                              " . $topicid . ",
                              " . $_SESSION['user_id'] . "
                        )";
            $result = mysql_query($sql);

            if(!$result)
            {
                //something went wrong, display the error
                echo 'An error occured while inserting your post. Please try again later.' . 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 successfully created <a href="topic.php?id='. $topicid . '">your new topic</a>.';
            }
        }
    }
}
}

include 'footer.php';
?>

我已正确连接到我的数据库。我如何从数据库中获取所有类别?

提前致谢!

0 个答案:

没有答案