php对mysql的增删改
<?php
//连接到数据库 $db=new MySQLi("localhost","root","","z_stu"); !mysqli_connect_error() or die("连接失败"); $db->query("set names utf8"); //查询老师列表 $sql="select * from teacher"; //执行sql语句 $result=$db->query($sql); //将结果转换成数组 $attr=$result->fetch_all(); ?> <table border="1" width="50%"> <caption>老师表</caption> <tr> <th>编号</th> <th>姓名</th> <th>性别</th> <th>生日</th> <th>职称</th> <th>所在系</th> <th>操作</th> </tr> <?php //循环打印老师信息 foreach($attr as $k=>$v){?> <tr> <td><?php echo $v[0]; ?></td><!--编号--> <td><?php echo $v[1]; ?></td><!--姓名--> <td><?php echo $v[2]=="男" ? "男" : "女";?></td><!--性别--> <td><?php echo substr($v[3],0,10);?></td><!--截取生日到天--> <td><?php echo $v[4]; ?></td><!--职称--> <td><?php echo $v[5]; ?></td><!--所在系--> <td> <!-- 方法一: <a href="chuli/delete.php?uid=老师编号"> <button>删除</button> </a> --> <!--方法2:--> <form action="chuli/delete.php" method="post"> <input type="hidden" name="uid" value="<?php echo $v[0]; ?>"> <button onClick="del(this)" class="btn" uid="<?php echo $v[0]; ?>" >删除</button> </form> <a href="chuli/update.php?type=update&tno=<?php echo $v[0]; ?>"> <button>编辑</button> </a> </td> </tr> <?php } ?> </table> <a href="chuli/update.php?type=add"> <button>添加数据</button> <script>
2、删除
<?php //获取传值 $uid=$_POST["uid"]; $useid=$_GET["useid"]; //连接到数据库 $db=new MySQLi("localhost","root","","z_stu"); !mysqli_connect_error() or die("连接失败"); $db->query("set names utf8"); //写删除sql语句 $sql="delete from teacher where tno='$uid'"; //执行sql语句 $result=$db->query($sql); if($result){ header("location:../index.php"); }else{ header("location:../index.php?id=1"); } ?>
3、编辑和添加数据都是对老师信息进行操作,所以可以用一个php文件进行操作
<?php //接收编辑传过来的值 $type=$_GET["type"]; $tno=$_GET["tno"]; //连接到数据库 $db=new MySQLi("localhost","root","","z_stu"); !mysqli_connect_error() or die("连接失败"); $db->query("set names utf8"); if($type=="update"){ $sql="select * from teacher where tno='$tno'"; $result=$db->query($sql); $attr=$result->fetch_row(); } $proName=array("助教","副教授","教授","讲师"); $dpName=array("计算机系","电子工程系","数学系"); ?> <fieldset> <legend> <?php echo $type=="update"? "编辑":"添加" ?> 数据 </legend> <form action="insert.php" method="post"> <input type="hidden" name="type" value="<?php echo $type ?>"><!-- --> <table> <tr> <td>编号</td> <td> <input type="text" name="tno" value="<?php echo $attr[0]==null ?"":$attr[0];?> " <?php echo $type=="update"? readonly:""; ?> > </td> </tr> <tr> <td>姓名</td> <td> <input type="text" name="tname" value="<?php echo $attr[1]==null ?"":$attr[1];?> "> </td> </tr> <tr> <td>性别</td> <td> <input type="radio" name="tsex" value="男" <?php echo $attr[2] == "男"? "checked":"" ?> >男 <input type="radio" name="tsex" value="女" <?php echo $attr[2] == "女"? "checked":"" ?> >女 </td> </tr> <tr> <td>出生年月日</td> <td> <input type="text" name="tbirthday" class="form_datetime" value="<?php echo $attr[3]==null ?"":$attr[3];?>"> </td> </tr> <tr> <td>职称</td> <td> <select name="pro" id=""> <?php foreach($proName as $v){ if($v==$attr[4]){ echo "<option selected> $v</option>"; }else{ echo "<option> $v</option>"; } } ?> </select> </td> </tr> <tr> <td>所在系</td> <td> <select name="depan" id=""> <option >计算机系</option> <option >电子工程系</option> </select> </td> </tr> </table> <button >提交</button> </form> </fieldset>
效果:
接收数据,并执行相应的sql语句
<?php //这个页面用来添加传过来的值到teacher表中 $type=$_POST["type"]; $tno=$_POST["tno"];//老师编号 $tname=$_POST["tname"];//老师姓名 $tsex=$_POST["tsex"];//老师性别 $tbirthday=$_POST["tbirthday"];//老师生日 $prof=$_POST["pro"];//老师职称 $depan=$_POST["depan"];//所在系 //连接到数据库 $db=new MySQLi("localhost","root","","z_stu"); !mysqli_connect_error() or die("连接失败"); $db->query("set names utf8"); //写添加sql语句 if($type=="add"){ $sql="insert into teacher(tno,tname,tsex,tbirthday,prof,depan) values('$tno','$tname','$tsex','$tbirthday','$prof','$depan')"; }else if($type=="update"){ $sql="update teacher set tname='$tname',tsex='$tsex',tbirthday='$tbirthday',prof='$prof',depan='$depan' where tno='$tno'"; } $result=$db->query($sql); header("location:../index.php"); ?>