更新语句mySQL问题

时间:2016-05-08 18:39:07

标签: php html mysql pdo html-form

当用户更新他们制作的博客帖子时,我无法更新数据库的posts表。

  

事件流程 - 用户发布博客文章,将其保存到数据库,然后他们可以返回并编辑它。编辑会显示一个预填充的html表单,其中填充了posts表中的数据。然后,用户可以更改标题和内容,当他们按下更新时,表单中的发布值应替换posts DB中原始帖子的标题和内容,所有其他列保持不变。

目前我的数据库似乎没有更新,不知道为什么。使用html / php / sql / pdos的组合来执行sql语句 - 为我的新手体验变得非常复杂,并且感谢任何帮助。 代码(UPDATE语句位于底部且最有问题):

// begin edit post
    if(isset($_GET['editPost'])) {
            $editPostId = $_GET['editPost'];
            $sqlEditPostCheck = <<<EOD
            SELECT author, id FROM posts WHERE author = '$currentUserId'
EOD;
            $stmtEditPostCheck = $pdo->prepare($sqlEditPostCheck);
            $stmtEditPostCheck->execute();

            $ableToEdit = false;

            while ($row = $stmtEditPostCheck->fetch(PDO::FETCH_ASSOC)) {
                if($editPostId === $row['id'] && $currentUserId === $row['author']) { $ableToEdit = true; }
            }

            if($ableToEdit === true) {
                $editPost_Title = "";
                $editPost_Content = "";

                $sqlEditPostPreFills = <<<EOD
            SELECT id, post_title, content FROM posts WHERE id="$editPostId"
EOD;
                $stmtEditPost = $pdo->prepare($sqlEditPostPreFills);
                $stmtEditPost->execute();   
                while ($row = $stmtEditPost->fetch(PDO::FETCH_ASSOC)) {
                    $editPost_Title = $row['post_title'];
                    $editPost_Content = $row['content'];
                    $editPostId = $row['id'];
                }    

                $content = <<<EOD
                <form action="?profile&editPost="$editPostId" method="post">
            <h1>Edit Post</h1>
            <div class="form-group">
              <input name="Epost_title" type="text" id="Epost_title" value="$editPost_Title" class="form-control">
            </div>
                        <div class="form-group">
              <textarea class="form-control" name="Epost_content" id="Epost_content" value="" rows="6">$editPost_Content</textarea>
            </div>
            <div style="text-align: right;">
              <button type="submit" name="update" style="width: 30%;" class="btn btn-success btn-lg">Update</button>
            </div>
                </form>
            <hr />
EOD;

        } // end IF ableToEdit
        $updatedContent = "";
            if(isset($_POST['Epost_content'])) { $updatedContent = $_POST['Epost_content']; }

    $updatedTitle = "";
            if(isset($_POST['Epost_title'])) { $updatedTitle = $_POST['Epost_title']; }

    if(isset($_POST['Epost_content']) && isset($_POST['Epost_title'])) {
    $sqlUpdatePost = <<<EOD
    UPDATE posts SET post_title='$updatedTitle', content='$updatedContent' WHERE posts.id='$editPostId' AND posts.author='$currentUserId';
EOD;
            $stmtUpdate = $pdo->prepare($sqlUpdatePost);
            $stmtUpdate->execute();
                }
    }
    // end edit post 

1 个答案:

答案 0 :(得分:-1)

这条线对我来说很糟糕

<form action="?profile&editPost="$editPostId" method="post">

尝试将其更改为

<form action="?profile&editPost=\"$editPostId\" method=\"post\">"