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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line