主页面
<h1>主页面</h1> <table width="100%" border="1" 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","123","mydb"); //2.判断是否出错 !mysqli_connect_error() or die("连接失败!"); //3.执行SQL语句 $result = $db->query("select * from Info"); //4.读取数据 $attr = $result->fetch_all(); foreach($attr as $v) { $sex = $v[2]?'男':'女'; //处理民族名称 $sqln = "select Name from Nation where Code='{$v[3]}'"; $rnation = $db->query($sqln); $attrn = $rnation->fetch_assoc(); echo "<tr> <td>{$v[0]}</td> <td>{$v[1]}</td> <td>{$sex}</td> <td>{$attrn['Name']}</td> <td>{$v[4]}</td> <td> <a href='Delete.php?code={$v[0]}'>删除</a> <a href='Update.php?code={$v[0]}'>修改</a> </td> </tr>"; } ?>
增添数据
add.php
<h1>添加数据</h1> <form action="AddChuLi.php" method="post"> <div>代号:<input type="text" name="code" /></div> <div>姓名:<input type="text" name="name" /></div> <div>性别: <input type="radio" value="男" name="sex" />男 <input type="radio" value="女" name="sex" />女 </div> <div>民族: <select name="nation"> <?php $db = new MySQLi("localhost","root","123","mydb"); $sql = "select * from Nation"; $r = $db->query($sql); $attr = $r->fetch_all(); foreach($attr as $v) { echo "<option value='{$v[0]}'>{$v[1]}</option>"; } ?> </select> </div> <div>生日:<input type="text" name="birthday" /></div> <div><input type="submit" value="添加数据" /></div> </form> <div><a href="main.php">主页面</a></div>
addchuli.php
<?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","123","mydb"); $sql = "insert into Info values('{$code}','{$name}',{$s},'{$nation}','{$birthday}')"; $result = $db->query($sql); if($result) { header("location:Add.php");//跳转到Add.php } else { echo "添加失败!"; }
删除数据
delete.php
<?php $code = $_GET["code"]; $db = new MySQLi("localhost","root","123","mydb"); $sql = "delete from Info where Code = '{$code}'"; $r = $db->query($sql); if($r) { header("location:main.php"); } else { echo "删除失败!"; }
修改主页
update.php
<body> <h1>修改数据</h1> <?php $code = $_GET["code"]; $db = new MySQLi("localhost","root","123","mydb"); $sinfo = "select * from Info where Code='{$code}'"; $r = $db->query($sinfo); $arr = $r->fetch_row(); //这个人的所有信息 ?> <form action="UpdateChuLi.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" value="男" name="sex" <?php echo $arr[2]?"checked='checked'":"" ?> />男 <input type="radio" value="女" name="sex" <?php echo $arr[2]?"":"checked='checked'" ?> />女 </div> <div>民族: <select name="nation"> <?php $sql = "select * from Nation"; $r = $db->query($sql); $attr = $r->fetch_all(); foreach($attr as $v) { if($v[0] == $arr[3]) { echo "<option selected='selected' value='{$v[0]}'>{$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> <div><input type="submit" value="修改数据" /></div> </form> <div><a href="main.php">主页面</a></div> </body>
updatechuli.php
<?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","123","mydb"); $sql="Update Info set Name='{$name}',Sex={$s},Nation='{$nation}',Birthday='{$birthday}' where Code='{$code}' "; $r = $db->query($sql); if($r) { header("location:main.php"); } else { echo "修改失败!"; }