PHP在表单提交后仍然给我旧数据[关闭]

问题描述 投票:-2回答:1
我有用于更新类别表中某些数据的表格。当我通过表单发布新数据后,页面刷新,但仍向我显示旧数据。

public function updateCategory($status, $main_category, $title, $bg_color, $meta_title, $meta_description, $meta_keywords, $id) { $sql = "UPDATE categories SET status = ?, main_category = ? , title = ?, bg_color = ?, meta_title = ?, meta_description = ?, meta_keywords = ? WHERE id = ?"; $stmt = $this->conn->prepare($sql); try { $stmt->execute([$status, $main_category, $title, $bg_color, $meta_title, $meta_description, $meta_keywords, $id]); return true; } catch (Exception $e) { echo $e->getMessage(); return false; } }

表格发布区域

$category = new Categories(); $id = $_GET['id']; $query = $category->getCategoryDetails($id); $result = false; if (isset($_POST['categorySave'])) { $result = $category->updateCategory($_POST['status'],$_POST['main_category'],$_POST['title'],$_POST['bg_color'],$_POST['meta_title'],$_POST['meta_description'], $_POST['meta_keywords'], $query->id); } ?> <form action="<?=$_SERVER['REQUEST_URI'];?>" method="post" enctype="multipart/form-data"> <?php if ($result) { ?> <div class="alert alert-success" role="alert"> <?=UPDATESUCCESS?> </div> <?php } ?> <label for="title">Başlık</label> <input type="text" class="form-control" id="title" name="title" placeholder="Başlık" value="<?=$query->title?>"> ...

其更新数据没有问题。例如,如果我更改标题“ Title New”,它将在数据库上更新。但是在表单提交后,它保持不变。 “旧标题”
php mysql pdo
1个回答
2
投票
将更新代码移到获取代码上方。

$result = false; if (isset($_POST['categorySave'])) { $result = $category->updateCategory($_POST['status'],$_POST['main_category'],$_POST['title'],$_POST['bg_color'],$_POST['meta_title'],$_POST['meta_description'], $_POST['meta_keywords'], $query->id); } $category = new Categories(); $id = $_GET['id']; $query = $category->getCategoryDetails($id); ?> <form action="<?=$_SERVER['REQUEST_URI'];?>" method="post" enctype="multipart/form-data"> <?php if ($result) { ?> <div class="alert alert-success" role="alert"> <?=UPDATESUCCESS?> </div> <?php } ?> <label for="title">Başlık</label> <input type="text" class="form-control" id="title" name="title" placeholder="Başlık" value="<?=$query->title?>"> ...

© www.soinside.com 2019 - 2024. All rights reserved.