Build in the Ability for a User to Edit Their Forum Post/s During the Current Login Session

50 views Asked by At

I have this basic forum and would like to enable the users to edit their posts. I am not completely sure how to prest this question but I will try. When a user is logged in they can open a current topic, see all the posts there so far, and reply with their own post. They enter the post and the opened topic page returns with their post included as the last post so far. I will paste in the essential code for the reply page first. This is from "addtopic.php"

    <?php
    session_start();
    if (isset($_SESSION['usr_id']) && !empty($_SESSION['usr_id']) ) {
    } else {
       header('Location: mustlogin.php'); #redirect URL
    }
    ?>
    <?php
    include_once 'dbalt.php';
    doDB();

    //check to see if we're showing the form or adding the post
    if (!$_POST) {
    // showing the form; check for required item in query string
    if (!isset($_GET['post_id'])) {
    header("Location: topiclist.php");
    exit;
    }

    //create safe values for use
    $safe_post_id = mysqli_real_escape_string($mysqli, $_GET['post_id']);

    //still have to verify topic and post
    $verify_sql = "SELECT ft.topic_id, ft.topic_title FROM forum_posts
    AS fp LEFT JOIN forum_topics AS ft ON fp.topic_id =
    ft.topic_id WHERE fp.post_id = '".$safe_post_id."'";

    $verify_res = mysqli_query($mysqli, $verify_sql)
    or die(mysqli_error($mysqli));

    if (mysqli_num_rows($verify_res) < 1) {
    //this post or topic does not exist
    header("Location: topiclist.php");
    exit;
    } else {
    //get the topic id and title
    while($topic_info = mysqli_fetch_array($verify_res)) {
    $topic_id = $topic_info['topic_id'];
    $topic_title = stripslashes($topic_info['topic_title']);
    }
    ?>

    <!doctype html>
    <html><!-- InstanceBegin template="/Templates/index-forum.dwt" codeOutsideHTMLIsLocked="false" -->
    <head>
    </head>

    <body bgcolor="#000000" style="">

<div class="container"><!-- begin container of everything 1200px max width -->

<div class="jumbotron" ETC......

</div><!-- end jumbotron -->

<nav class="navbar navbar-inverse"><!-- begin navbar --> ETC......
</nav><!-- end navbar -->


<!-- InstanceBeginEditable name="EditRegion3" -->
        <div class="title-bar"><n6>Add a Topic to the Forum</n6></div> 
            <div class="main-content">
                <table width="100%" ><!-- holds form content -->
                    <tr colspan="2">
                        <td colspan="2" style="padding-left: 40px; margin-right: 0px;"><!-- contains form table -->
                            <table width="100%" valign="top" style=""><!-- holds the addtopic form -->
                                <tr>
                                <td colspan="2" align="left" style="height: 2em; padding-left: 0px; margin-left: 0px;">&nbsp;<br>&nbsp;<br>&nbsp;<br></td>
                                    <td valign="top" align="left" width="" style="padding: 1em; padding-right: 20px; padding-top: 6em;"><!-- begin form -->
                                        <div>
                                        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p><label for="post_owner"><n5 style="color: #ffe066; font-size: 14pt;">Your Username:</n5></label><br/>
<input type="text" value=<?php echo $_SESSION['usr_id']; ?> id="post_owner" name="post_owner" size="40" maxlength="150" required="required" readonly></p>
<p><label for="post_text"><n5 style="color: #ffe066; font-size: 14pt;">Post Text:</n5></label><br/>
<textarea id="post_text" name="post_text" rows="" cols=""
required="required"></textarea></p>
<input type="hidden" name="topic_id" value="<?php echo $topic_id; ?>">
<button type="submit" name="submit" value="submit"  class="button">Add Post</button>
</form>
                                        </div>
                                    </td><!-- end form -->
                                </tr>
                            </table><!-- holds the addtopic form -->
                         </td><!-- end contains form table -->
                            </tr><!-- end form row-->
                    </table>
                    <div class="bottom-bar" style="border-top-style: solid; border-top-color: #0052cc; border-top-width: 1px; margin-left: 9px; margin-right: 9px; margin-top: 20px;"><n6>&nbsp;</div>
                    <table width="100%" style="padding-left: 20px; margin-right: 0px;"><!-- holds pic and text -->
                        <tr><!-- begin pic and text row-->
                            <td align="right" valign="top" width="50%" height="" style="padding: 1em;">
                                <img src="img/darian-warrior-w300-h443.png" class="img-responsive" width="203" height="300" alt=""/>
                            </td><!-- end image cell-->

                            <td width="50%" align="left" valign="middle" width="" style="padding-right: 1em; padding-left: 1em; padding-top: 1em; padding-bottom: 1em;">
                                <div class="blurb-box" style="width: 75%; text-align: left; padding: 1em; margin-bottom: 6em;"><p><n3 style="color: #ffe066; font-size: 14pt; text-align: left;">This is where you can start a topic. Please create a descriptive title then make your first entry. You can then share a link to your forum topic with others. Open your topic from the topics list and select to share it with social media or copy the URL and paste it into an email, text message, or directly into your social media account.</n3></p></div>
                            </td><!-- end text cell-->
                        </tr><!-- end pic and text row-->
                    </table><!-- end holds pic and text -->
                <table >

                </table><!-- end both tables -->
            </div>
        <!-- InstanceEndEditable -->

This will be from "showtopic.php"

<?php
session_start();
if (isset($_SESSION['usr_id']) && !empty($_SESSION['usr_id']) ) {
} else {
   header('Location: mustlogin.php'); #redirect URL
}
?>
<?php
include_once 'dbalt.php';
doDB();

//check for required info from the query string
if (!isset($_GET['topic_id'])) {
header("Location: topiclist.php");
exit;
}

//create safe values for use
$safe_topic_id = mysqli_real_escape_string($mysqli, $_GET['topic_id']);

//verify the topic exists
$verify_topic_sql = "SELECT topic_title FROM forum_topics
WHERE topic_id = '".$safe_topic_id."'";
$verify_topic_res = mysqli_query($mysqli, $verify_topic_sql)
or die(mysqli_error($mysqli));

if (mysqli_num_rows($verify_topic_res) < 1) {
//this topic does not exist
$display_block = "<p><em>You have selected an invalid topic.<br/>
Please <a href=\"topiclist.php\">try again</a>.</em></p>";
} else {
//get the topic title
while ($topic_info = mysqli_fetch_array($verify_topic_res)) {
$topic_title = stripslashes($topic_info['topic_title']);
}

//gather the posts
$get_posts_sql = "SELECT post_id, post_text,
DATE_FORMAT(post_create_time,
'%b %e %Y<br/>%r') AS fmt_post_create_time, post_owner
FROM forum_posts
WHERE topic_id = '".$safe_topic_id."'
ORDER BY post_create_time ASC";
$get_posts_res = mysqli_query($mysqli, $get_posts_sql)
or die(mysqli_error($mysqli));

//create the display string
$display_block = <<<END_OF_TEXT
<div style="text-align: left; padding: .3em;"><p><n3 style="font-size: 13pt; color: #ffe066;">Showing posts for </n3><br><n5b style="font-size: 18pt; color: #ffe066;">$topic_title</n5b></p></div>
<table>
<tr>
<th class="headertext">AUTHOR</th>
<th class="headertext">POST</th>
</tr>
END_OF_TEXT;

while ($posts_info = mysqli_fetch_array($get_posts_res)) {
$post_id = $posts_info['post_id'];
$post_text = nl2br(stripslashes($posts_info['post_text']));
$post_create_time = $posts_info['fmt_post_create_time'];
$post_owner = stripslashes($posts_info['post_owner']);

//add to display
$display_block .= <<<END_OF_TEXT
<tr>
<td class="listcells" valign="top" style="max-width: 33%; text-align: left;"><p><n5 style="font-size: 16pt; color: #ffe066;">$post_owner</n5><br/>
<n3 style="font-size: 13pt; color: #ffe066;">created on:<br/>$post_create_time</n3></p></td>
<td class="listcells" style="background-color: #eaeafb;"><p><n2 style="font-size: 12pt; color: black;">$post_text</n2><br/><br/></p>
<div style="width: 100%; background-color: #111155; padding: .3em; text-align: left;"><a href="replytopost.php?post_id=$post_id"><span><n3 style="font-size: 14pt;">REPLY TO POST</n3></span></a></div></td>
</tr>
END_OF_TEXT;
}

//free results
mysqli_free_result($get_posts_res);
mysqli_free_result($verify_topic_res);

//close connection to MySQL
mysqli_close($mysqli);

//close up the table
$display_block .= "</table>";
}
?>
<!doctype html>
<html><!-- InstanceBegin template="/Templates/index-forum.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
</head>

<body bgcolor="#000000" style="">

<div class="container"><!-- begin container of everything 1200px max width -->

<div class="jumbotron" ETC......
</div><!-- end jumbotron -->

<nav class="navbar navbar-inverse"><!-- begin navbar --> ETC......
</nav><!-- end navbar -->

<div class="col-lg-8 col-sm-12 column-middle"><!-- begin middle column -->  

        <!-- InstanceBeginEditable name="EditRegion3" -->
        <div class="title-bar"><n6>Join the Discussion with this Topic</n6></div> 
            <div class="main-content" style="">
                <table width="100%" ><!-- holds list content table -->
                    <tr>
                        <td style="padding-left: 0px; margin-right: 0px;"><!-- contains list content -->
                            <table width="100%" valign="top" style="">
                                <tr>
                                    <td valign="top" align="center" width="" style="padding: 1em;">
                                        <div style="margin-bottom: 2em; margin-top: 2em;">
                                            <?php echo $display_block; ?>
                                            <!-- <p>Would you like to <a href="addtopic.html">add a topic</a>?</p> -->
                                        </div>
                                    </td><!-- ccccc -->
                                </tr>
                            </table><!-- end contains list content -->
                        </td>
                    </tr>
                </table><!-- end holds list content table -->
                    <div class="bottom-bar" style="border-top-style: solid; border-top-color: #0052cc; border-top-width: 1px; margin-left: 9px; margin-right: 9px; margin-top: 20px;"><n6>&nbsp;</div>
                    <table width="100%" style="padding-left: 20px; margin-right: 0px; margin-bottom: 6em;"><!-- holds pic and text -->
                        <tr>
                            <td align="right" valign="top" width="50%" height="" style="padding: 1em;">
                                <img src="img/darian-warrior-w300-h443.png" class="img-responsive" width="203" height="300" alt=""/>
                            </td><!-- end image cell-->

                            <td width="50%" align="left" valign="middle" width="" style="padding-right: 1em; padding-left: 1em; padding-top: 1em; padding-bottom: 1em;">
                                <div class="blurb-box" style="width: 75%; text-align: left; padding: 1em; margin-bottom: 6em;"><p><n3 style="color: #ffe066; font-size: 14pt; text-align: left;">This is the topic you have chosen. The original post and all replies are shown here. Simply click "REPLY TO POST" and add to the discussion. Your reply will also be posted.</n3></p></div>
                            </td><!-- end text cell-->
                        </tr>
                    </table><!-- end holds pic and text -->
            </div>
        <!-- InstanceEndEditable --> 

I can offer code from other relevant pages if it were necessary. (addtopic.php, db.php, dbalt.php, do_addtopic.php, replytopost.php, showtopic.php, topiclist.php, there others that would not have relavence as would some of these) I have created a user for anyone who would like to help with this. You can login as: Username: ForumGuest Password: i#F4$2*ic The forum login page URL is: http://sungraffix.net/forum2/login.php Again, I would love to enable any user making a post with a reply to a topic the ability to edit their post they have just made during the current session. Any post they have made during the session would retain their ability to edit. I suppose it would be best if a new session would negate that ability. Maybe I'm asking too much, maybe not? I would be gratefull if someone can answer this. ...Thanks...

0

There are 0 answers