2013-12-14 47 views
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> 

回答

0

第一O形女不限,你需要学习更多关于OOP.I认为你缺乏 的OOP是如何工作的,以及如何在此实例类

http://www.php.net/manual/en/language.oop5.php

检查如何使用类工作

$ addcomment = new Comment($ _ POST);

你不需要这个函数来创建一个对象。

public function storeFormValues($params) 

__construct做这件工作

public function __construct() 
+0

我的错误是$ addcomment->插入($ tempart);我把$ tempart放进去,它现在可以工作,这是一个简单的null,它破坏了代码 – user3102608