数据访问 之 删除操作 ------ 2017-04-27
1、实现效果:点击删除———出现提示对话框“是否确定删除按钮”————点取消则不删除数据、点确定则删除数据
2、效果图如下:
(1)点击删除
(2)出现提示对话框
(3)确定删除
(4)查看数据库是否也已经删除
3、代码如下
main.php代码
重点:
(1)考虑到用户的交互性,性别不能显示为1,0;所以要转换一下;
(2)同样民族的也不能输出代号;---查询或连接查询
(3)删除操作要有提示 -- onlick事件
<body> <h1>对表的信息进行操作</h1> <table width="50%" border ="1px" cellpadding="0px" cellspacing="0px"> <tr> <td>code</td> <td>name</td> <td>pwd</td> <td>sex</td> <td>birthday</td> <td>nationality</td> <td>delete</td> <td>increase</td> </tr> <?php $db = new MySQLi("localhost","root","123","hw_0408"); $sql = "select * from zhuce"; $result = $db->query($sql); $arr = $result->fetch_all(); foreach($arr as $v) { $sex = $v[3]?"男":"女"; //第一种获取民族 //$sql1 = "select nationality from nationality where mzcode='{$v[5]}'"; //第二种获取民族 $sql1 = "select nationality from zhuce left join nationality on nationality.mzcode='{$v[5]}'"; $r1 = $db->query($sql1); $a1 = $r1->fetch_row(); echo "<tr> <td>{$v[0]}</td> <td>{$v[1]}</td> <td>{$v[2]}</td> <td>{$sex}</td> <td>{$v[4]}</td> <td>{$a1[0]}</td> <td><a href = 'del.php?code={$v[0]}' onclick=\"return confirm('确认删除吗?')\">delete</a></td> <td><a>increase</a></td> </tr>"; } ?>
del.php代码:
重点:
(1)用_get方式取要删除的code信息;
(2)返回原页面的两种方法:
<?php $code = $_GET["code"]; $db = new MySQLi("localhost","root","123","hw_0408"); $sql = "delete from zhuce where code='{$code}'"; if($db->query($sql)) { header("location:main.php"); /*echo "<script>window.location.href='main.php'</script>";*/ } else{ echo "删除失败"; } ?>