PHP增删改查
用表格遍历数据库
</head> <body> <table width="100%" border="2" cellpadding="0" cellspacing="0" > <tr> <td>代号</td> <td>姓名</td> <td>性别</td> <td>生日</td> <td>班级</td> <td>选项</td> </tr> <?php $db = new MySQLi("localhost","root","123","student"); $sql = "select * from student"; $result = $db->query($sql); $attr = $result->fetch_row(); while($attr= $result->fetch_row()) { if($attr[2]==1) { $attr[2]="男"; } else { $attr[2]="女"; } echo "<tr> <td>$attr[0]</td> <td>$attr[1]</td> <td>$attr[2]</td> <td>$attr[3]</td> <td>$attr[4]</td> <td><a href='shanchu.php?code={$attr[0]}'>删除</a></td> <td><a href='xiugai.php?code={$attr[0]}'>修改</a></td> </tr>"; } ?> </table> </body> </html>
加入修改页面
<?php $code = $_GET["code"]; $db = new MySQLi("localhost","root","123","student"); $sql = "select * from student where sno = '{$code}'"; $result = $db->query($sql); $attr = $result->fetch_row(); ?> <form action="xiugaichuli.php" method="post"> <div>编号:<input type="text" name="uid" value="<?php echo $attr[0] ?>" readonly="readonly" /></div> <div>姓名:<input type="text" name="wnd" value="<?php echo $attr[1] ?> " /></div> <div>性别:<input type="radio" name="sex" value="1" <?php echo $attr[2]?"checked='checked'":""; ?> />男<input type="radio" name="sex" value="0" <?php echo $attr[2]?"'":"checked='checked"; ?> />女 </div> <div>生日:<input type="text" name="shengri" value="<?php echo $attr[3] ?>"/></div> <div>班级:<input type="text" name="class" value="<?php echo $attr[4] ?>"/></div> <input type="submit" value="修改" /> </form>
加入修改处理页面
<?php $sno = $_POST["uid"]; $name = $_POST["wnd"]; $sex = $_POST["sex"]; $birthday = $_POST["shengri"]; $class = $_POST["class"]; $db = new MySQLi("localhost","root","123","student"); $sql = " update student set sname = '{$name}',ssex = '{$sex}',sbirthday = '{$birthday}',class = '{$class}' where sno = '{$sno}'"; $r = $db->query($sql); echo $sql; if($r) { header("location:biaoge.php"); } else { echo "修改失败"; } ?>