我很困惑。有些东西在我设计的新CMS中不起作用。我正在学习本教程,但正在改变一些东西(包括使用SQLite而不是MySQL)并添加更多功能。 http://www.elated.com/articles/cms-in-an-afternoon-php-mysql/由PHP在SQL数据库中未自动分配的自动增量ID
无论如何,几乎所有的东西现在都可以使用,但它不会保存新帖子。尽管编辑帖子功能仍然有效。
这是基础的形式(即正常工作编辑后相同的形式):
<h3>Create a blog post</h3>
<form method="POST" action="index.php?action=<?php echo $posts['formAction']; ?>">
<input type="hidden" id="id" name="id" value="<?php echo $posts['post'] -> id; ?>" />
<p>Title: <input id="title" name="title" type="text" placeholder="Enter the post title" required autofocus maxlength="255" value="<?php echo $posts['post'] -> title; ?>"/></p>
<p>Category: <input id="category" name="category" type="text" placeholder="Enter the post category" required maxlength="255" value="<?php echo $posts['post'] -> category; ?>"/></p>
<p>Tags: <input id="tags" name="tags" type="text" placeholder="Enter some tags for the post" required value="<?php echo $posts['post'] -> tags; ?>"/></p>
<p>Summary:</p><textarea id="summary" name="summary" cols="100" rows="5" placeholder="Enter a short summary of the post's content" required><?php echo $posts['post'] -> summary; ?></textarea>
<p>Content:</p><textarea id="body" name="body" cols="100" rows="30" placeholder="Enter the main content of the post" required><?php echo $posts['post'] -> body; ?></textarea>
<p>Publication date: </p><input type="date" id="pubDate" name="pubDate" placeholder="DD-MM-YYYY" required maxlength="10" value="<?php echo $posts['post'] -> pubDate ? date('Y-m-d', $posts['post'] -> pubDate) : ''; ?>" />
<br />
<input id="saveChanges" name="saveChanges" type="submit" value="Create Post"/>
</form>
这最终发布到下面的函数中的index.php:
function newPost()
{
$posts = array();
$posts['formAction'] = "newPost";
if (isset($_POST['saveChanges']))
{
$post = new Post;
$post -> storePostValues($_POST);
$post -> insertPost();
header("Location: index.php?status=changesSaved");
}
elseif (isset($_POST['cancel']))
{
header('Location: index.php');
}
else
{
$posts['post'] = new Post;
require('editPost.php');
}
}
而且最后是Post()类中的相关代码:
public function __construct($data = array()) {
if (isset($data['id'])) $this -> id = (int) $data['id'];
if (isset($data['title'])) $this -> title = $data['title'];
if (isset($data['pubDate'])) $this -> pubDate = (int) $data['pubDate'];
if (isset($data['reviseDate'])) $this -> reviseDate = (int) $data['reviseDate'];
if (isset($data['category'])) $this -> category = $data['category'];
if (isset($data['tags'])) $this -> tags = $data['tags'];
if (isset($data['summary'])) $this -> summary = $data['summary'];
if (isset($data['body'])) $this -> body = $data['body'];
if (isset($data['views'])) $this -> views = (int) $data['views'];
if (isset($data['likes'])) $this -> likes = (int) $data['likes'];
if (isset($data['dislikes'])) $this -> dislikes = (int) $data['dislikes'];
}
//Sets object properties from form POST values
public function storePostValues($params) {
$this -> __construct($params);
if (isset($params['pubDate'])) {
$pubDate = explode('-', $params['pubDate']);
if (count($pubDate) == 3) {
list ($d, $m, $y) = $pubDate;
$this -> pubDate = mktime(0, 0, 0, $m, $d, $y);
}
}
if (isset($params['reviseDate'])) {
$reviseDate = explode('-', $params['reviseDate']);
if (count($reviseDate) == 3) {
list ($d, $m, $y) = $reviseDate;
$this -> reviseDate = mktime(0, 0, 0, $m, $d, $y);
}
}
}
public function insertPost() {
if (!is_null($this -> id)) trigger_error("Post::insertPost(): Post ID already assigned.", E_USER_ERROR);
$dblocat = "sqlite:" . $_SERVER['DOCUMENT_ROOT'] . "/cgi-bin/blog.db";
$dbcon = new PDO($dblocat);
$dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sqlstr = "INSERT INTO posts(title, pubDate, reviseDate, category, tags, summary, body, views, likes, dislikes) VALUES(:title, FROM_UNIXTIME(:pubDate), FROM_UNIXTIME(:reviseDate), :category, :tags, :summary, :body, :views, :likes, :dislikes)";
$preparedstr = $dbcon -> prepare($sqlstr);
$preparedstr -> bindValue(":title", $this -> title, PDO::PARAM_STR);
$preparedstr -> bindValue(":pubDate", $this -> pubDate, PDO::PARAM_INT);
$preparedstr -> bindValue(":reviseDate", $this -> reviseDate, PDO::PARAM_INT);
$preparedstr -> bindValue(":category", $this -> category, PDO::PARAM_STR);
$preparedstr -> bindValue(":tags", $this -> tags, PDO::PARAM_STR);
$preparedstr -> bindValue(":summary", $this -> summary, PDO::PARAM_STR);
$preparedstr -> bindValue(":body", $this -> body, PDO::PARAM_STR);
$preparedstr -> bindValue(":views", 0, PDO::PARAM_INT);
$preparedstr -> bindValue(":likes", 0, PDO::PARAM_INT);
$preparedstr -> bindValue(":dislikes", 0, PDO::PARAM_INT);
$preparedstr -> execute();
$this -> id = $dbcon -> lastInsertId();
$dbcon = null;
}
那么会发生什么?那么,每次我保存一个帖子,说insertPost()函数中的错误被触发,说帖子ID已经存在......但是它被设置为在数据库中自动递增,所以我不太确定发生了什么。 ..我对PHP比较陌生,现在花了好几个小时试图了解为什么这不起作用。
任何意见,将不胜感激,我可以发送更多的代码,如果需要的话。 再次,我基于本教程,但我已经修改它,现在使用SQLite。 http://www.elated.com/articles/cms-in-an-afternoon-php-mysql/
由于提前, Ilmiont
这工作,但现在它似乎并没有检索主在我的getPosts()函数中,虽然getPostByID($ id)指定了ID,但数据库中的“id”列仍然有效。这意味着我的链接从主页使用href =“viewPost.php?postID = <?php echo $ post - > id;?>”不起作用...但是像$ post - > title这样的事情不是这样再次检索该列。建议? – Ilmiont 2014-10-11 15:02:09
我对此有个想法。你能编辑你的文章并添加你的函数insert()的代码,所以我可以帮忙吗? – Yoric 2014-10-11 15:17:09
非常感谢;我修改了取消设置帖子ID的方法,使得所有内容都可以正常工作。谢谢! – Ilmiont 2014-10-11 16:27:40