Home  >  Article  >  Backend Development  >  How to make a simple blog in PHP

How to make a simple blog in PHP

一个新手
一个新手Original
2017-09-08 09:37:178195browse

Recently, I had time to look at some PHP code.

The first is the integrated environment

First pass, phpMyAdminCreate a blogsurface.

Pure interface operation, the process is relatively simple. It should be noted that id is the primary key, and Set the auto_increnent option to indicate that the field will automatically increment when it is empty. Other fields are more casual, just pay attention to the type and length.

Create a data connection                                                                                                     conn.php

file.

<?php
    @mysql_connect("127.0.0.1:3306","root","") or die("mysql数据库连接失败");
    @mysql_select_db("test")or die("db连接失败");mysql_query("set names &#39;gbk&#39;");
?>
 mysqlThe default user name is

root


, the password is empty, the ## created here

#blog

is in the test library, so it needs to be connected to the test library. Addblog                                         

at./wamp/www/blog/ Create the add.php file in the

directory.

<a href="index.php"><B>index</B></a>
<a href="add.php"><B>add blog</B></a>
<hr>
<?php
    include("conn.php"); //引入连接数据库
    if (!empty($_POST[&#39;sub&#39;])) {    
        $title = $_POST[&#39;title&#39;];  //获取title表单内容
        $con = $_POST[&#39;con&#39;];      //获取contents表单内容
        $sql= "insert into blog values(null,&#39;0&#39;,&#39;$title&#39;,now(),&#39;$con&#39;)";    
        mysql_query($sql);    
        echo "insert success!";
}?>
<form action="add.php" method="post">
    title   :<br>
    <input type="text" name="title"><br><br>
    contents:<br>
    <textarea rows="5" cols="50" name="con"></textarea><br><br>
    <input type="submit"  name="sub" value="submit">
</form>
This code is divided into two parts, the upper part is the PHP code,

include ## The
# (or

require) statement will retrieve all text present in the specified file /Code / tag and copy it to a file using the include statement. Then, if it is judged that the content of name='sub' in the form is not empty, the content of the form will be obtained, and then $sql will be executed. statement, null

means id is empty (incremented), now() means taking the current date, $title and $con take the content submitted by the user in the form. Finallyeche prompts for successful insertion. The lower part is a simple HTML code, used to implement a blog form submission Function.

 创建blog的首页                          

./wamp/www/blog/目录下创建index.php文件。

<a href="index.php"><B>index</B></a>
<a href="add.php"><B>add blog</B></a>
<br><br>
<form action="" method="get" style=&#39;align:"right"&#39;>
    <input type="text" name="keys" >
    <input type="submit" name="subs" >
</form>
<hr>
<?php
    include("conn.php"); //引入连接数据库
       if (!empty($_GET[&#39;keys&#39;])) {        
       $key = $_GET[&#39;keys&#39;];        
       $w = " title like &#39;%$key%&#39;";
    }else{        
    $w=1;
    }    
    $sql ="select * from blog where $w order by id desc limit 5";    
    $query = mysql_query($sql);    
    while ($rs = mysql_fetch_array($query)) {
?>
<h2>title: <a href="view.php?id=<?php echo $rs[&#39;id&#39;]; ?>"><?php echo $rs[&#39;title&#39;]; ?></a>
    | <a href="edit.php?id=<?php echo $rs[&#39;id&#39;]; ?>">edit</a> 
    | <a href="del.php?id=<?php echo $rs[&#39;id&#39;]; ?>">delete</a> |
</h2>
<li>date: <?php echo $rs[&#39;data&#39;]; ?></li>
<!--截取内容展示长度-->
<p>contents:<?php echo iconv_substr($rs[&#39;contents&#39;],0,30,"gbk"); ?>...</p>  
<hr>
<?php
};?>

  该页面包含有的功能还是比较多的。

  首先是一个搜索表单,通过if判断搜索表单的内容是否为空,如果不为空,通过输入关键字匹配文章的标题并显示结果;如果为空查询所有blog内容,并循环显示每一篇文章的标题、日期、正文。点击标题会链接到该篇blog的详细页面。每一篇文章提供“编辑”和“删除”功能。

  mysql_query()用于执行sql语句。mysql_fetch_arry()将返回的数据生成数组,这样就可以像操作数组一样,操作数据库中的每一条数据了。

  然后是正文的显示,通过 iconv_substr() 函数提取正文前30个字符。

 查看blog                                                                                                             

./wamp/www/blog/目录下创建view.php文件。

<a href="index.php"><B>index</B></a>
<a href="add.php"><B>add blog</B></a>
<hr>
<?php
include("conn.php"); //引入连接数据库
    if (!empty($_GET[&#39;id&#39;])) {        
    $id = $_GET[&#39;id&#39;];        
    $sql ="select * from blog  where id=&#39;$id&#39; ";    
        $query = mysql_query($sql);        
        $rs = mysql_fetch_array($query);        
        $sqlup = "update blog set hits=hits+1 where id=&#39;$id&#39;";        
        mysql_query($sqlup);
    }?>
<h2>title: <?php echo $rs[&#39;title&#39;];?> </h2>
<h3>date: <?php echo $rs[&#39;data&#39;];?>  
click number: <?php echo $rs[&#39;hits&#39;]; ?></h3>
<hr>
<p>contents:<?php echo $rs[&#39;contents&#39;]; ?></p>

   blog的正文实现比较简单,通过get请求获取blogid,然后通过sql语句将该id对应的标题、日期和正文查询出来并显示。

  并外一个小功能是显示了一个简单的计数器,每刷新页面,点击数加1

 编辑blog                                                                                                         

./wamp/www/blog/目录下创建edit.php文件。

<a href="index.php"><B>index</B></a>
<a href="add.php"><B>add blog</B></a>
<hr>
<?php
    include("conn.php"); //引入连接数据库
//获取数据库表数据if (!empty($_GET[&#39;id&#39;])) {    
    $edit = $_GET[&#39;id&#39;];    
    $sql = "select * from blog where id=&#39;$edit&#39;";    
    $query = mysql_query($sql);    
    $rs = mysql_fetch_array($query);
}//更新数据库表数据if (!empty($_POST[&#39;sub&#39;])) {    
        $title = $_POST[&#39;title&#39;];  //获取title表单内容
    $con = $_POST[&#39;con&#39;];      //获取contents表单内容
    $hid = $_POST[&#39;hid&#39;]; 
    $sql= "update blog set title=&#39;$title&#39;, contents=&#39;$con&#39; where id=&#39;$hid&#39; ";    
    mysql_query($sql);    
    echo "<script>alert(&#39;update success.&#39;);location.href=&#39;index.php&#39;</script>";
}?>
<form action="edit.php" method="post">
    <input type="hidden" name="hid" value="<?php echo $rs[&#39;id&#39;];?>">
    title   :<br>
    <input type="text" name="title" value="<?php echo $rs[&#39;title&#39;];?>">
    <br><br>
    contents:<br>
    <textarea rows="5" cols="50" name="con" ><?php echo $rs[&#39;contents&#39;];?></textarea><br><br>
    <input type="submit"  name="sub" value="submit">
</form>

   编辑blog的功能相对复杂一些。分两部操作,第一步先将blog的标题和正文查询出来,并显示到输入框。第二步将编辑好的内容再更新到数据库中。

 删除blog                                                                                                  

./wamp/www/blog/目录下创建del.php文件。

<a href="index.php"><B>index</B></a>
<a href="add.php"><B>add blog</B></a>
<hr>
<?php    
    include("conn.php"); //引入连接数据库
    if (!empty($_GET[&#39;id&#39;])) {        
             $del = $_GET[&#39;id&#39;];  //删除blog
        $sql= "delete from blog where id=&#39;$del&#39; ";        
        mysql_query($sql);        
        echo "delete success!";
}?>

   最后是实现blog的删除功能,通过id将该条blog的查询出来并显示。

 因为所有页面没有使用前端样式有美化,很丑就不贴图了。功能还算完美。在此记录,算做PHP学习的整理。

=======================================================

另外,虽然每个语言都有优缺点,这里还是忍不住要吐槽一下PHP的两个不好之处。

1、符号不好写, “$” 、“ ->” 、 “=>”。这些符号虽然并没有增加代码语法的理解难度。但敲起来具恶心。每次在打“$”符号的时候,都要眼看键盘按着shift键找4在哪儿。

2、phphtml的混编在我看来也不是太优雅。

The above is the detailed content of How to make a simple blog in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn