注释PHP页面和数据库的问题

时间:2013-12-28 04:54:36

标签: php html database mysqli comments

您好我正在尝试创建一个评论PHP页面。但是数据库似乎没有链接到php页面。我已经三次检查了我的数据库信息,这绝对是正确的,但似乎无法允许用户提交评论。当您将文本放入框中并按提交页面时,只需重新加载,当我检查应该包含所有添加的注释的表时,它是空白的,就像我的代码中没有正确请求数据库一样? 我不确定这是不是问题,因为我对PHP和数据库都很新! 有谁知道我可能做错了什么?

数据库看起来像这样

名称类型归类空缺省额外

ID int(11)否无AUTO_INCREMENT
userID int(11)是NULL
profileID int(11)是NULL
评论文字utf8_general_ci否无
情绪int(11)是NULL
commDate timestamp否CURRENT_TIMESTAMP

<?php
session_start();
require_once('recaptcha/recaptchalib.php'); 
if (!isset($_SESSION['logged'])){
$_SESSION = array();
$_SESSION = destroy();
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'; 
 $db_username = 'xxxx'; 
$db_password = 'xxxx'; 
$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);
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(trim($_POST['submit']) == "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 . ")";
            }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);

 //}
}
}
}
?>
<!DOCTYPE html PUBLIC>
<html xmlns="http://www.w3.org/1999/xhtml">
<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 id="comments" action="home.php" method="post"> <select name="comments">
</select>
<h1>Do you have a comment on preferred company or number of bedrooms?</h1>
Comment: <textarea rows="2" cols="30" name="comment"></textarea>

<input type="submit" id="submit" name="submit" value="Submit" />
<?php echo $message;
    echo $str_result;
?>
<?php
    require_once('recaptcha/recaptchalib.php');
        $publickey = "6Lem4-gSAAAAAMHLAVbieIknMtUZo71ZKzzCkoFN";
    echo recaptcha_get_html($publickey);
?>
<?php echo $recaptcha_form;   //Close connection!
   mysqli_close($db_server);
?>
</form>
</body>
</html>

0 个答案:

没有答案