- 创建phpweb数据库,数据库表websql
``sql CREATE TABLE
websql(
IDint(11) DEFAULT NULL COMMENT '自动编号',
namevarchar(20) DEFAULT NULL COMMENT '个人姓名',
agetinyint(4) DEFAULT NULL COMMENT '个人年龄',
Result` varchar(20) DEFAULT NULL COMMENT ‘个人成绩’ ) ENGINE=InnoDB DEFAULT CHARSET=utf8
2. phpstudy的www目录下新建phpweb文件夹新建conn.php数据库连接文件
```php
<?php
$conn = mysqli_connect("localhost", "root", "root");
@mysqli_set_charset($conn, utf8);
@mysql_query($conn, utf8);
if (mysqli_connect_errno($conn)){
echo "连接MySQL失败:".mysqli_connect_error();
}
列表首页,php语法和html表格嵌套从数据库查询出数据并分页,点击详情跳转detail.php页面通过ID传递参数 ```php <?php require_once(‘conn.php’); ?>
.aline_center { text-align: center; } .ca { text-align: center; }
<?php $conn = mysqli_connect(“localhost”, “root”, “root”, “phpweb”); $sql=mysqli_query($conn,”select count(*) as total from websql”); //建立统计有计录集总数查询; $info=mysqli_fetch_array($sql); //使用mysqli_fetch_array获取所有记录集; $total=$info[‘total’]; //定义变量$total值为记录集的总数; if($total==0) { echo “本系统暂无任何查询数据!”; } //如果记录总数为0则显示无数据; else { ?>
PHP成绩查询系统
<tr>
<td>序号</td>
<td>姓名</td>
<td>年龄</td>
<td>成绩</td>
<td>操作</td>
</tr>
<?php
$pagesize=5;
//设置每页显示5条记录;
if ($total<=$pagesize){
$pagecount=1;
//定义$pagecount初使变量为1页;
}
if(($total%$pagesize)!=0){
$pagecount=intval($total/$pagesize)+1;
//取页面统计总数为整数;
}else{
$pagecount=$total/$pagesize;
}
if((@ $_GET['page'])==""){
$page=1;
//如果总数小于5则页码显示为1页;
}else{
$page=intval($_GET['page']);
//如果大于5条则显示实际的总数;
}
$sql1=mysqli_query($conn,"select * from websql order by ID asc limit ".($page-1)*$pagesize.",$pagesize ");
//设置websql数据表按ID升序排序查询出所有数据; while($info1=mysqli_fetch_array($sql1)) //使用mysqli_fetch_array查询所有记录集,并定义为$info1; { ?> <tr> <td><?php echo $info1['ID'];?></td><!--显示ID字段--> <td><?php echo $info1['name'];?></td><!--显示name字段--> <td><?php echo $info1['age'];?></td><!--显示age字段--> <td><?php echo $info1['Result'];?></td><!--显示Result字段--> <td><a href="detail.php?ID=<?php echo $info1['ID'];?>">详情</a></td> <!--设置跳转并传递ID值--> </tr> <?php } ?> </table> <table width="600" border="1" align="center" cellpadding="1" cellspacing="1"> <tr> <td>共有数据 <?php echo $total;//显示总页数; ?> 条,每页显示 <?php echo $pagesize;//打印每页显示的总页码;?> 条, 第 <?php echo $page;//显示当前页码;?> 页/共 <?php echo $pagecount;//打印总页码数 ?> 页: <?php if($page>=2) //如果页码数大于等于2则执行下面程序 { ?> <a href="index.php?page=1" title="首页"><font face="webdings"> 9 </font></a> / <a href="index.php?id=<?php echo $id;?>&page=<?php echo $page-1;?>" title="前一页"><font face="webdings"> 7 </font></a> <?php } if($pagecount<=4){ //如果页码数小于等于4执行下面程序 for($i=1;$i<=$pagecount;$i++){ ?> <a href="index.php?page=<?php echo $i;?>"><?php echo $i;?></a> <?php } }else{ for($i=1;$i<=4;$i++){ ?> <a href="index.php?page=<?php echo $i;?>"><?php echo $i;?></a> <?php }?> <a href="index.php?page=<?php echo $page-1;?>" title="后一页"><font face="webdings"> 8 </font></a> <a href="index.php?id=<?php echo $id;?>&page=<?php echo $pagecount;?>" title="尾页"><font face="webdings"> : </font></a> <?php }?></td> </tr> </table> <?php
}
?>
![image.png](https://cdn.nlark.com/yuque/0/2022/png/1590333/1657059496236-483f63b0-a561-431e-be7d-e8c3fa0f8e14.png#clientId=u8ae89247-98da-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=236&id=u093f1ba8&margin=%5Bobject%20Object%5D&name=image.png&originHeight=260&originWidth=656&originalType=binary&ratio=1&rotation=0&showTitle=false&size=18234&status=done&style=none&taskId=ud0373066-a59e-4094-b0cc-8f34dc413db&title=&width=596.363623437803) 4. 列表详情页detail.php ```php <?php require_once('conn.php'); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>PHP成绩查询系统</title> <style type="text/css"> .aline_center { text-align: center; } .ca { text-align: center; } </style> </head> <body class="ca"> <p class="aline_center">PHP成绩查询系统</p> <hr/> <table width="600" border="1" align="center" cellpadding="1" cellspacing="1"> <tr> <td>序号</td> <td>姓名</td> <td>年龄</td> <td>成绩</td> <td>编辑</td> </tr> <?php $conn = mysqli_connect("localhost", "root", "root", "phpweb"); $ID = @ $_GET['ID']; $sql = mysqli_query($conn, "select * from websql where ID = '".$ID."'"); $info = mysqli_fetch_array($sql); ?> <tr> <td><?php echo $info['ID'];?></td> <td><?php echo $info['name'];?></td> <td><?php echo $info['age'];?></td> <td><?php echo $info['Result'];?></td> <td><a href="update.php?ID=<?php echo $info['ID'];?>">更新</a> / <a href="del.php?ID=<?php echo $info['ID'];?>">删除</a></td> </tr> </table> <hr/> <p> </p> </body> </html>
- 点击详情后点击更新根据ID值跳转update.php详情页面 ```php <?php require_once(‘conn.php’); ?>
PHP成绩查询系统
序号 姓名 年龄 成绩 <?php echo $info[‘ID’];?>