create table bb(id int primary key auto_increment,username varchar(50),password
char(32),registerTime int,ip int unsigned) default charset=utf8,engine=innodb;
//语句的正确写法在php文件中
$sql = "insert into bb(username,password,registerTime,ip) values(‘$username’,‘$password’,$register_time,$ip)";
date_default_timezone_set('PRC');修改时区为中华人民共和国时间
///插入数据:连接数据库、执行insert into语句就可以
///获取数据:连接数据库、执行select from bb;语句就可以;
///分页:首先要知道总页数、每页显示几个数据
获取总的数据:select count() as count from bb;
ceil($total_page/$num);ceil是向上取整;
floor(x)向下取整
//$_GE的使用获取地址栏的参数page
$_GET获取地址中页数,参数拼接用?page=¥num
http://127.0.0.1/userlist4.php?page=1
$_REQUEST['id'];这个函数既可以获取post也可以是get请求的来的数据
get请求的来的是一个字符串,post请求来的是一个数组
join(',',array);可以把一个数组转换成字符串。
$sql = 'select * from registerUser limit '.$offset.', '.$num.'';字符串拼接很重要
echo '<td><a href="edit.php?id='.$id.'">编辑</a></td>';超链接是get传递
///宏定义
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PWD', '123456');
define('DB_NAME', 'kongkong');
define('DB_CHARSET', 'utf8');
date_default_timezone_set('PRC');
删除
<?php
include 'config.php';
$link = mysqli_connect(DB_HOST, DB_USER, DB_PWD);
if (!$link) {
die('数据库连接失败');
}
if (!mysqli_select_db($link, DB_NAME)) {
die('选择数据库失败');
}
mysqli_set_charset($link, DB_CHARSET);
/*
获取id这个字段的值,不论你是get传递还是post传递。
你通过get传递过来的是一个字符串,你通过post传递过来的是一个数组,所以我可以根据你是不是一个数组来对你进行删除
*/
$ids = $_REQUEST['id'];
/*
$sql = delete from registerUser where id=$ids;
$ids = [1, 3, 4, 7];
$str = join(', ', $ids); implode explode
$str = '1, 3, 4, 7';
$sql = delete form registerUser where id in (1, 3, 4, 7);
*/
if (is_array($ids)) {
$str = join(',', $ids);
$sql = 'delete from registerUser where id in ('.$str.')';
} else {
//$sql = "delete from registerUser where id=$ids";
$sql = 'delete from registerUser where id='.$ids;
}
$result = mysqli_query($link, $sql);
if ($result && mysqli_affected_rows($link)) {
echo '删除成功 <a href="user_list.php">返回列表页</a>';
} else {
echo '删除失败';
}
mysqli_close($link);
编辑
<?php
include 'config.php';
$link = mysqli_connect(DB_HOST, DB_USER, DB_PWD);
if (!$link) {
die('数据库连接失败');
}
if (!mysqli_select_db($link, DB_NAME)) {
die('数据库选择失败');
}
mysqli_set_charset($link, DB_CHARSET);
$id = $_GET['id'];
$sql = 'select * from registerUser where id='.$id;
$result = mysqli_query($link, $sql);
if ($result && mysqli_affected_rows($link)) {
$data = mysqli_fetch_assoc($result);
$name = $data['username'];
$time = date('Y-m-d H:i:s', $data['registerTime']);
$ip = long2ip($data['ip']);
} else {
die('更新失败');
}
mysqli_close($link);
$str = <<<"ABCD"
<table width="800" border="1">
<tr>
<td>用户名</td>
<td><input type="text" name="username" value=$name></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td>注册时间</td>
<td><input type="text" name="time" value="$time"></td>
</tr>
<tr>
<td>注册ip</td>
<td><input type="text" name="ip" value="$ip"></td>
</tr>
</table>
ABCD;
echo $str;
注册
<?php
include 'config.php';
//提取用户输入的数据
$username = trim($_POST['username']);
$password = $_POST['password'];
if (strlen($password) < 3) {
exit('密码长度不得小于3位');
}
$repassword = $_POST['repassword'];
//判断两次密码是否相等
if (strcmp($password, $repassword)) {
exit('两次密码不相等');
}
//将用户的信息存入到数据库中
$password = md5($password);
//用户的注册时间
$time = time();
//用户注册ip
$ip = $_SERVER['REMOTE_ADDR'];
if (!strcmp($ip, '::1')) {
$ip = '127.0.0.1';
}
$ip = ip2long($ip);
/*
连接数据库,插入数据
*/
$link = mysqli_connect(DB_HOST, DB_USER, DB_PWD);
if (!$link) {
exit('数据库连接失败');
}
$db = mysqli_select_db($link, DB_NAME);
if (!$db) {
exit('数据库选择失败');
}
mysqli_set_charset($link, DB_CHARSET);
//准备sql语句
$sql = "insert into registerUser(username, password, registerTime, ip) values('$username', '$password', $time, $ip)";
//echo $sql;
//exit;
//执行sql语句
$result = mysqli_query($link, $sql);
if ($result && mysqli_affected_rows($link)) {
echo '注册成功 <a href="user_list.php">用户列表</a>';
} else {
echo '注册失败';
}
mysqli_close($link);
用户列表
<?php
include 'config.php';
$link = mysqli_connect(DB_HOST, DB_USER, DB_PWD);
if (!$link) {
exit('数据库连接失败');
}
$db = mysqli_select_db($link, DB_NAME);
if (!$db) {
die('数据库选择失败');
}
mysqli_set_charset($link, DB_CHARSET);
//分页
//获取数据表中数据行数
$sql = 'select count(*) as count from registerUser';
$result = mysqli_query($link, $sql);
if ($result) {
$data = mysqli_fetch_assoc($result);
//获取总的数据行数
$total_count = $data['count'];
}
//每一页要显示多少条数据
$num = 5;
//求出总的页数
$total_page = ceil($total_count / $num);
//得到当前需要让我显示的页数
$page = empty($_GET['page']) ? 1 : $_GET['page'];
//对当前页做出判断
if ($page < 1) {
$page = 1;
}
if ($page > $total_page) {
$page = $total_page;
}
//0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
//01234 limit 0, 5 1
//56789 limit 5, 5 2
//11121314 limit 10, 5 3
$offset = ($page - 1) * $num;
$sql = 'select * from registerUser limit '.$offset.', '.$num.'';
$result = mysqli_query($link, $sql);
if ($result && mysqli_affected_rows($link)) {
echo '<form action="delete.php" method="post">';
echo '<table width="800" border="1">';
while ($data = mysqli_fetch_assoc($result)) {
$id = $data['id'];
$name = $data['username'];
$time = date('Y-m-d H:i:s', $data['registerTime']);
$ip = long2ip($data['ip']);
echo '<tr>';
echo '<td><input type="checkbox" name="id[]" value="'.$id.'"></td>';
echo '<td>'.$id.'</td>';
echo '<td>'.$name.'</td>';
echo '<td>'.$time.'</td>';
echo '<td>'.$ip.'</td>';
echo '<td><a href="delete.php?id='.$id.'">删除</a></td>';
echo '<td><a href="edit.php?id='.$id.'">编辑</a></td>';
echo '</tr>';
}
echo '</table>';
echo '<br /><input type="submit" value="删除"><br />';
echo '</form>';
} else {
echo '执行语句失败';
}
$prev = $page - 1;
$next = $page + 1;
$str = <<<"ABCD"
<a href="user_list.php">首页</a>
<a href="user_list.php?page=$prev">上一页</a>
<a href="user_list.php?page=$next">下一页</a>
<a href="user_list.php?page=$total_page">尾页</a>
ABCD;
echo $str;
mysqli_close($link);
表单列表
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form action="register.php" method="post">
<table width="800" border="1" align="center">
<caption><h1>注册信息</h1></caption>
<tr>
<td>用户名</td>
<td><input type="text" name="username" placeholder="请输入用户名"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password" placeholder="请输入密码"></td>
</tr>
<tr>
<td>重复密码</td>
<td><input type="password" name="repassword"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="dosubmit" value="注册"></td>
</tr>
</table>
</form>
</body>
</html>