【问题标题】:Update Statement mySQL issues更新语句 mySQL 问题
【发布时间】:2016-09-03 08:40:54
【问题描述】:

当用户更新他们发表的博客文章时,我无法更新我的数据库的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 

【问题讨论】:

    标签: php html mysql pdo forms


    【解决方案1】:

    这条线不适合我

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

    试着改成

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

    【讨论】:

    • 这仍然没有更新数据库。老实说,我不确定我希望表单的操作是什么,也不确定表单的操作是否会导致更新......不过谢谢!
    猜你喜欢
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 2011-08-01
    • 2010-11-21
    相关资源
    最近更新 更多