【问题标题】:PHP HTML form not uploading data to the databasePHP HTML表单未将数据上传到数据库
【发布时间】:2021-11-07 22:38:46
【问题描述】:

我正在创建一个社交网站,并尝试在帖子下添加评论。

当我尝试从comment_frame.php(所有评论代码所在的位置)添加它时,评论会正确上传。但是,当我尝试将 index.php 页面中的评论添加到 comment_frame.php 页面时,数据库中没有任何内容,也没有出现任何错误。我的代码如下。

comment_frame.php(有效的代码):

$post_id = $_POST['post_id'] ?? 0;

<form class='comment_frame.php' id='single_form' name='postComment<?php echo $post_id; ?>' 
    method='POST'>

    <textarea name='post_body' rows="3" placeholder='Write a comment...'></textarea>

    <input type='submit' name='postComment<?php echo $post_id; ?>' value='Post'>

</form>

if(isset($_POST['postComment' . $post_id])) {

        if (empty($_POST["post_body"])) {

            // echo "Comment can't be empty";
            echo "Comment can't be empty. <a href=".$_SERVER['HTTP_REFERER'].">Try Again</a>";
            //die() also terminates the script with display the message.
            exit();
        }

        $post_body = trim(strip_tags(filter_var($_POST['post_body'], FILTER_SANITIZE_STRING)));

        $stmt = $con->prepare("INSERT INTO comments (post_body, posted_by, posted_to, post_id) 
        VALUES (?, ?, ?, ?)");
        $stmt->bind_param("sssi", $post_body, $userLoggedIn, $posted_to, $post_id);
        $stmt->execute();

        if($posted_to != $userLoggedIn) {

            $notification = new Notification($con, $userLoggedIn);
            $notification->insertNotification($post_id, $posted_to, 'comment');
        }

            $get_commenters = $con->prepare('SELECT post_body, posted_by, posted_to, date_added 
                FROM comments WHERE post_id = ? ORDER BY date_added DESC');

            $get_commenters->bind_param("i", $post_id);
            $get_commenters->execute();
            $get_commenters->bind_result($post_body, $posted_by, $posted_to, $date_added);
            $get_commenters_result = $get_commenters->get_result();
            $notified_users = array();

            while ($row = $get_commenters_result->fetch_assoc()) {

            if($row['posted_by'] != $posted_to && $row['posted_by'] != $user_to 
                && $row['posted_by'] != $userLoggedIn && !in_array($row['posted_by'], $notified_users)) {

                $notification = new Notification($con, $userLoggedIn);
                $notification->insertNotification($post_id, $row['posted_by'], "comment_non_owner");

                array_push($notified_users, $row['posted_by']);
            }
        }
    }

不起作用的代码:

index.php:

<div class='comment_div'>

                            <form target='frame' class='comment_frame.php?post_id=$post_id' 
                            id='comment_form' name='postComment" . $post_id . "' 
                            method='POST'>

                                <textarea name='post_body' placeholder='Write a comment...'></textarea>

                                <input type='submit' name='postComment" . $post_id . "' 
                                value='". $post_id ."'>

                            </form>

                        </div>

comment_frame.php:

$post_id = $_POST['post_id'] ?? 0;

if(isset($_POST['postComment' . $post_id])) {

        if (empty($_POST["post_body"])) {

            // echo "Comment can't be empty";
            echo "Comment can't be empty. <a href=".$_SERVER['HTTP_REFERER'].">Try Again</a>";
            //die() also terminates the script with display the message.
            exit();
        }

        $post_body = trim(strip_tags(filter_var($_POST['post_body'], FILTER_SANITIZE_STRING)));

        $stmt = $con->prepare("INSERT INTO comments (post_body, posted_by, posted_to, post_id) 
        VALUES (?, ?, ?, ?)");
        $stmt->bind_param("sssi", $post_body, $userLoggedIn, $posted_to, $post_id);
        $stmt->execute();

        if($posted_to != $userLoggedIn) {

            $notification = new Notification($con, $userLoggedIn);
            $notification->insertNotification($post_id, $posted_to, 'comment');
        }

            $get_commenters = $con->prepare('SELECT post_body, posted_by, posted_to, date_added 
                FROM comments WHERE post_id = ? ORDER BY date_added DESC');

            $get_commenters->bind_param("i", $post_id);
            $get_commenters->execute();
            $get_commenters->bind_result($post_body, $posted_by, $posted_to, $date_added);
            $get_commenters_result = $get_commenters->get_result();
            $notified_users = array();

            while ($row = $get_commenters_result->fetch_assoc()) {

            if($row['posted_by'] != $posted_to && $row['posted_by'] != $user_to 
                && $row['posted_by'] != $userLoggedIn && !in_array($row['posted_by'], $notified_users)) {

                $notification = new Notification($con, $userLoggedIn);
                $notification->insertNotification($post_id, $row['posted_by'], "comment_non_owner");

                array_push($notified_users, $row['posted_by']);
            }
        }
    }

【问题讨论】:

  • 这将有助于您更具体地识别问题。脚本不起作用有点宽泛。您是否检查过哪些工作有效,哪些地方失败/没有按照您的预期工作。
  • action 是对的。您以GET 提出此请求,因为通过?post_id=$post_id
  • 尝试在input type hidden中传递post_Id
  • @Dharman 我没有看到任何地方说它已被弃用。
  • @sirtoby 在 PHP 8.1 中已弃用。它还没有发布,但我已经警告你,这个过滤器基本上没有意义,很快就会被删除。

标签: php html mysql forms


【解决方案1】:

在表单标签中使用 action 属性而不是 class。

<form target='frame' action='comment_frame.php?post_id=$post_id' 
                        id='comment_form' name='postComment" . $post_id . "' 
                        method='POST'>

【讨论】:

    【解决方案2】:

    试试这个

    //put this code in the index.php
    
        <form action="comment_frame.php"  id='single_form' name='postComment' 
            method='POST'>
            
            <input type="hidden" value="<?php echo $post_id; ?>" name="post_id">
            <textarea name='post_body' required rows="3" placeholder='Write a comment...'></textarea>
        
            <input type='submit' name='postComment<?php echo $post_id; ?>' value='Post'>
        
        </form>
        
        //put this code in the comment_frame.php
        
        $post_id = $_POST['post_id'];
        
        if(isset($_POST['postComment' . $post_id])) {
        
                if (empty($_POST["post_body"])) {
        
                    // echo "Comment can't be empty";
                    echo "Comment can't be empty. <a href=".$_SERVER['HTTP_REFERER'].">Try Again</a>";
                    //die() also terminates the script with display the message.
                    exit();
                }
        
                $post_body = trim(strip_tags(filter_var($_POST['post_body'], FILTER_SANITIZE_STRING)));
        
                $stmt = $con->prepare("INSERT INTO comments (post_body, posted_by, posted_to, post_id) 
                VALUES (?, ?, ?, ?)");
                $stmt->bind_param("sssi", $post_body, $userLoggedIn, $posted_to, $post_id);
                $stmt->execute();
        
                if($posted_to != $userLoggedIn) {
        
                    $notification = new Notification($con, $userLoggedIn);
                    $notification->insertNotification($post_id, $posted_to, 'comment');
                }
        
                    $get_commenters = $con->prepare('SELECT post_body, posted_by, posted_to, date_added 
                        FROM comments WHERE post_id = ? ORDER BY date_added DESC');
        
                    $get_commenters->bind_param("i", $post_id);
                    $get_commenters->execute();
                    $get_commenters->bind_result($post_body, $posted_by, $posted_to, $date_added);
                    $get_commenters_result = $get_commenters->get_result();
                    $notified_users = array();
        
                    while ($row = $get_commenters_result->fetch_assoc()) {
        
                    if($row['posted_by'] != $posted_to && $row['posted_by'] != $user_to 
                        && $row['posted_by'] != $userLoggedIn && !in_array($row['posted_by'], $notified_users)) {
        
                        $notification = new Notification($con, $userLoggedIn);
                        $notification->insertNotification($post_id, $row['posted_by'], "comment_non_owner");
        
                        array_push($notified_users, $row['posted_by']);
                    }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-04
      • 1970-01-01
      • 2012-04-10
      • 2015-08-31
      • 1970-01-01
      • 2017-08-24
      • 1970-01-01
      • 2018-07-06
      相关资源
      最近更新 更多