目前,我正在尝试创建一个新闻系统,在该系统中,我使用ckeditor编写一篇文章,在发布之后,该字段将输入保存在mysql表中,在索引页上显示已发布的新闻。我能够获取数据,这样它就可以显示在索引页上,并且可以正常工作。我就是这样做的:
<div class="panel-body">
<?php
$db = getDB();
$stmt = $db->prepare("SELECT * FROM news ORDER BY created_at DESC;");
$stmt->execute();
while($data = $stmt->fetch(PDO::FETCH_ASSOC)) {
$title = $data['title'];
$content = $data['content'];
$date = $data['created_at'];
$admin = (int)$data['admin'];
$id = (int)$data['id'];
$admin_data = new User($admin);
$admin_name = $admin_data->getUsername();
print("<div class='timeline-panel'>");
print("<div class='timeline-heading'>");
print ("<h4 class='timeline-title'><b>$title</b></h4>");
print ("<p><small class='text-muted'><i class='fa fa-clock-o'></i> $date $admin_name</small></p>");
print("</div>");
print("<div class='timeline-body'>");
print("<p>$content</p>");
print("</div>");
print("</div>");
print("</li>");
print("<hr style='height:1px;border-top:2px solid #aa0000'>");
}
?>
</div>
现在为了能够发布新闻,我尝试了使用ckeditor并起草了这个,但它根本没有保存字段中的值。;
<script src="/ugrp/ckeditor/ckeditor.js"></script>
<div id="wrapper">
<div id="page-wrapper">
<div class="row">
<div class="col-lg-12">
</div>
<!-- /.col-lg-12 -->
</div>
<div class="col-md-12" style="padding: 20px 15px;">
<?php
if(isset($_POST['submit'])) {
$db = getDB();
$stmt = $db->prepare("INSERT INTO `news`(`id`, `admin`, `content`, `title`, `created_at`) VALUES (:title, :content, :created_at, :admin, :id)");
$stmt->bindParam('title', $title, PDO::PARAM_STR);
$stmt->bindParam('content', $content, PDO::PARAM_INT);
$stmt->bindParam('created_at', $created_at, PDO::PARAM_INT);
$stmt->bindParam('admin', $admin, PDO::PARAM_INT);
$stmt->bindParam('id', $id, PDO::PARAM_INT);
$stmt->execute();
}
?>
<form>
<div class="form-group">
<input name="title" type="text" id="title" class="form-control" placeholder="Title of the News"><br>
</div>
<div class="form-group">
</div>
<textarea name="content" id="content">
</textarea>
<script>
CKEDITOR.replace( 'content' );
</script>
<center><button type="submit" name="submit" class="btn bg-red btn-block"><i class="fa fa-pencil"></i> Submit</button></center>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
我只想能够发布的新闻,这将保存在上面提到的表中的代码。
我确信我在发布代码中犯了很多错误,这就是为什么任何帮助都会被感激的原因。
这就是它在索引页上显示新闻时的样子。注:图中所示数值是在表中手动添加的。
1条答案
按热度按时间syqv5f0l1#
在新闻表中,将id字段更改为主键并自动递增。
试试这个: