进入博客目录新建index.php页面
<?php
require “XXXX/index.html”;//引入html页面
是否能进入localhost/xxx/index.php
注意,php5开始,已经废弃使用此函数,如需使用,请降低php的版本!
$con = mysql_connect("localhost", "root", "root"); //@加在前面屏蔽错误 建立一个公共文件,common.php,写入本代码
if (!$con) {
die("无法连接数据库:".mysql_error());
}
mysql_query("set names utf8"); //设置数据编码
mysql_select_db('blog'); //选择数据库
进入mysql建立表
注意id字段——————属性非负,A_I自动递增
//测试数据库是否连接
//$result=mysql_query("select * from article");
//echo $result;
显示表明已连接
$result=mysql_query("select * from article");是一个资源对象,需转成数组
$row=mysql_fetch_assoc($result);
print_r($row);//打印数组
可用while($row=mysql_fetch_assoc($result);){
print_r($row);//循环出多组数组
}
成功打印出之后审查处需要修改的地方,
嵌入
<?php
$result = mysql_query("select * from article");//资源对象,需转换成数组
while ($row = mysql_fetch_assoc($result)) {
?>
文章所在div
<?php } ?>
把原div内容去掉,引入<?php echo $row['title']; ?>,<?php echo $row['content']; ?>,<?php echo date('Y-m-d,H:i:s,$row['time']); ?>
新增内容
新增add.php <a href add.php
include ‘common.php’;
require 'XXXX/add.html';
查看add.html有没有表单提交。按钮类型是否submit
if($_POST){
//print_r($_POST); //查看表单是否提交
$title=$_POST['title']; //接受表单提交过来的值
$content=$_POST['content'];
$time=time();
//执行插入语句
mysql_query(“indsert into article (title,content,time)VALUES(‘$title’,'$content','$time')”);
echo "<script>alert("撰写成功");location.href=‘index.php’;</script>";
}
编辑内容
首页找到编辑按钮,<a href= “edit.php?id=<?php echo $row['id'] ?>"> //给它一个id
新建一个edit.php
include “common.php”;
require “XXXX/edit.htmle”;
$id= $_GET['id'] //接受id
$info = mysql_query(“select * from article where id=‘$id’”);
$article=mysql_fetch_assoc($info); //转成数组
在html页面中对应处value=写<?php echo $article['title']; ?>,<?php echo $article['content']; ?>写在textarea中间
最后确认表单提交三个要素
if($_POST){
//print_r($_POST); //查看表单是否提交
$title=$_POST['title']; //接受表单提交过来的值
$content=$_POST['content'];
$time=time();
执行更新语句
mysql_query("update article set title=‘$title’,content='$content',time='$time' where id ='$id' ");
echo "<script>alert("编辑成功");location.href=‘index.php’;</script>";
}
删除
找到删除按钮对应处添加<a href="del.php?id=<?php echo $row['id'];?>"
创建dl.php
$id=$_GET[‘id’];
mysql_query("delete from article where id='$id'");
echo "<script>alert("删除成功");location.href=‘index.php’;</script>";