2013-12-23 108 views
0

我想知道是否有任何善良的人可以幫我解決我的php難題!我非常非常新的PHP,我不明白如何添加一個功能的評論部分。我製作了一個數據庫表(名爲'comments')來存儲用戶提交的所有評論。我不確定這些東西: 1.如何將評論部分(在php頁面 - home.php)連接到我的數據庫表(評論) 2.如何使人張貼的評論貼到相同的頁面 - home.php使用php和html添加註釋

我做錯的現在,當我在網址欄中輸入這個錯誤出現

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> 

回答

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 { 
+0

太謝謝你了!它現在說我沒有錯誤!但我現在有錯誤reCAPTCHA輸入不正確。回去再試一次。 (reCAPTCHA說:不正確的captcha溶膠)感謝您的輸入! 警告:mysqli_error()期望參數1爲mysqli,null在第41行的/home/fh11alb/public_html/home.php中給出012, 警告:mysqli_error()期望參數1爲mysqli,null在...中給出/ 42線上的home.php 插入失敗: – Ash

+0

你在哪裏定義變量'$ db_server'?它似乎失蹤了。 – Rikesh