2017-08-27 13 views
0

ブログ(PHP、MySQL、HTML、CSS)にコメントを挿入する際に問題があります。 、PHPを使用してHTMLフォームからMySQLにデータを挿入するとエラーが発生する

  1. ポスト(ID、カテゴリ、タイトル、本文、著者、タグ、日付)、
  2. コメント(ID、post_idの、作者、電子メール、コンテンツ、日付): 私はとMySQLでデータベースを持っています
  3. カテゴリ(ID、名前)

私はコメントテーブルにpost_idのを挿入する方法がわからないです。私は多くのオプションを試してみましたが、私はこのメッセージを受け取りますが、私はどこで間違いを犯したのか分かりません。 私は行137を持っていませんが、私は問題が挿入クエリのどこかにあることを知っています。誰かが私にそれを理解するのを助けることができる?

メッセージ:

あなたのSQL構文でエラーが発生しています。私は私が手にしての行動でポストのIDを渡すために必要なことが判明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

ラインで「」の近くに使用する権利構文についてはMySQLサーバのバージョンに対応するマニュアルを確認してください形式:

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'; ?> 
関連する問題