发布在刷新页面时消失的注释

时间:2014-01-13 13:53:38

标签: php html database comments

我已经使用php(home.php)创建了一个评论页面,用户会在页面上发布评论但是当页面刷新时,所有以前的评论都会消失。 有谁知道如何让评论留在页面上? 也有人知道如何使他们登录的用户ID /名称与时间戳一起显示(添加评论时已经存在)??

<?php
session_start();
require_once('recaptcha/recaptchalib.php'); 
if (!isset($_SESSION['logged'])){
$_SESSION = array();
header('location: home_start.php'); //your login form
require_once("functions.php");
include_once("home_start.php");
}

//EXISTING DATABASE CONNECTION CODE
$db_hostname = 'xxxx';
$db_database = 'xxxx'; //'Your database name' 
$db_username = 'xxxxx'; //'your username'; 
$db_password = 'xxxx'; //'Your password'; 
$db_status = 'not initialised';
$str_result = ' ';
$str_options = ' ';
$db_server = mysqli_connect($db_hostname, $db_username, $db_password);
$db_status = "connected";
$db_select = mysqli_select_db($db_server, $db_database);
require_once("cleanstring.php");
 if (!$db_server){
die("Unable to connect to MySQL: " . mysqli_connect_error($db_server)); 
$db_status = "not connected";
}else{ 
   //NEW SUBMISSION HANDLING CODE HERE
    if(isset($_POST['submit'])) {
              $privatekey = "6Lem4-gSAAAAADsaa9KXlzSAhLs8Ztp83Lt-x1kn"; 
              $resp = recaptcha_check_answer ($privatekey,
                            $_SERVER["REMOTE_ADDR"],
                            $_POST["recaptcha_challenge_field"],
                            $_POST["recaptcha_response_field"]);

         if (!$resp->is_valid) {
                $message = "The reCAPTCHA wasn't entered correctly. Go back and try it again. (reCAPTCHA said: " . $resp->error . ")";
                        //Print out existing comment

                        } else {
                            $userID = $_SESSION['userID'];
                            $comment = clean_string($db_server, $_POST['comment']);
                            if ($comment != '') {
                            $query2 = "INSERT INTO comments (comment) VALUES ('$comment')";
                                    mysqli_select_db($db_server, $db_database);
                                    mysqli_query($db_server, $query2) or
                                                 die("Insert failed: " . mysqli_error($db_server));

                                                            $message = "Thanks for your comment!";
                            }
                            $query3 = "SELECT * FROM comments";
                            $result3 = mysqli_query($db_server, $query3);
                            if (!$result3) die("Database access failed: " . mysqli_error($db_server)); 
                            while($array = mysqli_fetch_array($result3)){
                                     $comments = date('d/m/Y', strtotime($array['commDate'])) . "<p>" . $array['comment'] . "</p><br/>";
                        }
                             mysqli_free_result($result3);

 //}
}
}
 }
 //require_once “db_close.php”;
 ?>
 <!DOCTYPE html PUBLIC "-xxxxx>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 <link href="home.css" rel="stylesheet" type="text/css"/>
 <title>Home</title>
 </head>

 <body>
 <div id="middle">
<h2><strong>HELLO!</strong></h2>
<h2>Welcome to <strong>Cosy Cribs</strong> website!</h2>
<p>This website combines all the possible lettings available to YOU from the most prefered letting companies in the great city of Leeds!</p>
<p>It was recognised that when students attempt to let a house for the next year, there were far too many different websites and companies visit; making the whole ordeal of finding a house more stressful then needs be!</p>
<p>We, at <strong>Cosy Cribs</strong>, decided that your lives needed to be made easier, and so we announce a website that provides you with all of the lettings from these different companies - all on one website - and links to the house you like.</p>
<h2>ENJOY!</h2>
 </div>
 <form method="post" action="home.php">
 <h1>Do you have a comment on preferred company or number of bedrooms?</h1>
 Comment: <textarea rows="2" cols="30" name="comment"></textarea>
 <?php
    require_once('recaptcha/recaptchalib.php');
        $publickey = "6Lem4-gSAAAAAMHLAVbieIknMtUZo71ZKzzCkoFN";
    echo recaptcha_get_html($publickey);
 ?>
 <?php echo $recaptcha_form;   //Close connection!
   mysqli_close($db_server);
 ?>
 <?php
echo $comments;
 ?>
 <form id="comments" action="home.php" method="post"> 
 <input type="submit" id="submit" name="submit" value="Submit" />
 </form>
 </body>
 </html>

0 个答案:

没有答案