PHP实现简易blog的制作

2019-05-02 18:33:57于丽

在./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['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>

编辑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['id'])) {
    $del = $_GET['id']; //删除blog
    $sql= "delete from blog where id='$del' ";
    mysql_query($sql);
    echo "delete success!";

  }

?>

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

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

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

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

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易采站长站。

相关文章 大家在看