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;
//語(yǔ)句的正確寫法在php文件中
$sql = "insert into bb(username,password,registerTime,ip) values(‘$username’,‘$password’,$register_time,$ip)";
date_default_timezone_set('PRC');修改時(shí)區(qū)為中華人民共和國(guó)時(shí)間
///插入數(shù)據(jù):連接數(shù)據(jù)庫(kù)曙蒸、執(zhí)行insert into語(yǔ)句就可以
///獲取數(shù)據(jù):連接數(shù)據(jù)庫(kù)、執(zhí)行select from bb;語(yǔ)句就可以肖油;
///分頁(yè):首先要知道總頁(yè)數(shù)臂港、每頁(yè)顯示幾個(gè)數(shù)據(jù)
獲取總的數(shù)據(jù):select count() as count from bb;
ceil($total_page/$num);ceil是向上取整审孽;
floor(x)向下取整
//$_GE的使用獲取地址欄的參數(shù)page
$_GET獲取地址中頁(yè)數(shù),參數(shù)拼接用佑力?page=¥num
http://127.0.0.1/userlist4.php?page=1
$_REQUEST['id'];這個(gè)函數(shù)既可以獲取post也可以是get請(qǐng)求的來(lái)的數(shù)據(jù)
get請(qǐng)求的來(lái)的是一個(gè)字符串打颤,post請(qǐng)求來(lái)的是一個(gè)數(shù)組
join(',',array);可以把一個(gè)數(shù)組轉(zhuǎn)換成字符串。
$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('數(shù)據(jù)庫(kù)連接失敗');
}
if (!mysqli_select_db($link, DB_NAME)) {
die('選擇數(shù)據(jù)庫(kù)失敗');
}
mysqli_set_charset($link, DB_CHARSET);
/*
獲取id這個(gè)字段的值揍移,不論你是get傳遞還是post傳遞反肋。
你通過(guò)get傳遞過(guò)來(lái)的是一個(gè)字符串,你通過(guò)post傳遞過(guò)來(lái)的是一個(gè)數(shù)組罕邀,所以我可以根據(jù)你是不是一個(gè)數(shù)組來(lái)對(duì)你進(jìn)行刪除
*/
$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">返回列表頁(yè)</a>';
} else {
echo '刪除失敗';
}
mysqli_close($link);
編輯
<?php
include 'config.php';
$link = mysqli_connect(DB_HOST, DB_USER, DB_PWD);
if (!$link) {
die('數(shù)據(jù)庫(kù)連接失敗');
}
if (!mysqli_select_db($link, DB_NAME)) {
die('數(shù)據(jù)庫(kù)選擇失敗');
}
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>注冊(cè)時(shí)間</td>
<td><input type="text" name="time" value="$time"></td>
</tr>
<tr>
<td>注冊(cè)ip</td>
<td><input type="text" name="ip" value="$ip"></td>
</tr>
</table>
ABCD;
echo $str;
注冊(cè)
<?php
include 'config.php';
//提取用戶輸入的數(shù)據(jù)
$username = trim($_POST['username']);
$password = $_POST['password'];
if (strlen($password) < 3) {
exit('密碼長(zhǎng)度不得小于3位');
}
$repassword = $_POST['repassword'];
//判斷兩次密碼是否相等
if (strcmp($password, $repassword)) {
exit('兩次密碼不相等');
}
//將用戶的信息存入到數(shù)據(jù)庫(kù)中
$password = md5($password);
//用戶的注冊(cè)時(shí)間
$time = time();
//用戶注冊(cè)ip
$ip = $_SERVER['REMOTE_ADDR'];
if (!strcmp($ip, '::1')) {
$ip = '127.0.0.1';
}
$ip = ip2long($ip);
/*
連接數(shù)據(jù)庫(kù)日熬,插入數(shù)據(jù)
*/
$link = mysqli_connect(DB_HOST, DB_USER, DB_PWD);
if (!$link) {
exit('數(shù)據(jù)庫(kù)連接失敗');
}
$db = mysqli_select_db($link, DB_NAME);
if (!$db) {
exit('數(shù)據(jù)庫(kù)選擇失敗');
}
mysqli_set_charset($link, DB_CHARSET);
//準(zhǔn)備sql語(yǔ)句
$sql = "insert into registerUser(username, password, registerTime, ip) values('$username', '$password', $time, $ip)";
//echo $sql;
//exit;
//執(zhí)行sql語(yǔ)句
$result = mysqli_query($link, $sql);
if ($result && mysqli_affected_rows($link)) {
echo '注冊(cè)成功 <a href="user_list.php">用戶列表</a>';
} else {
echo '注冊(cè)失敗';
}
mysqli_close($link);
用戶列表
<?php
include 'config.php';
$link = mysqli_connect(DB_HOST, DB_USER, DB_PWD);
if (!$link) {
exit('數(shù)據(jù)庫(kù)連接失敗');
}
$db = mysqli_select_db($link, DB_NAME);
if (!$db) {
die('數(shù)據(jù)庫(kù)選擇失敗');
}
mysqli_set_charset($link, DB_CHARSET);
//分頁(yè)
//獲取數(shù)據(jù)表中數(shù)據(jù)行數(shù)
$sql = 'select count(*) as count from registerUser';
$result = mysqli_query($link, $sql);
if ($result) {
$data = mysqli_fetch_assoc($result);
//獲取總的數(shù)據(jù)行數(shù)
$total_count = $data['count'];
}
//每一頁(yè)要顯示多少條數(shù)據(jù)
$num = 5;
//求出總的頁(yè)數(shù)
$total_page = ceil($total_count / $num);
//得到當(dāng)前需要讓我顯示的頁(yè)數(shù)
$page = empty($_GET['page']) ? 1 : $_GET['page'];
//對(duì)當(dāng)前頁(yè)做出判斷
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 '執(zhí)行語(yǔ)句失敗';
}
$prev = $page - 1;
$next = $page + 1;
$str = <<<"ABCD"
<a href="user_list.php">首頁(yè)</a>
<a href="user_list.php?page=$prev">上一頁(yè)</a>
<a href="user_list.php?page=$next">下一頁(yè)</a>
<a href="user_list.php?page=$total_page">尾頁(yè)</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>注冊(cè)信息</h1></caption>
<tr>
<td>用戶名</td>
<td><input type="text" name="username" placeholder="請(qǐng)輸入用戶名"></td>
</tr>
<tr>
<td>密碼</td>
<td><input type="password" name="password" placeholder="請(qǐng)輸入密碼"></td>
</tr>
<tr>
<td>重復(fù)密碼</td>
<td><input type="password" name="repassword"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="dosubmit" value="注冊(cè)"></td>
</tr>
</table>
</form>
</body>
</html>