这次作业包括以下几个方面:
0.配置文件(我的是conn.php文件)
//基础的我就不写了,这是mysqli设置字符编码的方式。网上很多假的……
mysqli_set_charset($conn,"utf8");
1.添加登录功能
//接收POST值的函数,比$_POST好的地方在于可以防止无法接收的情况。
function post($str)
{
$val = !empty($_POST[$str]) ? $_POST[$str] : null;
return $val;
}
//获取时的方法
$username = post('username');
//判断是否为空建议用empty函数
if( empty($username) || empty($password) )
//php中建议写else语句,否则也可能会报错,书写方式如下。和C不同的是,不能把括号和else断开。
if{
}else
{
}
//mysqli_query函数执行错误时返回值为布尔型,需要检查上面(如$sql语句)是否输入错误。
$result = mysqli_query($conn,$sql);//mysqli中需要加入两个参数 $row = mysqli_num_rows($result);//符合搜索的要求
if(!$row)//判断是否为空
{
echo "";
} //注意这里是window不是windows还有每句话的结尾应该加上;号,否则执行不了。
2.制作主页面
//这里用$_GET函数
function get($str)
{
$val = !empty($_GET[$str]) ? $_GET[$str] : 1;
return $val;
}
修改
删除
//这里是做分页展示的代码
$page = get('page');
$pagesize = 5;
//计算偏移量
$offset = ($page - 1) * $pagesize;
$sql = "select * from mes_info order by addtime desc limit $offset,$pagesize;";
$res = mysqli_query($conn,$sql);
//获取最大页数
$total = mysqli_num_rows($res);
3.添加增加功能
分为两个文件:add.php和insert.php
add.php
//日期的输入,用date函数。具体使用方法上网查看。
$addtime = date("Y-m-d H:i:s");
//插入数据到数据库
$sql = "insert into mes_info values('','$title','$content','$addtime')";
其他语句与index代码一样,都是套路,233333。
4.添加删除功能
分为两个文件:index.php和delete.php
index.php
删除
//接收当前记录的id【注意】:只有表单才能设置post,其他一律用get
$id = $_GET['id'];
//准备删除sql语句
$sql = "delete from mes_info where id='$id'";
5.制作修改功能
这个功能涉及到三个文件:index.php,edit.php和update.php
index.php
修改
//根据id查询
$sql = "select * from mes_info where id = '$id'";
//查询记录集
$res = mysqli_query($conn,$sql);
//取出结果
$row = mysqli_fetch_assoc($res);
update.php
//对数据进行验证
if (empty($title) || empty($content)) {
echo "";
exit;
}
//更新数据
$sql = "update mes_info set title='$title',content='$content',addtime='$addtime' where id='$id'";
6.添加查看功能