使用php和html添加评论

时间:2013-12-23 13:36:53

标签: php html database

我想知道是否有任何善良的人能够帮助我解决我的PHP难题!我非常非常新的PHP,我不太明白如何添加功能的评论部分。我创建了一个数据库表(名为“comments”)来存储用户将提交的所有注释。我不确定这些事情: 1.如何将评论部分(在php页面 - home.php上)连接到我的数据库表(评论) 2.如何将人们发布的评论发布到同一页面 - home.php

我做错了,因为当我输入网址时出现此错误

Parse error: syntax error, unexpected '{', expecting '(' in .../home.php on line 34

无论如何,我希望有人可以帮助我! 感谢

<?php
session_start();
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");
 require_once("db_connect.php");
 }
 //EXISTING DATABASE CONNECTION CODE
 //if (!$db_server){
    //die("Unable to connect to MySQL: " . mysqli_connect_error($db_server)); 
 }else{    $   db_status = "not connected";
       //NEW SUBMISSION HANDLING CODE HERE
    //if(trim($_POST['submit']) == "Submit"){
    //}//EXISTING CODE (to create the options list) HERE...
 //} 
 require_once('recaptcha/recaptchalib.php');
 $privatekey = " 6Lem4-gSAAAAADsaa9KXlzSAhLs8Ztp83Lt-x1kn"; 
 $resp = recaptcha_check_answer ($privatekey,
    $_SERVER["REMOTE_ADDR"], 
    $_POST["recaptcha_challenge_field"], 
    $_POST["recaptcha_response_field"]);
 $message = "";
 if (!$resp->is_valid) {
    $message = "The reCAPTCHA wasn't entered correctly. Go back and try it again     (reCAPTCHA said: " . $resp->error . ")";
    } else {
    // ADD YOUR CODE HERE to handle a successful ReCAPTCHA submission 
  // e.g. Validate the data
        $unsafe_name = $_POST['fullname'];
        } 
        $message .= "Thanks for your input $unsafe_name !";
        echo $message;

         if {
              $bedrooms = $_POST['year'];
          $bedrooms = clean_string($db_server, $year); 
          $comment = clean_string($db_server, $_POST['comment']);
            else ($comment != "") {
             $query2 = "INSERT INTO comments (comment) VALUES ('$comment')";
                     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);
                 while($array = mysqli_fetch_array($result3)){
                        $comments = date('d/m/Y', strtotime($array['commDate'])) . "<p>" . $array['comment'] . "</p><br/>";

                 }

?>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <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>
 <?php echo $recaptcha_form; ?>
 <input type="submit" id="submit" name="submit" value="Submit form" />

 </form>

 </body>
 </html>

1 个答案:

答案 0 :(得分:0)

您的以下声明syntactically不正确,

if {
      $bedrooms = $_POST['year'];
      $bedrooms = clean_string($db_server, $year); 
      $comment = clean_string($db_server, $_POST['comment']);
else ($comment != "") {

应该是,

if (isset($comment) && $comment == '') {
      $bedrooms = $_POST['year'];
      $bedrooms = clean_string($db_server, $year); 
      $comment = clean_string($db_server, $_POST['comment']);
}
else {