简体   繁体   English

PHP数据不在两页之间发送

[英]PHP data not sending between two pages

I'm having trouble trying to send data between two pages. 我在尝试在两个页面之间发送数据时遇到麻烦。 I have looked for examples but could not figure it out at all; 我一直在寻找例子,但根本无法弄清楚。 it's frustrating. 令人沮丧。

The SQL is the following: SQL如下:

$query = "SELECT post_id, title , bodyofpost , username, datetime
            FROM Post GROUP BY datetime DESC
            LIMIT ?,?";

Here is the code snippet of the first PHP page that has the data that is needed to be sent to the other: 这是第一个PHP页面的代码片段,其中包含需要发送到另一个页面的数据:

while ($stmt->fetch()) 
  {

    echo "Post ID: ".$post_id."<br/>";
    echo "Title : ".$title."<br/>";
    echo "<strong> Username: ".$username."</strong><br />";
    echo "BodyofPost: ".$bodyofpost."<br/>";
    echo "Date Created: ".$datetime."<br/><p>";
    echo $addComment = "<a href=\"addComments.php?$post_id\">AddComment</a>";
    echo"</br>";
    echo"</br>";


  }

This is the add comments page that the postID needs to be sent to. 这是需要将postID发送到的添加评论页面。 It has a hidden value and when I try to insert the postID into the database, the postID is always 0, which it shouldn't be. 它有一个隐藏的值,当我尝试将postID插入数据库时​​,postID始终为0,而​​不应该为0。

    echo'</form>';
    echo '<form method="post" action="addComments.php">';
    echo'<input type="hidden" name="post_id" value="postID"/>';
    echo '<table>';
    echo '<tr><td>Comments:</td>';
    echo '<td><input type="text" name="comment"></td></tr>';
    echo '<tr><td colspan="2" align="center">';
    echo '<input type="submit" value="PostComment"></td></tr>';
    echo '</table></form>';

Ignore the comment form above; 忽略上面的评论表格; it works. 有用。

This is the part of the add comment code where I tried inserting only the postID into the database to test if it works but it didn't. 这是添加注释代码的一部分,在该部分中,我尝试仅将postID插入数据库以测试其是否有效,但无效。

if(isset($_GET['post_id'])){
    $postID= $_GET['post_id'];
    $sql = "INSERT INTO Comments (post_id)
    VALUES ('$postID') ";

}

So let's say we have 10 posts and we group by descending order and I clicked the linked for post 10. Then it would send me to the addcomment page and I typed in the desired comments etc. Then when I go and check the inserted comment at my database the post ID would always be 0; 假设我们有10个帖子,我们按降序分组,然后我单击了帖子10的链接。然后它将把我发送到addcomment页面,并输入了所需的评论,等等。然后当我去查看插入的评论时,我的数据库的帖子ID始终为0; it should be 10 because it's the postID I was commenting on. 应该是10,因为这是我在评论的postID。 Any help is appreciated!! 任何帮助表示赞赏! Thank you!! 谢谢!!

Your form method is set to POST and your code are specting GET method. 您的form方法设置为POST而您的代码正在指定GET方法。

try this out: 试试这个:

if(isset($_POST['post_id'])){
    $postID= $_POST['post_id'];
    $stmt = $connection->prepare("INSERT INTO Comments (post_id)
    VALUES (?) ");
    $stmt->bind_param('i',$postID);
    $stmt->execute();
    $stmt->close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM