首页 > 解决方案 > 添加数据时,数据库表会混淆

问题描述

我想在我的项目中添加 crud 功能,但我遇到了一个问题。我可以通过网站添加帖子,但在数据库中,表格包含应该在不同行中的信息,请参见图像。我已经设法使作者和标题显示在正确的行中,但我不能对 post_date 和 last_update 做同样的事情。如您所见,我的帖子的图像部分(我刚刚输入图像进行测试)存储在专用于 last_update 数据的列中。无论出于何种原因,我的帖子的内容都无处可寻。

我还添加了让我在下面创建帖子的代码。我需要这个才能添加其余的 crud 功能,但我不知道如何解决这个问题。

<?php

$msg = '';
// Check if POST data is not empty
if (!empty($_POST)) {
// Post data not empty insert a new record
// Set-up the variables that are going to be inserted, we must check if the POST variables exist if 
not we can default them to blank
$id = isset($_POST['id']) && !empty($_POST['id']) && $_POST['id'] != 'auto' ? $_POST['id'] : NULL;
// Check if POST variable "name" exists, if not default the value to blank, basically the same for 
all variables
$author = isset($_POST['author']) ? $_POST['author'] : '';
$title = isset($_POST['title']) ? $_POST['title'] : '';
$content = isset($_POST['content']) ? $_POST['content'] : '';
$post_date = isset($_POST['post_date']) ? $_POST['post_date'] : date('Y-m-d H:i:s');
$post_update = isset($_POST['post_update']) ? $_POST['post_update'] : date('Y-m-d H:i:s');
$image = isset($_POST['image']) ? $_POST['image'] : '';
// Insert new record into the pages table
$stmt = $conn->prepare('INSERT INTO pages VALUES (?, ?, ?, ?, ?, ?, ?)');
$stmt->execute([$id, $author, $title, $content, $post_date, $post_update, $image]);
// Output message
$msg = 'Created Successfully!';
}
?>


<div class="content update">
<h2>Create Post</h2>
<form action="create.php" method="post">
    <label for="id">ID</label>
    <input type="text" name="id" placeholder="26" value="auto" id="id">
    <label for="name">Content</label>
    <input type="text" name="auhtor" placeholder="Content" id="author">
    <label for='content'>Author</label>
    <input type='text' name='content' placeholder='username' id='content'>
    <label for='content'>Title</Title></label>
    <input type='text' name='title' placeholder='Title' id='title'>
    <label for="created">Post Update</label>
    <input type="datetime-local" name="post_update" value="<?=date('Y-m-d\TH:i')?>" id="post_update">
    <label for="created">Post Date</label>
    <input type="datetime-local" name="post_date" value="<?=date('Y-m-d\TH:i')?>" id="post_date">
    <label for="title">Image</label>
    <input type="text" name="image" placeholder="Image" id="image">
    <input type="submit" value="Create">
</form>
<?php if ($msg): ?>
<p><?=$msg?></p>
<?php endif; ?>
</div>

标签: phphtmldatabasepdo

解决方案


推荐阅读