php - 表单 $Post 中的数据库输入无法正常工作

标签 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>

最佳答案

First of all you need to study more about OOP.I think you are lacking how the OOP works and how to instantiate classes

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

check here how to work with classes

$addcomment = 新评论($_POST);

您不需要此函数来创建对象。

public function storeFormValues( $params ) 

__construct 完成这项工作

public function __construct() 

关于php - 表单 $Post 中的数据库输入无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20588348/

相关文章:

php - WordPress 查询数据库,用于一次查询中每个用户的帖子计数

mysql - 使用 MySQL Workbench CE 导出数据库的数据字典?

css - 你能改变选择下拉箭头的背景颜色吗?

asp.net-mvc - ASP.NET MVC - 使用相同的表单来创建和编辑

图像/表单到 Pascal/Delphi 代码转换器?

php - mysql 组和过滤结果

javascript - Highstock 与 php 和 mysql

php - 如何修复phpmyadmin的 'Access Denied'消息?

mysql - 在mysql查询中提及database.table.field总是失败

php - 在 select mysql 上连接数据和文本