doulingzou1712 2015-08-02 21: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?

$post = isset($_GET['id']) ? $_GET['id'] : '';

if (isset($_GET['id']))
{
    $id = $_GET['id'];
    $sql = "SELECT * FROM posts WHERE id = ?";
    $results = $db->prepare($sql);
    $results->bindValue(1, $id);
    $results->execute();
    $post = $results->fetch(PDO::FETCH_ASSOC);
}

if (isset($_POST['editPostForm']))
{
    $title = $_POST["title"];
    $body = $_POST["body"];
    $id = $_GET['id'];

    $stmt = $db->prepare("UPDATE posts SET title = ?, body = ? WHERE id = ?");
    $stmt->bindParam(1, $title);
    $stmt->bindParam(2, $body);
    $stmt->bindParam(3, $id);
    $stmt->execute(); 

    header("Location: posts");
}

$twigContext = array(
    "post" => $post
);

echo $twig->render('edit.html.twig', $twigContext);

HTML File:

  <div class="wrapper">
    <form method="post" action="edit.php" class="editComposeForm">
      <h2>Edit Post</h2>
      <input type="text" value="{{ post.title }}" name="title"><br>
      <textarea name="body">{{ post.body }}</textarea><br>
      <input type="submit" value="Update" name="editPostForm">
    </form>
  </div>
  • 写回答

2条回答 默认 最新

  • dongqiong8021 2015-08-02 21: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:

      <div class="wrapper">
        <form method="post" action="edit.php" class="editComposeForm">
    
          <input type="hidden" value="{{ post.id }}" name="id">
    
          <h2>Edit Post</h2>
          <input type="text" value="{{ post.title }}" name="title"><br>
          <textarea name="body">{{ post.body }}</textarea><br>
          <input type="submit" value="Update" name="editPostForm">
        </form>
      </div>
    

    And in the php

    $id = $_POST['id'];
    

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

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

    And in the php

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

报告相同问题?

悬赏问题

  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的