码迷,mamicode.com
首页 > 其他好文 > 详细

文章发布系统学习记录

时间:2016-11-08 17:28:14      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:elf   wpc   zoj   code   vda   数据   txt   qml   gpt   

学习来源于慕课网

技术分享

1、配置基本常用的信息:config.php:

header("Content-type: text/html; charset=utf-8");
define(‘HOST‘, ‘127.0.0.1‘);
define(‘USERNAME‘, ‘root‘);
define(‘PASSWORD‘, ‘PASSWORD‘);

2、连接数据库:connect.php

require_once(‘config.php‘);
//链接数据库
if(!($con=mysql_connect(HOST,USERNAME,PASSWORD)))
{
    echo mysql_error();
}
//选择数据库
if(!mysql_select_db(‘test‘)){
    echo mysql_error;
}
//字符集
if(!mysql_query(‘set names utf8‘)){
    echo mysql_error();
}

 

3、添加文章,通过form表单提交数据到处理页面进行处理。处理页面:

$title = $_POST[‘title‘];
$author = $_POST[‘author‘];
$description = $_POST[‘description‘];
$content = $_POST[‘content‘];
$datetime = time();
$insertsql = "insert into article(title,author,description,content,dateline) 
VALUES (‘$title‘,‘$author‘,‘$description‘,‘$content‘,$datetime)";

 把传递过来的post信息入库,赋值给变量。拼写sql语句,使用mysql_query()函数执行插入操作。

4、文章列表

<?php
require_once (‘../connect.php‘);
$sql = "select * from article ORDER BY dateline desc";
$query = mysql_query($sql);
if($query&&mysql_num_rows($query)){
    while($row = mysql_fetch_assoc($query)) {
        $data[] = $row;
    }
}else{
    $data[] = array();
}
?>

查询数据库,把资源标识符存在变量query。通过mysql_fetch_assoc()函数把数据赋值给数组data[]。然后在html中通过foreach函数把数据一一列举出来。

<?php
    if(!empty($data)){
        foreach ($data as $value){
            ?>
            <tr>
                <td><?php echo $value[‘id‘]?></td>
                <td><?php echo $value[‘title‘]?></td>
                <td><a href="article.del.handle.php?id=<?php echo $value[‘id‘]?>">删除</a> <a href="article.modify.php?id=<?php echo $value[‘id‘]?>">修改</a></td>
            </tr>
            <?php
        }
    }
    ?>

在修改和删除链接中需要传入当前文章的id值。

5、修改文章
<?php
require_once(‘../connect.php‘);
$id = $_GET[‘id‘];
$query = mysql_query("select * from article WHERE id=$id");
$data = mysql_fetch_assoc($query);
?>

通过$_GET[‘id‘]获取当前文章的id值,通过指定id去读取对应的数据。然后显示出来。

 6、删除文章

<?php
require_once(‘../connect.php‘);
$id = $_GET[‘id‘];
$deletesql = "delete from article where id= $id";
if(mysql_query($deletesql)){
    echo "<script>alert(‘文章删除成功‘)</script>";
}else{
    echo "<script>alert(‘文章删除失败‘)</script>";
}

 PS:小错误

【1】id忘记设置成自动增长了。插入一条记录之后就添加不了记录。
【2】拼写错误
【3】少些了 echo
【4】忘记了单引号或者双引号 ‘’ “” $ 等符号。
【5】html中引入php代码需要封闭<?php ?>
【6】$_POST[‘id‘] $_GET[‘id‘],大写,[],‘‘,

文章发布系统学习记录

标签:elf   wpc   zoj   code   vda   数据   txt   qml   gpt   

原文地址:http://www.cnblogs.com/scqilin/p/6043273.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!