0
我正在嘗試爲我的網站創建一個評論系統,其中文章ID保存在評論表中,這樣當每篇評論知道該文章被調用時應該去哪裏。我已經得到它,以便通過從數據庫中拉出來正確顯示評論。但是,我在獲取它時遇到問題,因此用戶可以輸入註釋並將其保存在數據庫中。在我點擊提交按鈕後,表單中的所有信息似乎都不會保存在數據庫中的任何地方。我已經運行了SQL命令並且它可以正常工作。我有一種感覺,它在構造或形式本身的某個位置。以下是評論系統用於上傳評論的所有代碼段。如果有人能幫我弄清楚這個錯誤,將不勝感激。
在comment.php:
public function __construct($data=array()) {
if (isset($data['id'])) $this->id = (int) $data['id'];
if (isset($data['publicationDate'])) $this->publicationDate = (int) $data['publicationDate'];
if (isset($data['title'])) $this->title = preg_replace ("/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['title']);
if (isset($data['content'])) $this->content = $data['content'];
if (isset($data['articleid'])) $this->articleid = (int) $data['articleid'];
}
public function storeFormValues($params) {
// Store all the parameters
$this->__construct($params);
// Parse and store the publication date
if (isset($params['publicationDate'])) {
$publicationDate = explode ('-', $params['publicationDate']);
if (count($publicationDate) == 3) {
list ($y, $m, $d) = $publicationDate;
$this->publicationDate = mktime (0, 0, 0, $m, $d, $y);
}
}
}
public function insert($art) {
// Insert the comment
$conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
$sql = "INSERT INTO comments (publicationDate, title, content, articleid) VALUES (FROM_UNIXTIME(:publicationDate), :title, :content, :art)";
$st = $conn->prepare ($sql);
$st->bindValue(":publicationDate", $this->publicationDate, PDO::PARAM_INT);
$st->bindValue(":title", $this->title, PDO::PARAM_STR);
$st->bindValue(":content", $this->content, PDO::PARAM_STR);
$st->bindValue(":art", $art, PDO::PARAM_INT);
$st->execute();
$this->id = $conn->lastInsertId();
$conn = null;
}
在index.php中
function viewArticle() {
if (!isset($_GET["articleId"]) || !$_GET["articleId"]) {
homepage();
return;
}
$results = array();
$results['article'] = Article::getById((int)$_GET["articleId"]);
$results['pageTitle'] = $results['article']->title . " | Gaming News";
$craps = array();
$data = Comment::getList((int)$_GET["articleId"]);
$craps['comments'] = $data['craps'];
require(TEMPLATE_PATH . "/viewArticle.php");
}
在viewarticle.php:
<?php
if (isset($_POST['saveChanges'])) {
// User has posted the article edit form: save the new article
$addcomment = new Comment;
$addcomment->storeFormValues($_POST);
$addcomment->insert((int)$_GET["articleId"]);
}
?>
<script>
function closeKeepAlive() {
if (/AppleWebKit|MSIE/.test(navigator.userAgent)) {
var xhr = new XMLHttpRequest();
xhr.open("GET", "/ping/close", false);
xhr.send();
}
}
</script>
<form action="index.php" method="post" enctype="multipart/form-data" onsubmit="closeKeepAlive()">
<ul>
<li>
<label for="title">Name</label>
<input type="text" name="title" id="title" placeholder="Your Name" required autofocus maxlength="255" value="" />
</li>
<li>
<label for="content">Comment</label>
<textarea name="content" id="content" placeholder="The Comment You Want" required maxlength="100000" style="height: 10em;"></textarea>
</li>
<li>
<label for="publicationDate">Publication Date</label>
<input type="date" name="publicationDate" id="publicationDate" placeholder="YYYY-MM-DD" required maxlength="10" value="" />
</li>
<div class="buttons">
<input type="submit" name="saveChanges" value="Comment" />
</div>
我的錯誤是$ addcomment->插入($ tempart);我把$ tempart放進去,它現在可以工作,這是一個簡單的null,它破壞了代碼 – user3102608