Android程序员学PHP开发(28)-简单博客系统-PhpStorm

转载请注明出处: http://blog.csdn.net/iwanghang/
觉得博文有用,请点赞,请评论,请关注,谢谢!~


简单博客系统,发布,编辑,删除,数据库的练习~暂时不考虑安全性,先看GIF动图:



看一下数据库截图:



好了,看代码:

conn.php:

<?php
    /**
     * mysql_query — 发送一条 MySQL 查询
     */

    /**
     * 连接数据库(返回资源)
     */
    @mysql_connect("127.0.0.1:3306","root","") or die("mysql数据库连接失败");

    /**
     * 选择一个数据库作为默认的数据库使用
     */
    @mysql_select_db("blog")or die("db连接失败");

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='align:"right"'>
    <input type="text" name="keys" >
    <input type="submit" name="subs" >
</form>
<hr>

<?php
    include("conn.php"); //引入连接数据库

    $sql = "select id,hits,title,date,contents from simpleblog";

    $result = mysql_query($sql); // 只要放一个正确的sql就可以执行

    while (list($id, $hits, $title, $date, $contents) = mysql_fetch_row($result)){ // 遍历表内容
        echo "id = {$id}<br>";
        echo "hits = {$hits}<br>";
        echo "title = {$title}<br>";

        ?>titleLink: <a href="view.php?id=<?php echo $id; ?>"><?php echo $title."<br>"; ?></a><?php

        echo "date = {$date}<br>";
        echo "contents = {$contents}<br>";
        echo "contents = ".iconv_substr($contents,0,15)."...<br>"; // iconv_substr — 截取字符串的部分

        ?>edit: <a href="edit.php?id=<?php echo $id; ?>">edit</a><?php echo "<br>";
        ?>delete: <a href="del.php?id=<?php echo $id; ?>">delete</a><?php echo "<br>";
        echo "--------------------------------------------------<br>";
    }

add.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($_POST['sub'])) {
        $title = $_POST['title'];  //获取title表单内容
        $con = $_POST['con'];      //获取contents表单内容
        $sql= "insert into simpleblog 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>

view.php:

<a href="index.php"><B>index</B></a>
<a href="add.php"><B>add blog</B></a>
<hr>

<?php

    include("conn.php"); //引入连接数据库

    $id = $_GET['id'];

    $sql = "select id,hits,title,date,contents from simpleblog where id='$id'";

    $result = mysql_query($sql); // 只要放一个正确的sql就可以执行

    while (list($id, $hits, $title, $date, $contents) = mysql_fetch_row($result)) { // 遍历表内容
        echo "id = {$id}<br>";
        echo "hits = {$hits}<br>";
        echo "title = {$title}<br>";

        ?>titleLink: <a href="view.php?id=<?php echo $id; ?>"><?php echo $title."<br>"; ?></a><?php

        echo "date = {$date}<br>";
        echo "contents = {$contents}<br>";
        echo "contents = ".iconv_substr($contents,0,15)."...<br>"; // iconv_substr — 截取字符串的部分

        ?>edit: <a href="edit.php?id=<?php echo $id; ?>">edit</a><?php echo "<br>";
        ?>delete: <a href="del.php?id=<?php echo $id; ?>">delete</a><?php echo "<br>";
    }
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 simpleblog 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 simpleblog 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>
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 simpleblog where id='$del' ";
        mysql_query($sql);
        echo "delete success!";
    }
?>



转载请注明出处: http://blog.csdn.net/iwanghang/



欢迎移动开发爱好者交流
沈阳或周边城市公司有意开发Android,请与我联系
联系方式

微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值