doulingzou1712 2015-08-02 13:20 采纳率: 0%
浏览 24
已采纳

php更新查询不会使用pdo更新

I have a edit.php file as seen below. The page shows a blog post via GET['id'] in a form to edit the title and body of that post. When editPostForm is submitted, it should update the database with the new content and redirect back to the /posts/ page. The redirect works, but when viewing the post, nothing has changed about the blog post.

What am I doing wrong?

  1. $post = isset($_GET['id']) ? $_GET['id'] : '';
  2. if (isset($_GET['id']))
  3. {
  4. $id = $_GET['id'];
  5. $sql = "SELECT * FROM posts WHERE id = ?";
  6. $results = $db->prepare($sql);
  7. $results->bindValue(1, $id);
  8. $results->execute();
  9. $post = $results->fetch(PDO::FETCH_ASSOC);
  10. }
  11. if (isset($_POST['editPostForm']))
  12. {
  13. $title = $_POST["title"];
  14. $body = $_POST["body"];
  15. $id = $_GET['id'];
  16. $stmt = $db->prepare("UPDATE posts SET title = ?, body = ? WHERE id = ?");
  17. $stmt->bindParam(1, $title);
  18. $stmt->bindParam(2, $body);
  19. $stmt->bindParam(3, $id);
  20. $stmt->execute();
  21. header("Location: posts");
  22. }
  23. $twigContext = array(
  24. "post" => $post
  25. );
  26. echo $twig->render('edit.html.twig', $twigContext);

HTML File:

  1. <div class="wrapper">
  2. <form method="post" action="edit.php" class="editComposeForm">
  3. <h2>Edit Post</h2>
  4. <input type="text" value="{{ post.title }}" name="title"><br>
  5. <textarea name="body">{{ post.body }}</textarea><br>
  6. <input type="submit" value="Update" name="editPostForm">
  7. </form>
  8. </div>

展开全部

  • 写回答

2条回答 默认 最新

  • dongqiong8021 2015-08-02 13:48
    关注

    The html form does not have an input with the name id that you are attempting to access in the php code. You can add it either as a hidden form element (first example below) or as part of the query string in the action attribute (second example below).

    Add it as a hidden form element:

    1. <div class="wrapper">
    2. <form method="post" action="edit.php" class="editComposeForm">
    3. <input type="hidden" value="{{ post.id }}" name="id">
    4. <h2>Edit Post</h2>
    5. <input type="text" value="{{ post.title }}" name="title"><br>
    6. <textarea name="body">{{ post.body }}</textarea><br>
    7. <input type="submit" value="Update" name="editPostForm">
    8. </form>
    9. </div>

    And in the php

    $id = $_POST['id'];
    

    OR add it to the query string in the action attribute.

    1. <div class="wrapper">
    2. <form method="post" action="edit.php/id={{ post.id }}" class="editComposeForm">
    3. <h2>Edit Post</h2>
    4. <input type="text" value="{{ post.title }}" name="title"><br>
    5. <textarea name="body">{{ post.body }}</textarea><br>
    6. <input type="submit" value="Update" name="editPostForm">
    7. </form>
    8. </div>

    And in the php

    $id = $_GET['id'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部