数据库输入从表单$ Post无法正常工作

时间:2013-12-14 21:15:11

标签: php mysql forms

我正在尝试为我的网站创建一个评论系统,其中文章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>

1 个答案:

答案 0 :(得分:0)

  

首先,您需要更多地了解OOP。我认为您缺乏   OOP如何工作以及如何实例化类

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

  

在这里查看如何使用类

$ addcomment = new Comment($ _ POST);

你不需要这个功能来创建一个对象。

public function storeFormValues( $params ) 

__ construct完成这项工作

public function __construct()