zoukankan      html  css  js  c++  java
  • 挤点时间写博客-php&MySQL实践

      hi

    晚上要吃火锅的嘛,挤点时间写点东西吧,别被老板发现哦

    1、PHP与MySQL

    五、文章发布系统之后台

    5.2 创建配置文件和初始化文件

    为了统一配置以及管理方便,还有就是减少代码的冗余。

    分别为config.php和connect.php

    config.php

    <?php
    /*
    * 配置文件
    */
    //配置数据库的相关信息
    //由于是常量,直接用define
    define('HOST', '127.0.0.1');
    define('USERNAME', 'root');
    define('PASSWORD', '');

    connect.php

    <?php
    /*
    * 链接到数据库的文件
    * 主要是链接到数据库服务器,然后选择数据库。
    * 特殊的是设置字符集。
    * 然后希望对每个操作进行判断
    */

    //包含配置文件
    require_once 'config.php';

    //连库
    if(!$con=mysqli_connect(HOST,USERNAME,PASSWORD)){
    echo mysqli_error($con);
    }

    //选库
    if(mysqli_select_db($con, 'info')){
    echo mysqli_error($con);
    }

    //字符集
    if(mysqli_query($con,'set names utf8')){
    echo mysqli_error($con);
    }

    完成后测试一下链接文件就ok了。这里的mysqli和mysql都可以,只要格式正确就行。

    5.3 发布文章

    文章发布界面article.add.php

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <style type="text/css">
    body {
    margin-left: 0px;
    margin-top: 0px;
    margin-right: 0px;
    margin-bottom: 0px;
    }
    </style>
    </head>

    <body>
    <table width="100%" height="520" border="0" cellpadding="8" cellspacing="1" bgcolor="#000000">
    <tr>
    <td height="89" colspan="2" bgcolor="#FFFF99"><strong>后台管理系统</strong></td>
    </tr>
    <tr>
    <td width="156" height="287" align="left" valign="top" bgcolor="#FFFF99"><p><a href="article.add.php">发布文章</a></p>
    <p><a href="article.manage.php">管理文章</a></p> <a href="article.add.php"></a></td>
    <td width="837" valign="top" bgcolor="#FFFFFF">
    <form id="form1" name="form1" method="post" action="article.add.handle.php">
    <table width="779" border="0" cellpadding="8" cellspacing="1">
    <tr>
    <td colspan="2" align="center">发布文章</td>
    </tr>
    <tr>
    <td width="119">标题</td>
    <td width="625"><label for="title"></label>
    <input type="text" name="title" id="title" /></td>
    </tr>
    <tr>
    <td>作者</td>
    <td><input type="text" name="author" id="author" /></td>
    </tr>
    <tr>
    <td>简介</td>
    <td><label for="description"></label>
    <textarea name="description" id="description" cols="60" rows="5"></textarea></td>
    </tr>
    <tr>
    <td>内容</td>
    <td><textarea name="content" cols="60" rows="15" id="content"></textarea></td>
    </tr>
    <tr>
    <td colspan="2" align="right"><input type="submit" name="button" id="button" value="提交" /></td>
    </tr>
    </table>
    </form></td>
    </tr>
    <tr>
    <td colspan="2" bgcolor="#FFFF99"><strong>版权所有</strong></td>
    </tr>
    </table>
    </body>
    </html>

    不是很漂亮就是了,学习嘛

    文章发布处理程序article.add.handle.php

    <?php
    require_once('../connect.php');
    //把传递过来的信息入库,在入库之前对所有的信息进行校验。
    if(!(isset($_POST['title'])&&(!empty($_POST['title'])))){
    echo "<script>alert('标题不能为空');window.location.href='article.add.php';</script>";
    }
    $title = $_POST['title'];
    $author = $_POST['author'];
    $description = $_POST['description'];
    $content = $_POST['content'];
    $dateline = time();
    $insertsql = "insert into article(title, author, description, content, dateline) values('$title', '$author', '$description', '$content', $dateline)";
    if(mysqli_query($con,$insertsql)){
    echo "<script>alert('发布文章成功');window.location.href='article.manage.php';</script>";
    }else{
    echo "<script>alert('发布失败');window.location.href='article.manage.php';</script>";
    }
    ?>

    注意两者的功能和连接,就是add页面把东西传给handle处理

     ------------------------

    由于我遇到了前所未见的乱码问题。。。跪着解决中。。。。望大家不吝赐教(wamp环境,mysql,zend,浏览器都已经设置为utf8,apache配置文件中添加了AddDefaultCharset UTF-8,问题依然存在,我晕啊。。。。)

  • 相关阅读:
    华为的管理变革之路
    产品创新型组织变革的四个阶段
    新产品如何在市场上快速取得成功?
    如何做好基础技术的创新?
    产品创新型总经理应具备哪些方面的素质?
    项目型组织如何快速过渡到产品型组织?
    华为是如何做技术规划和产品路标开发的?
    华为干部选拔和任用的标准
    导论:1、大学计算机——2、计算机信息数字化基础——二进制&数字化(数制)
    导论:1、大学计算机——1、计算机与问题求解
  • 原文地址:https://www.cnblogs.com/andy1202go/p/5013219.html
Copyright © 2011-2022 走看看