一、建立主页面:
<title>主页面</title> </head> <body> <h1>主页面</h1> <table border="1" width="100%" cellpadding="0" cellspacing="0"> <tr> <td>代号</td> <td>姓名</td> <td>性别</td> <td>民族</td> <td>生日</td> <td>操作</td> </tr> <?php //1.创造连接对象 $db=new MySQLi("localhost","root","","mydb"); //2. 判断是否出错 !mysqli_connect_error() or die("连接出错!"); //3. 写SQL语句 $sql="select * from info"; //4. 读取数据 $result=$db->query($sql); //5. 执行语句 if($result) { $arr=$result->fetch_all(); foreach ($arr as $value) { //处理性别: $sex=$value[2]?'男':'女'; //处理民族: $sqln="select Name from nation where Code='{$value[3]}'"; $resultn=$db->query($sqln); $arrn=$resultn->fetch_assoc(); echo "<tr> <td>{$value[0]}</td> <td>{$value[1]}</td> <td>{$sex}</td> <td>{$arrn['Name']}</td> <td>{$value[4]}</td> <td> <a href='Delete1.php?code={$value[0]}'>删除</a> <a href='Update1.php?code={$value[0]}'>修改</a> </td> </tr>"; } } ?> </table>
<!--转到增加数据的页面 -->
<div> <a href="Add1.php">添加数据</a></div>
</body>
</html>
二、添加数据:
1.添加数据页面:
<title>添加数据</title> </head> <body> <h1>添加数据</h1> <form action="AddChu.php" method="post"> <div>代号:<input type="text" name="code"></div> <div>姓名:<input type="text" name="name"></div> <div>性别:<input type="radio" name="sex" value="男">男 <input type="radio" name="sex" value="女">女 </div> <div>民族:<select name="nation" style="172px"> <?php $db=new MySQLi("localhost","root","","mydb"); !mysqli_connect_error() or die("连接失败!"); $sql="select * from nation"; $result=$db->query($sql); if($result) { $arr=$result->fetch_all(); foreach ($arr as $value) { echo "<option value='{$value[0]}'>{$value[1]}</option>"; } } ?> </select> </div> <div>生日:<input type="text" name="birthday"></div><br> <div><input type="submit" value="添加数据"></div> <div><a href="Zhu1.php">主页面</a></div> </form> </body> </html>
2.添加数据后进行处理:
<?php $code = $_POST["code"]; $name = $_POST["name"]; $sex = $_POST["sex"]; $s=1; if($sex=="女") { $s=0; } $nation= $_POST["nation"]; $birthday = $_POST["birthday"]; $db=new MySQLi("localhost","root","","mydb"); !mysqli_connect_error() or die("连接失败"); $sql="insert into info values('{$code}','{$name}',{$s},'{$nation}','{$birthday}')"; $result= $db->query($sql); if($result) { header ("location:Add1.php"); //跳转到Add1.php } else { echo "添加数据失败!"; }
三、删除数据:
<?php $code=$_GET['code']; $db=new MySQLi("localhost","root","","mydb"); !mysqli_connect_error() or die("连接失败!"); $sql="delete from info where code='{$code}'"; $result=$db->query($sql); if($result) { header ("location:Zhu1.php"); } else { echo "删除失败!"; }
四、修改数据:
1.修改数据页面:
<body> <h1>修改数据</h1> <?php $code=$_GET["code"]; $db=new MySQLi("localhost","root","","mydb"); !mysqli_connect_error() or die("连接失败!"); $sinfo="select * from info where code='{$code}'"; $result=$db->query($sinfo); $arr=$result->fetch_row();//这个人的所有信息 ?> <form action="UpdateChu.php" method="post"> <div><input type="hidden" name="code" value="<?php echo $arr[0]?>"></div> <div>姓名:<input type="text" name="name" value="<?php echo $arr[1]?>"></div> <div>性别:<input type="radio" name="sex" value="男" <?php echo $arr[2]?"checked='checked'":"" ?>>男 <input type="radio" name="sex" value="女" <?php echo $arr[2]?"":"checked='checked'" ?>>女 </div> <div>民族:<select name="nation"> <?php $sql="select * from nation"; $resultn=$db->query($sql); $attr=$resultn->fetch_all(); foreach ($attr as $v) { if($v[0]==$arr[3]) { echo "<option value='{$v[0]}' selected='selected'>{$v[1]}</option>"; } else { echo "<option value='{$v[0]}'>{$v[1]}</option>"; } } ?> </select> </div> <div>生日:<input type="text" name="birthday" value="<?php echo $arr[4] ?>"></div><br> <div><input type="submit" value="修改数据"></div><br> </form> <div><a href="Zhu1.php">主页面</a></div> </body> </html>
2.修改数据后处理:
<?php $code = $_POST["code"]; $name = $_POST["name"]; $sex = $_POST["sex"]; $s=1; if($sex=="女") { $s=0; } $nation= $_POST["nation"]; $birthday = $_POST["birthday"]; $db=new MySQLi("localhost","root","","mydb"); !mysqli_connect_error() or die("连接失败"); $sql="Update Info set Name='{$name}',Sex={$s},Nation='{$nation}',Birtday='{$birthday} where Code='{$code}'"; $resultn= $db->query($sql); if($resultn) { header ("location:Zhu1.php"); //跳转到主页面 } else { echo "修改失败!"; }