2016-05-03 31 views
1

私のデータベースには、「投稿」テーブル(投稿された投稿を含む)と「返信」テーブル(すべての投稿への返信をすべて含む)があります。投稿を元の投稿のIDに投稿するにはどうすればよいですか?

ユーザーが投稿に返信すると、返信を含むデータベースの行にも元の投稿のIDが含まれます。

post_idの値を除いてすべてがすべて入力されていますが、すべてのエントリがゼロです。これをどうすれば解決できますか?

home.php:

<?php 
// top_content.php includes the database connection file. 
include "top_content.php"; 
// Include our script to convert MySQL timestamp to 'ago' format. 
include "time_ago.php"; 
// Create an object for the time conversion functions 
$timeAgoObject = new convertToAgo; 

include "menu_and_logo.php"; 
// Query the database for all submitted posts. // 
$sql = "SELECT * FROM Posts ORDER BY date DESC"; 
// Store the result in a variable. // 
$res = mysqli_query($db, $sql) or die(mysqli_error); 
// Check that the result has > 0 rows; that at least one post exists in the database. // 
if(mysqli_num_rows($res) != 0) { 
     // The mysqli_fetch_array retrieves and returns the next row of our query, which is then assigned to $row. // 
     // Then it executes the echos, and the procedure begins anew. This is how we get the post list. // 
     while($row = mysqli_fetch_array($res)) { 
      // Set the post_id variable equal to the post_id of each post. // 
      $post_id = $row['post_id']; 
      $ts = $row['date']; 
      $post_title = $row['post_title']; 
      $post_creator = $row['post_creator']; 
      // Convert Date Time 
      $convertedTime = ($timeAgoObject -> convert_datetime($ts)); 
      // Then convert to 'ago' time 
      $when = ($timeAgoObject -> makeAgo($convertedTime)); 
      // Display the data of each row. THe while row will execute all posts, creating a list 
      // display. 
      echo '<div>'; 
      // The text is set as the post title, and points to post.php?id=*the post_id of the post. // 
      // In post.php, we check that $_GET['id'] is set (that the user is visiting post.php?id=some_integer), 
      // then query the database for the respective post, and echo the relevant content. // 
      echo '<a href="post.php?id='.$post_id.'">'.$post_title.'</a>'; 
      echo '<p>by <a href = "view_profile.php?user='.$post_creator.'">'.$post_creator.'</a> '.$when.'</p>'; 
      echo '</div>'; 

     } 

}else { 
    echo "There are no posts."; 
} 

?> 

post.php:

<!DOCTYPE html> 

<?php 
session_start(); 
// Include our script to convert MySQL timestamp to 'ago' format. 
include "time_ago.php"; 
// Create an object for the time conversion functions 
$timeAgoObject = new convertToAgo; 
// Iniate connection to the database. 
include "db_connect.php"; 
// Check that the user is visiting post.php?id=some_integer). 
if($_GET['id']) { 
     // Set the post id as a variable, for convenience. 
    $post_id = $_GET['id']; 
     // Query the database for the post that corresponds to the post-title link that has been clicked. 
     $sql = "SELECT * FROM Posts WHERE post_id = '".$post_id."' "; 
     $res = mysqli_query($db, $sql)or die(mysqli_error()); 
     // Check that there exists a row containing the relevant post id. 
     if(mysqli_num_rows($res) != 0) { 
      // Store the current row's array of data as a variable. 
      while($row = mysqli_fetch_array($res)) { 
       // Set the current row's data as variables. 
       $post_title = $row['post_title']; 
       $post_content = $row['post_content']; 
       $post_creator = $row['post_creator']; 
       $ts = $row['date']; 
        // Convert Date Time 
       $convertedTime = ($timeAgoObject -> convert_datetime($ts)); 
       // Then convert to ago time 
       $when = ($timeAgoObject -> makeAgo($convertedTime)); 
       // Display the relevant post data. 
       echo '<h2>'.$post_title.'</h2>'; 
       echo '<p>Submitted by <a href = "view_profile.php?user='.$post_creator.'">'.$post_creator.'</a> '.$when.'</p><nr><br>'; 
       echo ''.$post_content.'';  
      } 

     }else{ 
      echo "This post does not exist."; 
     } 

}else{ 
     header("home.php"); 
} 

?> 
<!-- I've moved the html tags here because the file needs the $post_title  variable before setting the title --> 
<html> 
<head><title><?php echo ''.$post_title.' - Lboro Maths'; ?></title></head> 

<body> 
    <!-- #2: The form where users can submit replies to the original post. --> 
    <form action="reply_parse.php" method="POST"> 
      <input type="textarea" name="reply_content" placeholder="Post a reply..."> 
      <input type="submit" name="submit_reply" value="Reply"> 
    </form> 

</body> 
</html> 

reply_parse.php:私たちは隠しフィールドを挿入し、それを変数に渡すために必要な

<?php 
session_start(); 

include "db_connect.php"; 

$post_id = $_GET['id']; 
$reply_content = $_POST['reply_content']; 
$reply_creator = $_SESSION['username']; 
$date = date('y-m-d H:i:s'); 

if(isset($_POST['submit_reply'])) { 

     $sql = "INSERT INTO Replies (post_id, reply_content, reply_creator, reply_date) VALUES ('$post_id', '$reply_content', '$reply_creator', '$date')"; 
     $res = mysqli_query($db, $sql)or die(mysqli_error); 

     header("Location: home.php"); 

}else{ 
     echo "Fail."; 
} 

?> 
+0

IDが取得している値を確認するには、このコードを呼び出すコードを確認する必要があります。 –

+0

お詫び申し上げます。私の編集は十分ですか? – Callum

+0

ブラウザで、Webページを右クリックし、「ソースコード」を選択します。 –

答えて

0

あなたはpost.phpでフォームでない "ID" フィールドを持っていない、とフォームがメソッドのPOST GETではないので、「reply_parseです。 php "は" id "として何も得られません。私はそれが問題だと思う。

1

'id'の値を取得します。オリジナル

:固定

<body> 
    <!-- #2: The form where users can submit replies to the original post. --> 
    <form action="reply_parse.php" method="POST"> 
      <input type="textarea" name="reply_content" placeholder="Post a reply..."> 
      <input type="submit" name="submit_reply" value="Reply"> 
    </form> 
</body> 

<body> 
    <!-- #2: The form where users can submit replies to the original post. --> 
    <?php 
     echo "<form action='reply_parse.php' method='POST'>"; 
     echo "<input type='hidden' name ='post_id' value='$post_id'>"; 
     echo "<input type='textarea' name='reply_content' placeholder='Post a reply...'>"; 
     echo "<input type='submit' name='submit_reply' value='Reply'>"; 
     echo "</form>"; 
    ?> 
</body> 
+0

フォームはmethod = POSTで、method = GETに変更します。あるいは '$ _POST [" id "]'で '$ _GET [" id "]'を変更してください。それで、私のために何ですか?何もない?ちょうどありがとう、さようなら? –

+0

私のコメントは答えですか?それは素晴らしいアイデアのように聞こえる、分を私に与える... –

関連する問題