php表单两次提交信息

时间:2015-02-23 05:54:34

标签: php forms

我有一个表单应该将论坛主题的回复输入到数据库中,并将用户重定向回相同的主题。经过多次尝试和错误后,我终于让表单工作了,只是每次都将两个相同的条目放入数据库中。我无法弄清楚为什么。我已经查找了同样的问题,并且大多数其他人在表单提交后没有重定向,或者他们使用的是AJAX或jquery等。这是我的页面信息:

<?php
session_start();
include_once('includes/config.php');
include_once('classes/topic.php');
include_once('classes/post.php');
include('includes/header.php');

?>

<link rel="stylesheet" href="css/dd.css">

<?php

$topic = new Topic;

if (isset($_GET['id'])) 
{
    $topic_id = $_GET['id'];
    $data = $topic->fetch_data($topic_id);

        if (isset($_POST['content'])) 
        { 
        // someone posted a reply
        $date = date('Y-m-d H:i:s');
        $by = $_SESSION['user_id'];
        $query = $pdo->prepare("INSERT INTO dd_posts (post_content, post_date, post_by, post_topic) VALUES (? ,? ,?, ?)");

        $query->bindParam(1, $_POST['content']);
        $query->bindParam(2, $date);
        $query->bindParam(3, $by);
        $query->bindParam(4, $_GET['id']);

        $query->execute();
        $result = $query->execute();

        header("location:topic.php?id=".$_GET['id']); 
        exit;
        }      
?>
    <div id ="wrapper">
        <div class="drop-section">
            <div id="menu">
                <a class="item" href="drop_index.php">Dead Drop</a>
                <a class="item" href="add_topic.php">New Post</a>
                <a class="item" href="admin/add_cat.php">New Category</a>
                <div id="userbar">
                    <?php
                        if( $user->is_logged_in() ) {
                            echo 'Hello ' . $_SESSION['user_name'] . '. How are you?';
                        } else {
                            echo '<a class="item" href="login.php">Sign in</a> or <a class="item" href="index.php">Create an account</a>';
                        } 
                    ?>
                </div>
            </div>
            <table>
                <tr class = "header-row">
                    <div id = "sans">
                        <?php echo $data['topic_subject']; ?> 
                            - <small>started by <a href="player.php?id=<?php echo $data['user_id']; ?>"><?php echo $data['user_name']; ?>                               </a></small><br />
                        <?php echo $data['topic_content']; ?>
                    </div>
                </tr>

<?php
// retrieve all the replies to the original topic
$post = new Post;
$topic_id = $_GET['id'];
$posts = $post->fetch_all_posts_by_topic($topic_id);

?>
                <tr>
                    <td class="first-column">

                        <?php foreach ($posts as $post) { ?>

                        <div class="drop-content-box">

                            <li><?php echo $post['post_content']; ?><br />
                                <div class = "forum-user-info">
                                    <a href="player.php?id=<?php echo $post['user_id']; ?>">
                                    <?php echo $post['user_name']; ?></a> - level: 
                                    <?php echo $post['user_level']; ?>

                                </div> 
                            </li>
                        </div>

                        <?php } ?>

                    </td>
                </tr>
            </table>

<?php
        if( $user->is_logged_in() ) 
        {

            ?>    
                <div id = "header-section">Reply</div>
                        <?php if (isset($error)) { ?>
                            <small><?php echo $error; ?></small>
                        <?php } ?>

                        <form action="<?php echo "topic.php?id=".$_GET['id']?>" method="post" autocomplete="off">
                            <small><i>Do not post the actual answer to any level.</i></small><br />
                        <textarea rows="15" cols="50" name="content" placeholder="Give us your thoughts..."></textarea><br />
                        <input type="submit" value="Post" />
                        </form>
        </div>
</div>
<?php
        } else {
                echo '<div id = "errors"><small>You must be signed in to reply.</div></small>';
        }
}

include_once('includes/footer.php');
?>

1 个答案:

答案 0 :(得分:0)

您正在执行两次查询。

$query->execute();
$result = $query->execute();