zoukankan      html  css  js  c++  java
  • php之微型博客的创建

    一,微型博客的开发思路

    微型博客的创建,确定无疑我们会用到PHP和mysql之间的增添删改查,首先来看一下思维导图:

    搭建好计算机里的apache php 和mysql的联动功能,打开phpmyadmin,创建一个数据库(phplearn),在这个数据库里创建一个数据表(news)。

    二,开发所需的各个项目

    1,公共模板(conn.php

    <?php

      @mysql_connect("localhost","root","")or die("mysql连接失败");

      @mysql_select_db("phplearn")or die("db连接失败");

      //@mysql_set_charset("gdk");

      mysql_query("set names 'gbk'");

    ?>

    上边用到了mysql及具体数据库的连接,分别用到了mysql_connect和mysql_select_db这两个函数,分别用来连接mysql和数据库phplearn。

    mysql_set_charset用于指定数据库编码,mysql_query是数据库sql语句执行函数,可直接在括号内写sql语句。

    值得注意的是“@”符号,它用于屏蔽mysql报错时的提示,避免用户体验不友好及安全性方面的考虑。

    die(),该函数用于数据库连接失败时给与错误提示。

    2, 添加博文页add.php

    <?php

     include(conn.php);

     if(!empty($_POST['sub'])){

      $title=$_POST[‘title’];

      $con=$_POST[‘con’];

      $sql="insert into 'news' ('id','title','dates','contents') values (null,'$title',now(),'$con');

      mysql_query($sql);

      echo "<script>alert('添加成功');location.href='index.php';</script>"

      }

    ?>

    <div class="con">

    <form action="add.php" method="post">

    标题<input type"text" name="title"><br/>

    内容<textarea rows='5' cols='50' name='con'></textarea><br/>

    <input type=‘submit’ name='sub' value='发表'>

    </form>

    </div>

    include(conn.php)调用指定文件;

    empty()判断值是否为空;

    $_post获取表单post提交方式的值;

    insert into‘表名’ (‘字段1’,‘字段2’,‘字段3’,‘字段4’.。。。)values(‘值1’,‘值2’,‘值3’,‘值4’.。。。),

    sql插入语句;

    location.href="",js页面跳转。

    3,首页index.php

    <div class="nav">

    <button><a href="add.php">发博文</a></button>

    <from action="" method="get">

    <input type="text" name="keys" />

    <input type="submit" name="subs" value="搜索" />

    </form>

    </div>

    <?php

     include("conn.php");

     if (!empty($_GET[keys])){

       $w= 'title' like '%"._GET[keys]."%'";

    } else[$w=1;}

    $sql="select * from 'news' where $w order by id desc limit 10";

    $query=mysql_query($sql);

    while(mysql_fetch_array($querry)){

    ?>

    <div class="main">

    <h1>

    <a href="view.php?id= <?php $rs['id'] ?>"><?php echo $rs['title'] ?></a>

    </h1>

    <p><?php echo $rs['contents'] ?></p>

    <span><?php echo $rs['dates'] ?></span>

    <p class="oper">

    <a href="edit.php?id= <?php $rs['id']?>">编辑</a>

    <a href="del.php?id= <?php $rs['id'] ?>">删除</a>

    </p>

    </div>

    <?php

     }

    ?>

    select * from '表名' [where] [order] [limit], sql 查询语句。

    $_GET表单get提交方式,不同于post,是用于查询,运行效率高,但安全性较差。

    mysql_fetch_array(),将数据库资源类型转换为数组。

    4,删除博文页del.php

    <?php

     include("conn.php");

     if(!empty($_GET['del'])){

     $d=$_GET['del'],;

     $sql="delete from 'news' where 'id'='$d'";

     mysql_query($sql);

     echo "<scripr>alert('删除成功'); localtion.herf='index.php';</script>";

     }

    ?>

    delete from '表名' [where]...,删除sql语句。

    }  

    ?>

    5,修改博文页面edit.php

    <?php

      include("conn.php");

      if(!empty($_GET['id'])){

      $id=$_GET['id'];

      $sql="select * from 'news' where 'id'=['$id']";

      $query=mysql_query('$sql');

      $rs=mysql_fetch_array($query);

     }

      if(!empty($_POST['hid'])){

      $title=$_POST['title'];

      $con=$_POST['contents'];

      $hid=$_POST['hid'];

      $sql="update 'news' set 'title'='$title' 'contents'='$con' where 'id'='$hid' limit 1"

      echo "<script> alert ('更新成功'); location.href='index.php';</script>"

      }

    ?>

    <div class="con">

    <form action="edit.php" method="post">

    <input type="hiden" name="hid" value="<?php echo $rs['id'] ?>">

    标题<input type="text" name="title" value="<?php echo $rs['title'] ?>">

    内容<textarea rows="5" cols="50" name="con"><?php echo $rs['contents'] ?></textarea><br/>

    <input type="submit" name="sub" value="发表">

    </form>

    </div>

    更新指定id的数据,需要获取对应指定id,因此需要设置指定id以供调取。

    6,博文页内容view.php

    <div class='nav'>

    <button><a href="index.php">回到主页</a></button>

    </div>

    <?php

      include("conn.php");

      

      if(!empty($_GET['id'])){

      $sql="select * from 'news' where 'id'='".$_GET['id']"'";

      $query=mysql_query($sql);

      $rs=mysql_fetch_array($query);

      $sqlup="update 'news' set hits=hits+1 where 'id'='"._GET['id']."'";

      mysql_query($sqlup);

      }  

    ?>

    <div class="main">

    <h1><?php echo $rs['title'] ?></h1>

    <span><?php echo $rs['date'] ?></span>

    <span>点击量; <?php echo $rs['hits']></span>

    </hr>

    </p>

    </div>```

      

  • 相关阅读:
    Asp.net 中 listbox 滚动条 定位 火麒
    Silverlight跨域访问WCF服务 火麒
    网页加载速度优化技巧
    很漂亮的login
    ireport
    方法1,定位,相当于四周受力
    方法2,平移
    大端小端数据存储方式
    c++中RTTI
    C语言细节数组a+1和&a+1
  • 原文地址:https://www.cnblogs.com/whrTC/p/9183307.html
Copyright © 2011-2022 走看看