In the process of learning PHP, I believe that many people try to develop various functions by themselves. Have you ever used PHP to write a blog? In this article, we will teach you step by step how to use PHP to implement a blog. We hope that through learning, you can write a PHP blog yourself.
First create a blog table through phpMyAdmin.
Pure interface operation, the process is relatively simple. It should be noted that id is the primary key, and the auto_increnent option is set to indicate that Increments when the field is empty. Other fields are more casual, just pay attention to the type and length.
Create data connection .
<?php @mysql_connect("127.0.0.1:3306","root","") or die("mysql数据库连接失败"); @mysql_select_db("test")or die("db连接失败");mysql_query("set names 'gbk'"); ?>
Add blog
Create the add.php file in the ./wamp/www/blog/ directory.
<a href="index.php"><B>index</B></a> <a href="add.php"><B>add blog</B></a> <hr> <?phpinclude("conn.php"); //引入连接数据库if (!empty($_POST['sub'])) { $title = $_POST['title']; //获取title表单内容 $con = $_POST['con']; //获取contents表单内容 $sql= "insert into blog values(null,'0','$title',now(),'$con')"; 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>
Create the homepage of the blog
Create the index.php file in the ./wamp/www/blog/ directory.
<a href="index.php"><B>index</B></a> <a href="add.php"><B>add blog</B></a> <br><br> <form action="" method="get" style='align:"right"'> <input type="text" name="keys" > <input type="submit" name="subs" > </form> <hr> <?phpinclude("conn.php"); //引入连接数据库 if (!empty($_GET['keys'])) { $key = $_GET['keys']; $w = " title like '%$key%'"; }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['id']; ?>"><?php echo $rs['title']; ?></a> | <a href="edit.php?id=<?php echo $rs['id']; ?>">edit</a> | <a href="del.php?id=<?php echo $rs['id']; ?>">delete</a> | </h2> <li>date: <?php echo $rs['data']; ?></li> <!--截取内容展示长度--> <p>contents:<?php echo iconv_substr($rs['contents'],0,30,"gbk"); ?>...</p> <hr> <?php };?>
View blog
Create the view.php file in the ./wamp/www/blog/ directory. <a href="index.php"><B>index</B></a>
<a href="add.php"><B>add blog</B></a>
<hr>
<?phpinclude("conn.php"); //引入连接数据库
if (!empty($_GET['id'])) { $id = $_GET['id']; $sql ="select * from blog where id='$id' ";
$query = mysql_query($sql); $rs = mysql_fetch_array($query);
$sqlup = "update blog set hits=hits+1 where id='$id'"; mysql_query($sqlup);
}?>
<h2>title: <?php echo $rs['title']; ?> </h1>
<h3>date: <?php echo $rs['data']; ?> click number: <?php echo $rs['hits']; ?></h3>
<hr>
<p>contents:<?php echo $rs['contents']; ?></p>
The implementation of blog text is relatively simple. Get the blog id through a get request, and then query and display the title, date and text corresponding to the id through a sql statement.
An additional small function is to display a simple counter. Every time the page is refreshed, the number of clicks is incremented by 1.
Edit blog#Create the edit.php file in the ./wamp/www/blog/ directory.
<a href="index.php"><B>index</B></a> <a href="add.php"><B>add blog</B></a> <hr> <?phpinclude("conn.php"); //引入连接数据库 //获取数据库表数据if (!empty($_GET['id'])) { $edit = $_GET['id']; $sql = "select * from blog where id='$edit'"; $query = mysql_query($sql); $rs = mysql_fetch_array($query); }//更新数据库表数据if (!empty($_POST['sub'])) { $title = $_POST['title']; //获取title表单内容 $con = $_POST['con']; //获取contents表单内容 $hid = $_POST['hid']; $sql= "update blog set title='$title', contents='$con' where id='$hid' "; mysql_query($sql); echo "<script>alert('update success.');location.href='index.php'</script>"; }?>
<form action="edit.php" method="post"> <input type="hidden" name="hid" value="<?php echo $rs['id'];?>"> title :<br> <input type="text" name="title" value="<?php echo $rs['title'];?>"> <br><br> contents:<br> <textarea rows="5" cols="50" name="con" ><?php echo $rs['contents'];?></textarea><br><br> <input type="submit" name="sub" value="submit"> </form>
The function of editing blog is relatively complicated. The operation is divided into two steps. The first step is to query the title and text of the blog and display them in the input box. The second step is to update the edited content to the database.
Delete blogCreate the del.php file in the /www/blog/ directory.
<a href="index.php"><B>index</B></a> <a href="add.php"><B>add blog</B></a> <hr>
Finally, the blog deletion function is implemented, and the blog is queried and displayed through the ID.
Okay, this is how a blog is completed. Although the interface is not very beautiful, its functions are still complete. Friends who are interested should hurry up and practice it.
Related recommendations:
php blog website development example tutorial (1/8)_PHP tutorial
php blog website development example tutorial (1/8)
The above is the detailed content of Simple blog tutorial in PHP. For more information, please follow other related articles on the PHP Chinese website!