无法使用php mysql pdo更新db记录

kkih6yb8  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(318)

我无法使用pdo更新数据库中的记录。我没有得到任何错误,但记录没有在数据库中更新。我可以插入和删除记录没有任何问题。我只是无法使用下面的代码和相关表单更新现有记录。
这是我正在使用的代码。

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

au9on6nz1#

在查询中,您不必使用 :NOW() 但只是 NOW()

相关问题