php - $URL 值未添加到 mySQL db PHP

标签 php mysql seo

这是我的代码:

<?php

/**
 * Class to handle articles
 */

class Article
{
// Properties

/**
* @var int The article ID from the database
*/
public $id = null;

/**
* @var int When the article is to be / was first published
  */
public $publicationDate = null;

/**
* @var string Full title of the article
*/
public $title = null;

/**
* @var string A short summary of the article
*/
public $summary = null;

/**
* @var string The HTML content of the article
*/
 public $content = null;


/**
* Sets the object's properties using the values in the supplied array
*
* @param assoc The property values
*/

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['summary'] ) ) $this->summary = preg_replace ( "/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['summary'] );
if ( isset( $data['content'] ) ) $this->content = $data['content'];
}


/**
* Sets the object's properties using the edit form post values in the supplied array
*
* @param assoc The form post values
*/

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;
  }
 }
}


/**
 * Returns an Article object matching the given article ID
*
* @param int The article ID
* @return Article|false The article object, or false if the record was not found or there was a problem
*/

public static function getById( $id ) {
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "SELECT *, UNIX_TIMESTAMP(publicationDate) AS publicationDate FROM articles WHERE id = :id";
$st = $conn->prepare( $sql );
$st->bindValue( ":id", $id, PDO::PARAM_INT );
$st->execute();
$row = $st->fetch();
$conn = null;
if ( $row ) return new Article( $row );
}


/**
* Returns all (or a range of) Article objects in the DB
*
* @param int Optional The number of rows to return (default=all)
* @param string Optional column by which to order the articles (default="publicationDate ASC, id ASC")
* @return Array|false A two-element array : results => array, a list of Article objects; totalRows => Total number of articles
*/

public static function getList( $numRows=1000000, $order="publicationDate DESC, ID DESC" ) {
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "SELECT SQL_CALC_FOUND_ROWS *, UNIX_TIMESTAMP(publicationDate) AS publicationDate FROM articles
        ORDER BY " . mysql_escape_string($order) . " LIMIT :numRows";

$st = $conn->prepare( $sql );
$st->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
$st->execute();
$list = array();

while ( $row = $st->fetch() ) {
  $article = new Article( $row );
  $list[] = $article;
}

// Now get the total number of articles that matched the criteria
$sql = "SELECT FOUND_ROWS() AS totalRows";
$totalRows = $conn->query( $sql )->fetch();
$conn = null;
return ( array ( "results" => $list, "totalRows" => $totalRows[0] ) );
}


/**
* Inserts the current Article object into the database, and sets its ID property.
*/

public function insert() {

// Does the Article object already have an ID?
if ( !is_null( $this->id ) ) trigger_error ( "Article::insert(): Attempt to insert an Article object that already has its ID property set (to $this->id).", E_USER_ERROR );

$preURL = $data['title'];

$preURL2 = preg_replace('/[^a-z0-9]/i',' ', $preURL);
$preURL3 = str_replace(" ","-", $preURL2);
$url = $date.'/'.$preURL3.'.html';

// Insert the Article
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "INSERT INTO articles ( publicationDate, title, summary, content, url) 
       VALUES ( FROM_UNIXTIME(:publicationDate), :title, :summary, :content, :url )";
$st = $conn->prepare ( $sql );
$st->bindValue( ":publicationDate", $this->publicationDate, PDO::PARAM_INT );
$st->bindValue( ":title", $this->title, PDO::PARAM_STR );
$st->bindValue( ":summary", $this->summary, PDO::PARAM_STR );
$st->bindValue( ":content", $this->content, PDO::PARAM_STR );
$st->bindValue( ":url", $this->url, PDO::PARAM_STR ); 
$st->execute();
$this->id = $conn->lastInsertId();
$conn = null;
}

?>

这并没有像我希望的那样将精简版的“titl”添加到 Url 行。难道我做错了什么?我在某处错误地定义了变量吗?我没有收到任何错误 - 只是在 url 行中留下了空值。

谢谢。

最佳答案

在您的 insert() 函数中,您正在绑定(bind) $this->url,它可能只是 $url。以及下一行,

$preURL = $data['title'];

也应该改为,

$preURL = $this->title;

关于php - $URL 值未添加到 mySQL db PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20157256/

相关文章:

php - Yii 中的 afterAction 缺陷

mysql - 为什么这个简单的查询要花很长时间?

ruby-on-rails - Rails 新闻过滤器 url seo

php - 我如何记录每个机器人(谷歌、雅虎等)访问我的网站

jquery - 在 ASP.NET MVC 中延迟渲染阻塞 CSS 包

php - Laravel 队列 - 如何设置 FAST 处理器

javascript - 在 JavaScript 中解码 url 时遇到问题

php - JSON 预览在 Chrome 开发工具中不起作用

php - 注意:未定义索引:已记录

MySQL case语句错误,部分查询数据未显示