Home > Article > Backend Development > Squeeze some time to blog-php&MySQL practice, blog-php_PHP tutorial
hi
You’re going to have hot pot tonight, take some time to write something, don’t let your boss find out
1. PHP and MySQL
5. Backend of article publishing system
5.2 Create configuration file and initialization file
In order to unify the configuration and facilitate management, there is also the need to reduce code redundancy.
Config.php and connect.php respectively
config.php
/*
* Configuration file
*/
//Configuration database related information
//Since it is a constant, use define
define( 'HOST', '127.0.0.1');
define('USERNAME', 'root');
define('PASSWORD', '');
connect.php
/*
* File linked to the database
* Mainly link to the database server and then select the database.
* The special thing is to set the character set.
* Then hope to judge each operation
*/
//Include configuration file
require_once 'config.php';
//Connect the database
if(!$con=mysqli_connect(HOST,USERNAME,PASSWORD)){
echo mysqli_error($con);
}
//Select database
if(mysqli_select_db($con, 'info')){
echo mysqli_error($con);
}
//Character set
if(mysqli_query($con,'set names utf8')){
echo mysqli_error($con);
}
After completion, test the linked file and it will be ok. Both mysqli and mysql are acceptable here, as long as the format is correct.
5.3 Publish article
Article publishing interface article.add.php
后台管理系统 | |
版权所有 |
不是很漂亮就是了,学习嘛
文章发布处理程序article.add.handle.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,问题依然存在,我晕啊。。。。)