简体   繁体   English

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

[英]php update query won't update using pdo

I have a edit.php file as seen below. 我有一个edit.php文件,如下所示。 The page shows a blog post via GET['id'] in a form to edit the title and body of that post. 该页面以GET ['id']格式显示博客文章,以编辑该文章的标题和正文。 When editPostForm is submitted, it should update the database with the new content and redirect back to the /posts/ page. 提交editPostForm时,它应使用新内容更新数据库,然后重定向回/ posts /页面。 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: HTML档案:

  <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>

The html form does not have an input with the name id that you are attempting to access in the php code. html表单没有输入名称,您试图在php代码中访问该名称。 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 并在PHP中

$id = $_POST['id'];

OR add it to the query string in the action attribute. 或将其添加到action属性中的查询字符串中。

  <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 并在PHP中

$id = $_GET['id'];

表单没有输入id ,您的id'' ,这是它不会更新的原因。在表单中,请在action="edit.php?id=somevalue"设置ID,例如<form method="post" action="edit.php?id={{ post.id }}" class="editComposeForm">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM