2017-08-27 23 views
0

我插入評論我的Blog(PHP,MySQL的,HTML,CSS)的問題。 我在MySQL與數據庫:使用PHP的HTML表單到MySQL插入數據會導致錯誤

  1. 帖子(編號,分類,標題,正文,作者,標籤,日期),
  2. 評論(ID,POST_ID,作者,電子郵件,內容,日期),
  3. 類別(ID,姓名)

我不知道如何插入到POST_ID comments表。我嘗試了很多選擇,但我收到了這條消息,但我不確定我犯了什麼錯誤。 我沒有137行,但我知道,這個問題是在插入查詢某處。有人能幫我弄明白嗎?

消息:

您的SQL語法錯誤;檢查對應於你的MySQL服務器版本使用附近的「」在行137

<?php include 'includes/header.php'; ?> 
<?php 
    $id = $_GET['id']; 

    //Create DB Object 
    $db = new Database(); 

    //Create Query 
    $query = "SELECT * FROM posts WHERE id = ".$id; 
    //Run Query 
    $post = $db->select($query)->fetch_assoc(); 

    //Create Query 
    $query = "SELECT * FROM categories"; 
    //Run Query 
    $categories = $db->select($query); 

    //add code 
    //Create Query 
    $query = "SELECT * FROM comments WHERE post_id = ".$id; 
    //Run Query 
    $comments = $db->select($query); 
     //test if the form is submitted 
    if(isset($_POST['submit'])) 
    { 
     //Assign Vars 
     //$post_id = mysqli_real_escape_string($db->link, $_POST['post_id']); 
     //$post_id = $id; 
     //if(!is_numeric($post_id)) 
     // die('invalid post id'); 
     $author = mysqli_real_escape_string($db->link, $_POST['author']); 
     $email = mysqli_real_escape_string($db->link, $_POST['email']); 
     $content = mysqli_real_escape_string($db->link, $_POST['content']); 

     //Simple Validation 
     if($post_id == '' || $author == '' || $email == '' || $content == '') 
     { 
      //Set Error 
      $error = 'Please fill out all required fields'; 
     } 
     else 
     { 
      $query = "INSERT INTO comments (post_id, author, email, content) 
       VALUES('$post_id', '$author', '$email', '$content')"; 

      $insert_row = $db->insert($query); 
     } 

    } 
?> 
<!-- dodajemy kod--> 
<div class="blog-post"> 
      <h2 class="blog-post-title"><?php echo $post['title']; ?></h2> 
      <p class="blog-post-meta"><?php echo formatDate($post['date']); ?> by <a href="#"><?php echo $post['author']; ?></a></p> 
       <?php echo $post['body']; ?>  
      </div><!-- /.blog-post --> 
<!-- dodajemy kod--> 

<?php if($comments) : ?> 
<?php echo '<ol id="comments">'; ?>  
    <?php while($row = $comments->fetch_assoc()) : ?> 
     <?php echo '<li id="comment-'.$row['id'].'">'; ?> 
      <p><a href="#"><?php echo $row['author']; ?></a> - <?php echo formatDate($row['date']); ?> </p> 
       <?php echo $row['content']; ?> 
        <?php echo '</li>'; ?> 
    <?php endwhile; ?>  
<?php echo '</ol>'; ?>  
<?php else : ?> 
    <p>There are no comments yet</p> 
<?php endif; ?> 
<br> 
<form role="form" method="post" action="post.php"> 
    <div class="form-group"> 
    <label>Author</label> 
    <input name="author" type="text" class="form-control" placeholder="Enter Author Name"> 
    </div> 
    <div class="form-group"> 
    <label>Email</label> 
    <input name="email" type="text" class="form-control" placeholder="Enter Email Adress"> 
    </div> 
    <div class="form-group"> 
    <label>Content</label> 
    <textarea name="content" class="form-control" placeholder="Enter Comment Content"></textarea> 
    </div> 
    <div class="form-group"> 
    <input type='hidden' name='post_id' id='post_id' value='<?php echo $id; ?>' /> 
    </div> 
    <div> 
    <input name="submit" type="submit" class="btn btn-default" value="Submit" /> 
    <a href="index.php" class="btn btn-default">Cancel</a> 
    </div> 
    <br> 
</form> 
<?php include 'includes/footer.php'; ?> 
+2

粘貼代碼__here__,而在一些其他資源 –

+0

什麼是你想逃避每'''用反斜槓時實現? –

+0

這是一個非常簡單的驗證,以不插入數據庫上的空數據,或者你的意思是查詢? –

回答

0

我發現我需要得到並通過帖子的ID中的作用正確的語法手冊形式:

action="post.php?id=<?php echo $_GET['id']; ?>" 

    <?php include 'includes/header.php'; ?> 
<?php 
    $id = $_GET['id']; 

    //Create DB Object 
    $db = new Database(); 

    //Create Query 
    $query = "SELECT * FROM posts WHERE id = ".$id; 
    //Run Query 
    $post = $db->select($query)->fetch_assoc(); 

    //Create Query 
    $query = "SELECT * FROM categories"; 
    //Run Query 
    $categories = $db->select($query); 

    //add code 
    //Create Query 
    $query = "SELECT * FROM comments WHERE post_id = ".$id; 
    //Run Query 
    $comments = $db->select($query); 
     //test if the form is submitted 
    if(isset($_POST['submit'])) 
    { 
     //Assign Vars 
     $post_id = mysqli_real_escape_string($db->link, $_POST['post_id']); 
     //$post_id = $_GET['id']; 
     //if(!is_numeric($post_id)) 
     // die('invalid post id'); 
     $author = mysqli_real_escape_string($db->link, $_POST['author']); 
     $email = mysqli_real_escape_string($db->link, $_POST['email']); 
     $content = mysqli_real_escape_string($db->link, $_POST['content']); 

     //Simple Validation $post_id == '' || 
     if((!is_numeric($post_id))|| $author == '' || $email == '' || $content == '') 
     { 
      //Set Error 
      $error = 'Please fill out all required fields'; 
     } 
     else 
     { 
      $query = "INSERT INTO comments (post_id, author, email, content) 
       VALUES('$post_id', '$author', '$email', '$content')"; 

      $insert_row = $db->insert($query); 
     } 

    } 
?> 
<!-- dodajemy kod--> 
<div class="blog-post"> 
      <h2 class="blog-post-title"><?php echo $post['title']; ?></h2> 
      <p class="blog-post-meta"><?php echo formatDate($post['date']); ?> by <a href="#"><?php echo $post['author']; ?></a></p> 
       <?php echo $post['body']; ?>  
      </div><!-- /.blog-post --> 
<!-- dodajemy kod--> 

<?php if($comments) : ?> 
<?php echo '<ol id="comments">'; ?>  
    <?php while($row = $comments->fetch_assoc()) : ?> 
     <?php echo '<li id="comment-'.$row['id'].'">'; ?> 
      <p><a href="#"><?php echo $row['author']; ?></a> - <?php echo formatDate($row['date']); ?> </p> 
       <?php echo $row['content']; ?> 

        <?php echo '</li>'; ?> 
    <?php endwhile; ?>  
<?php echo '</ol>'; ?>  
<?php else : ?> 
    <p>There are no comments yet</p> 
<?php endif; ?> 
<br> 
<form role="form" method="post" action="post.php?id=<?php echo $id; ?>"> 
    <div class="form-group"> 
    <label>Author</label> 
    <input name="author" type="text" class="form-control" placeholder="Enter Author Name"> 
    </div> 
    <div class="form-group"> 
    <label>Email</label> 
    <input name="email" type="text" class="form-control" placeholder="Enter Email Adress"> 
    </div> 
    <div class="form-group"> 
    <label>Content</label> 
    <textarea name="content" class="form-control" placeholder="Enter Comment Content"></textarea> 
    </div> 
    <div class="form-group"> 
    <input type='hidden' name='post_id' id='post_id' value='<?php echo $id; ?>' /> 
    </div> 
    <div> 
    <input name="submit" type="submit" class="btn btn-default" value="Submit" /> 
    <a href="index.php" class="btn btn-default">Cancel</a> 
    </div> 
    <br> 
</form> 
<?php include 'includes/footer.php'; ?> 
相關問題