:?:
练习一个非常简单的数据库查询,删除,修改,增加页面;
写了5个页面(不要在意命名细节):
0:再数据库里新建一个库:ss22;再库中建一个表u;
1:数据库连接;
2:信息首页,也是查询;
3:删除页;
4:修改页;
5:增加页;
新建文件ljsj.php
数据库连接页:
<?php
ini_set('display_errors','0');
$link=mysqli_connect('localhost','root','');
if(mysqli_connect_errno($link)){
echo mysqli_connect_error($link);exit;
}
mysqli_select_db($link,'ss22');
mysqli_set_charset($link,'utf8');
新建文件b.php
信息首页,查询页:
<?php
include './ljsj.php';
$sql='SELECT id,name,pwd,sex,age FROM u';
$re=mysqli_query($link,$sql);
if($re&&mysqli_num_rows($re)>0){
$u=array();
while($r=mysqli_fetch_assoc($re)){
$u[]=$r;
}
}
$i=1;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>信息管理</title>
</head>
<body>
<table border="1" width="700" align="center" cellpadding="0" cellspacing="0">
<caption><h2>信息管理</h2></caption>
<tr>
<th width="120">序号</th>
<th width="120">姓名</th>
<th width="120">城市</th>
<th width="120">性别</th>
<th width="120">年龄</th>
<th width="60">操作</th>
</tr>
<?php foreach($u as $v) :?>
<tr align="center">
<td><?php echo $i++?></td>
<td><?php echo $v['name']?></td>
<td><?php echo $v['pwd']?></td>
<td><?php
$s=$v['sex'];
switch($s){
case 0:
echo '女';
break;
case 1:
echo '男';
break;
}
?></td>
<td><?php echo $v['age']?></td>
<td>
<form action="shanc.php" method="post">
<input type="hidden" name="id" value="<?php echo $v[id] ?>">
<input type="submit" value="删除">
</form>
<form action="xg.php" method="post">
<input type="hidden" name="xm" value="<?php echo $v['name'] ?>">
<input type="hidden" name="cs" value="<?php echo $v['pwd'] ?>">
<input type="hidden" name="xb" value="<?php echo $v['sex'] ?>">
<input type="hidden" name="nl" value="<?php echo $v['age'] ?>">
<input type="hidden" name="id" value="<?php echo $v['id'] ?>">
<input type="submit" value="修改">
</form>
<a href="./sq.php"><input type="button" value="增加"><a>
</td>
</tr>
<?php endforeach;?>
</table>
</body>
</html>
<?php mysqli_close($link);?>
新建chanc.php
删除页:
<?php
include './ljsj.php';
$sql="DELETE FROM u WHERE id=".$_POST['id']."";
$re=mysqli_query($link,$sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>删除数据</title>
</head>
<body>
<h2>删除人员信息</h2>
<?php
if($re){
echo '删除成功';
}else{
echo '删除失败';
}
?>
<a href="./b.php"><input type="button" value="返回信息管理"></a>
</body>
</html>
<?php mysqli_close($link);?>
新建xg.php
修改页:
<?php
include './ljsj.php';
$sql= "UPDATE u SET name='".$_POST['xm']."',pwd='".$_POST['cs']."',sex='".$_POST['xb']."',age='".$_POST['nl']."' WHERE id=".$_POST['id']."";
$re=mysqli_query($link,$sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>修改数据</title>
<style type="text/css">
.b{width:260px;height:280px;margin:100px auto;border:1px solid #808080;text-align:center;}
.c{width:80px;height:30px;margin:0 auto;line-height:30px;color:red;}
</style>
</head>
<body>
<div class="b">
<h2>修改人员信息</h2>
<form action="xg.php" method="post">
姓名:<input type="text" name="xm" value="<?php echo $_POST['xm'] ?>"/><br/>
城市:<input type="text" name="cs" value="<?php echo $_POST['cs'] ?>"/><br/>
性别:          <label><input type="radio" name="xb" value="1"
<?php if($_POST['xb']==1){
echo 'checked';
} ?>
/>男</label>
<label><input type="radio" name="xb" value="0"
<?php if($_POST['xb']==0){
echo 'checked';
} ?>
>女</label><br/>
年龄:<input type="number" name="nl" value="<?php echo $_POST['nl'] ?>"/><br/><br/>
<input type="hidden" name="id" value="<?php echo $_POST['id'] ?>">
<a href="./b.php"><input type="button" value="返回信息管理"></a>
        
<input type="submit" value="修改" />
</form>
<br/>
<div class="c">
<?php
if($re && mysqli_affected_rows($link)>0){
echo '修改成功';
}else{
echo '修改失败';
}
?>
</div>
</body>
</html>
<?php mysqli_close($link);?>
新建sq.php
增加页:
<?php
include './ljsj.php';
$sql= "INSERT INTO u VALUES(NULL,'".$_POST['xm']."','".$_POST['cs']."',".$_POST['xb'].",".$_POST['nl'].")";
$re=mysqli_query($link,$sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>增加数据</title>
<style type="text/css">
.b{width:260px;height:280px;margin:100px auto;border:1px solid #808080;text-align:center;}
.c{width:80px;height:30px;margin:0 auto;line-height:30px;color:red;}
</style>
</head>
<body>
<div class="b">
<h2>增加人员信息</h2>
<form action="sq.php" method="post">
姓名:<input type="text" name="xm" /><br/>
城市:<input type="text" name="cs" /><br/>
性别:          <label><input type="radio" name="xb" value="1" checked />男</label>
<label><input type="radio" name="xb" value="0 ">女</label><br/>
年龄:<input type="number" name="nl" /><br/><br/>
<a href="./b.php"><input type="button" value="返回信息管理"></a>
        
<input type="submit" value="增加" />
</form>
<br/>
<div class="c"><?php if($re&&mysqli_affected_rows($link)){
echo '添加成功';
}elseif(empty($_POST['xm'])){
echo '请填写内容';
}else{
echo '添加失败';
}
?></div>
</div>
</body>
</html>
<?php mysqli_close($link);?>