www.webdeveloper.com
Results 1 to 3 of 3

Thread: PHP and MySQL forum

  1. #1
    Join Date
    Oct 2013
    Posts
    26

    Question PHP and MySQL forum

    I am building a forum that displays all of the titles and dates of the topics in 1 page, then after clicking on the name of the post i am trying to send the user to a page that displays the content of the thread, currently i am just using the body text to test, it is pulling correct information from the database but after it runs the query in the second page it always keeps that first result, even after backing up and loading the page again. I'm not sure what is causing this to pull the same results every time, here is my code.

    Here is the forum.
    PHP Code:

    require_once '../header.php';

    if (!
    $loggedin) die("You must be logged in to view the Forum.");
    $user $_SESSION['user'];


    if (isset(
    $_POST['submit'])
    ) {
        if (!
    $result mysqli_fetch_row($db->query("SELECT * FROM users where user_name='$user'"))) 
        echo 
    "failed";

        

        
    $topic_subject $db->real_escape_string($_POST['topic_subject']);
        
    $topic_date date("Y-m-d H:i:s");
        
        
    $topic_cat $db->real_escape_string($_POST['topic_category']);
        
    $query "SELECT * FROM users where user_name='$user'";
        
        if (!
    $result mysqli_fetch_row($db->query($query))) {
            echo 
    "QUERY failed: $query<br />" mysql_error() . "<br /><br />";
        } else {
            
    $user_id $result[0];
        
        
        }
        
        
    $content $db->real_escape_string($_POST['body']);

        
    $image '';

        
         
    $query "INSERT INTO forum_topics (topic_subject, topic_date, topic_cat, topic_by, content) values ('$topic_subject', '$topic_date', '$topic_cat', '$user_id', '$content')";

        if (!
    $result $db->query($query)) {
            echo 
    "INSERT failed: $query<br />" mysqli_error($db) . "<br /><br />";
        } else {
                    echo 
    "<script>window.location = 'http://localhost:56819/forum/forum.php'</script>";
            echo 
    "Thank you for sharing, <a href='forum.php'>Click Here</a> to refresh the page.";
        }

    } else {



        echo 
    "<div id='ForumWindow'>";
        echo <<<_END
    <form action="/forum/forum.php" method="post">
    <div class="toc">
    <ul class="menu" style="border: 1px solid black;">
        <a href="javascript:Tog('block')"><li class="topicHeading">Create New Topic</li></a>
        <div id="expandable" style="overflow:hidden;display:none;">
        <ul class="drop">
            <li><input type="text" name="topic_subject" value="Topic Subject" /></li>
            <li class="radio"><input type="radio" name="topic_category" value="Perenials" /></li>Perenials
            <li class="radio"><input type="radio" name="topic_category" value="Vegetables" /></li>Vegetables
            <li class="radio"><input type="radio" name="topic_category" value="Annuals" /></li>Annuals
            <li><input type="textarea" name="body" value="Write a comment here." col="30" row="5" /></li>
            <li><input type="submit" name="submit" value="Post Topic" /></li></ul>
        <a href="javascript:Tog('none')">Less</a>
        </div>

    _END;


        
    $query "SELECT * FROM forum_topics WHERE topic_cat='Annuals'";

        
    $result $db->query($query);

           echo 
    "<div class='categories' id='Annuals'>";
           echo 
    "<div class='topicHeadings'>Annuals </div>";
        while (
    $row mysqli_fetch_array($result)) {
          
            
    $_SESSION['post'] = $row['topic_ID'];   
            echo 
    "<a href='/forum/viewpost.php'><li class='topicHeading'>" $row['topic_subject'] . " " $row['topic_date'] . "</li></a>";
            echo 
    "<br>";
        
        }
            echo 
    "</div>";

        
    $query "SELECT * FROM forum_topics WHERE topic_cat='vegetables'";

        
    $result $db->query($query);

           echo 
    "<div class='categories' id='Vegetables'>";
           echo 
    "<div class='topicHeadings'>Vegetables </div>";
        while (
    $row mysqli_fetch_array($result)) {
            
    $_SESSION['post'] = $row['topic_ID'];   
            echo 
    "<a href='/forum/viewpost.php'><li class='topicHeading'>" $row['topic_subject'] . " " $row['topic_date'] . "</li></a>";
            echo 
    "<br>";
        
        }
            echo 
    "</div>";


        
    $query "SELECT * FROM forum_topics WHERE topic_cat='Perenials'";

        
    $result $db->query($query);

           echo 
    "<div class='categories' id='Perenials'>";
           echo 
    "<div class='topicHeadings'>Perenials</div>";
        while (
    $row mysqli_fetch_array($result)) {
            
    $_SESSION['post'] = $row['topic_ID'];   
            echo 
    "<a href='/forum/viewpost.php'><li class='topicHeading'>" $row['topic_subject'] . " " $row['topic_date'] . "</li></a>";
        

            echo 
    "<br>";
        }
            echo 
    "</div>";

        
    /* close connection */
        
    $db->close();
        echo 
    "</ul></div>";

        echo 
    "</form></ul>";

        echo 
    "</div>";

    }
    require_once 
    '../footer.php'
    and the page that is supposed to show the body of the post.

    PHP Code:
    require_once "../header.php";
        
    if (!
    $loggedin) die ("You must be logged in to view the forum.");
    $post $_SESSION['post'];
    if (
    $post) {
        
    $query "SELECT * FROM forum_topics WHERE topic_ID='1'";

        if (!
    $result $db->query($query)) die("Query failed." mysqli_error($db));
        else {

        while (
    $row mysqli_fetch_array($result)) {
            echo 
    $row['content'];
        }

        }    
      
    } else die(
    "Your session has timed out please return to the forum to select a post."); 

  2. #2
    Join Date
    Oct 2013
    Posts
    26
    in that second bit of code the topic_ID in the query should say $post instead of 1, i was testing if i could force it to change, which i can.

  3. #3
    Join Date
    Oct 2013
    Posts
    26
    neverming i figured it out.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles