按下列要求完成各个页面:(提示:在做此题前需先建立一数据库,包含一张表,此表至少包含 5 个字段(姓名,性别,兴趣爱好,家庭住址,备注)),
首先展示一下各个界面实现后的效果
页面大概如上述所示。接下来看一下各个功能的代码实现(代码折叠框上的名称即为此文件的名称):
1.创建mysql表
biao.php
<?php $servername = "localhost"; $username = "root";//数据库名称 $password = "root";//数据库密码 $dbname = "syFour";//数据库的名字 // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 使用 sql 创建数据表userinfo为表名 $sql = "CREATE TABLE userinfo ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30) NOT NULL, male VARCHAR(10) NOT NULL, hobby VARCHAR(255), address varchar(255), beizhu varchar (255) )"; if ($conn->query($sql) === TRUE) { echo "Table MyGuests created successfully"; } else { echo "创建数据表错误: " . $conn->error; } $conn->close();
2.添加界面显示:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> p{ font-weight: bold; } </style> </head> <body> <form action="insert.php" method="post" > <table border="1" align="center" width="500"> <thead > <tr> <td colspan="2" align="center" bgcolor="#808080" > <p>添加个人资料</p></td> </tr> </thead> <tbody> <tr> <td>真实姓名</td> <td><input type="text" name="name" id="name"></td> </tr> <tr> <td>性别</td> <td> <input type="radio" name="sex" value="男"/>男 <input type="radio" name="sex" value="女" />女 </td> </tr> <tr> <td>兴趣爱好</td> <td><input type="text" name="hobby" id="hobby"/></td> </tr> <tr> <td>家庭住址</td> <td> <select name="address"> <option>中国</option> <option>美国</option> <option>日本</option> <option>韩国</option> </select> </td> </tr> <tr> <td>备注</td> <td> <textarea name="bz" cols="50" rows="5"></textarea> </td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="提交" name="sub"/> <input type="reset" value="重置" name="reset"> </td> </tr> </tbody> </table> </form> </body> </html>
3.添加功能实现:
<?php $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "syFour"; $name = $_POST['name']; $sex = $_POST['sex']; $hobby = $_POST['hobby']; $address = $_POST['address']; if($_POST['bz']==null){ $bz="这家伙很懒,什么也没有留下"; }else{ $bz = $_POST['bz']; } // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } $sql = "INSERT INTO userinfo (name,male,hobby,address,beizhu) VALUES ('".$name."','".$sex."','".$hobby."','".$address."','".$bz."')"; if ($conn->query($sql) === TRUE) { echo "新记录插入成功"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } header("Location:select.php"); $conn->close();
4.查询全部功能实现:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> p{ font-weight: bold; } .wrapper{ width: 1000px; margin: 20px auto; } h2{ text-align: center; } </style> </head> <body> <div class="wrapper"> <h2>学生个人信息表</h2> <div class="add"> <a href="insert.html">添加个人资料</a> </div> <table width="960" border="1"> <tr> <th>ID</th> <th>姓名</th> <th>性别</th> <th>爱好</th> <th>地址</th> <th>备注</th> <th>操作</th> </tr> <?php $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "syFour"; // 创建连接 $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("连接失败: " . mysqli_connect_error()); } $sql = "SELECT * FROM userinfo order by id asc"; //结果集 $result = mysqli_query($conn, $sql); //解析结果集 $userNum = mysqli_num_rows($result); for($i=0;$i<$userNum;$i++){ $row = mysqli_fetch_assoc($result); echo"<tr>"; echo"<td>{$row['id']}</td>"; echo"<td>{$row['name']}</td>"; echo"<td>{$row['male']}</td>"; echo"<td>{$row['hobby']}</td>"; echo"<td>{$row['address']}</td>"; echo"<td>{$row['beizhu']}</td>"; echo"<td> <a href='javascript:del({$row['id']})'>删除</a> <a href='update.php?id={$row['id']}'>修改</a> </td>" ; echo"</tr>"; } //5.释放结果集 mysqli_free_result($result); mysqli_close($conn); ?> </table> </div> <script type="text/javascript"> function del(id){ if(confirm("确定删除此人信息?")){ window.location= "delete.php?id="+id; } } </script> </body> </html>
5.修改页面实现:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <?php $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "syFour"; // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } $id = $_GET['id']; $sql = mysqli_query($conn,"select * from userinfo where id = $id "); $sql_arr = mysqli_fetch_assoc($sql); ?> <form action="update2.php" method="post"> <label>ID: </label><input type="text" name="id" readonly value="<?php echo $sql_arr['id']?>"><br/> <label>姓名: </label><input type="text" name="name" value="<?php echo $sql_arr['name']?>"><br/> <label>性别:</label> <input type="radio" name="male" value="男" checked/>男 <input type="radio" name="male" value="女" />女 <br/> <label>兴趣爱好:</label><input type="text" name="hobby" value="<?php echo $sql_arr['hobby']?>"><br/> <label>家庭住址:</label> <select name="address" > <option>中国</option> <option>美国</option> <option>日本</option> <option>韩国</option> </select><br/> <label>备注:</label><input type="text" name="beizhu" value="<?php echo $sql_arr['beizhu']?>"><br/> <input type="submit" value="提交"> </form> </body> </html> <?php
6.修改功能实现:
<?php $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "syFour"; // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } //获取修改的用户信息 $id = $_POST['id']; $name = $_POST['name']; $male = $_POST['male']; $hobby = $_POST['hobby']; $address = $_POST['address']; $beizhu = $_POST['beizhu']; //更新数据 mysqli_query($conn,"update userinfo set name='$name',male='$male', hobby='$hobby',address='$address',beizhu='$beizhu' where id='$id'")or die('修改出现错误'.mysqli_error()); header("Location:select.php");
7.删除功能实现:
<?php $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "syFour"; // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } $id = $_GET['id']; mysqli_query($conn,"DELETE FROM userinfo WHERE id='$id'") or die("删除数据出错".mysqli_error()); header("Location:select.php");
到此基本功能基本实现,此实验主要练习了一下php对mysql数据库的具体操作,具体可以参考一下菜鸟教程相关案例。