回显函数和编辑永久链接

时间:2015-09-29 23:02:46

标签: php .htaccess echo

我想在我的功能中做一些改变

    <?php

function add_post($title,$contents,$category){
    $title      = mysql_real_escape_string($title);
    $contents   = mysql_real_escape_string($contents);
    $category   = (int)$category;

    mysql_query("INSERT INTO `posts` SET
                `cat_id`     = {$category},
                `title`      = '{$title}',
                `contents`   = '{$contents}',
                `date_posted`= NOW()");
}

function edit_post($id,$title,$contents,$category){
    $id         = (int)$id;
    $title      = mysql_real_escape_string($title);
    $contents   = mysql_real_escape_string($contents);
    $category   = (int)$category;

    mysql_query("UPDATE `posts` SET
                `cat_id`     = {$category},
                `title`      = '{$title}',
                `contents`   = '{$contents}'
                WHERE `id` = {$id}");  
}

function add_category($name){
  $name = mysql_real_escape_string($name);

  mysql_query("INSERT INTO `categories` SET `name` = '{$name}'");
}

function delete($table, $id){
    $table = mysql_real_escape_string($table);
    $id    = (int)$id;

    mysql_query("DELETE FROM `{$table}` WHERE `id`= {$id} ");

}

function get_posts($id = null, $cat_id = null){
    $posts = array();
    $query = "SELECT
              `posts`.`id` AS `post_id` ,
               `categories`.`id` AS `category_id`,
               `title`,`contents`,`date_posted`,
               `categories`.`name`
               FROM `posts`
               INNER JOIN `categories` ON `categories`.`id` = `posts`.`cat_id` " ;
    if(isset($id)){
        $id = (int)$id;
        $query .= " WHERE `posts`.`id` = {$id} ";
             }
    if(isset($cat_id)){
        $cat_id = (int)$cat_id;
        $query .= " WHERE `cat_id` = {$cat_id} ";
             }         

    $query .= "ORDER BY `post_id` DESC";

    $query = mysql_query($query);

    while($row = mysql_fetch_assoc($query)){
    $posts[] = $row;
   }
   return $posts;
}

function get_categories($id = null){
   $categories = array();

   $query = mysql_query("SELECT `id`,`name` FROM `categories`");

   while($row = mysql_fetch_assoc($query)){
    $categories[] = $row;
   }

   return $categories;
}

function category_exists($field,$name){
    $name = mysql_real_escape_string($name);
    $field = mysql_real_escape_string($field);

    $query = mysql_query("SELECT COUNT(1) FROM categories WHERE `{$field}` = '{$name}'");

    return(mysql_result($query,0) == 0)?false : true;
} 

我想获得像domain.com/watch/(movie_name)/episode-x /

这样的永久链接

我认为我只需要.htaccess不能正常工作的永久链接功能? (功能不是我的,所以我想知道它好吗?它安全吗?

1 个答案:

答案 0 :(得分:0)

在这种情况下,假设.htaccess部分正在工作,你只是在进行字符串连接以从部分生成永久链接。

function generate_permalink($cat_name, $id = null)
{
    $permalink = "http://localhost/blog/watch/$cat_name";
    if ($id) {
        $permalink = $permalink . '/episode/' . str_pad($id, 3, '0', STR_PAD_LEFT);
    }
    return $permalink;
}