我在使用PDO更新数据库中的记录时遇到问题。我没有收到任何错误,但是记录未在db中更新。我可以毫无问题地插入和删除记录。我只是无法使用下面的代码和关联的表单来更新现有记录。
这是我正在使用的代码。
<?php
require '../app/start.php';
if (!empty($_POST)){
$id = $_POST['id'];
$label = $_POST['label'];
$title = $_POST['title'];
$slag = $_POST['slag'];
$body = $_POST['body'];
$updatePage = $db->prepare("
UPDATE pages
SET
label = :label,
title = :title,
body = :body,
slag = :slag,
updated = :NOW()
WHERE
id = :id
");
$updatePage -> execute([
'id' => $id,
'label' => $label,
'title' => $title,
'body' => $body,
'slag' => $slag,
]);
header('Location: ' . BASE_URL . '/admin');
}
if (!isset($_GET['id'])){
header('Location: ' . BASE_URL . '/admin');
die();
}
$page = $db->prepare("
SELECT id, title, label, body, slag
FROM pages
WHERE id = :id
");
$page->execute(['id' => $_GET['id']]);
$page = $page->fetch(PDO::FETCH_ASSOC);
//var_dump($page);
require VIEW_ROOT . '/admin/edit.php';
编辑表格代码
<form action = "<?echo BASE_URL; ?>/admin/edit.php" method = "POST"
autocomplete="off">
<label for = "title">
Title
<input type ="text" name ="title" id= "title"
value ="<?php echo e($page['title']); ?>">
</label>
<label for = "label">
Label
<input type ="text" name ="label" id= "label"
value ="<?php echo e($page['label']); ?>">
</label>
<label for = "slag">
Slug
<input type ="text" name ="slag" id= "slag"
value ="<?php echo e($page['slag']); ?>">
</label>
<label for = "body">
Body
<textarea name="body" id="body" cols="30" rows="18">
<?php echo e($page['body']);?></textarea>
</label>
<input type="hidden" name="id" value=
"<?php echo e($page['id']);?>">
<input type="submit" value="Edit">
</form>
答案 0 :(得分:0)
在查询中,您不必使用:NOW()
,而只需使用NOW()