实例二增删改查
显示操作页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<form action="piliangshanchu.php" method="post" >
<table border="1px">
<tr>
<td>复选框</td>
<td>code</td>
<td>名字</td>
<td>性别</td>
<td>民族</td>
<td>生日</td>
<td>管理</td>
</tr>
<?php
//造对象
$db= new mysqli("localhost","root","","1");
//判断连接
!mysqli_connect_error() or die ("连接错误");
//sql语句
$sql="select * from info ";
//执行sql,返回结果对象
$result=$db->query($sql);
//纵览数组
while($arry=$result->fetch_row())
{ //sql语句
$sql="select Name from nation where code='{$arry[3]}'";
//执行sql返回结果对象
$result1=$db->query($sql);
//取数据
$arry1=$result1->fetch_row();
$sex=$arry[2]==1?"男":"女";
echo "<tr>
<td>
<input type='checkbox' name='ck[]' value='{$arry[0]}' class='ck' />
</td>
<td>{$arry[0]}</td>
<td>{$arry[1]}</td>
<td>$sex</td>
<td>{$arry1[0]}</td>
<td>{$arry[4]}</td>
<td><a onclick=\" return confirm('确定删除')\" href='16613shanchu.php?code={$arry[0]}'>删除</a><a href='16613xiugai-1.php?code={$arry[0]}'>修改</a></td>
</tr>";
}
?>
<tr>
<td><input type="checkbox" onclick="CheckAll(this)" />全选</td>
<td><input type="submit" value="批量删除"/></td>
</form>
</tr>
</table>
<a href="16613-2.php">添加</a>
</body>
</html>
<script type="text/javascript">
function CheckAll(a)
{
var ck = document.getElementsByClassName("ck");
//a.checked;
//document.getElementById().removeAttribute
for(var i=0;i<ck.length;i++)
{
if(a.checked)
{
ck[i].setAttribute("checked","checked");
}
else
{
ck[i].removeAttribute("checked");
}
}
}
</script>

添加页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<form action="16613zengjia.php" method="post">
<div>代号:<input type="text" name="code" /></div>
<div>姓名:<input type="text" name="name"/></div>
<div>性别:<input type="radio" name="sex" value="1"/>男<input name="sex" type="radio" value="0" />女</div>
<div>民族:<select name="nation" />
<?php
//创造连接对象
$db=new mysqli("localhost","root","","1");
//sql语句
$sql="select * from nation";
//执行sql返回结果对象
$result=$db->query($sql);
//取值
$attr=$result->fetch_all();
//纵览数组创造下拉选项
foreach ($attr as $a)
{
echo"<option value='{$a[0]}'>{$a[1]}</option>";
}
?>
</select>
</div>
<div>生日:<input type="text" name="birthday"/></div>
<input type="submit" value="提交添加"/></form>
</body>
</html>

添加处理
<?php
$code = $_POST["code"];
$name = $_POST["name"];
$sex = $_POST["sex"];
$nation = $_POST["nation"];
$birthday = $_POST["birthday"];
//造对象
$db=new mysqli("localhost","root","","1");
//sql语句
$sql="insert into info values ('{$code}','{$name}','{$sex}','{$nation}','{$birthday}')";
//执行sql
$result=$db->query($sql);
//判断执行是否成功
if($result)
{
header("location:16613-1.php");
}
else
{
echo "添加失败.'<br/>'.<a href='location:16613-2.php'>返回上一页</a>";
}
删除处理
<?php
$code=$_GET["code"];
//造对象
$db=new mysqli("localhost","root","","1");
//sql语句
$sql="delete from info where code='{$code}'";
//执行sql
$result=$db->query($sql);
//判断执行是否成功
if($result)
{
header("location:16613-1.php");
}
else
{
echo "删除失败.'<br/>'.<a href='location:16613-1.php'>返回上一页</a>";
}
修改页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<?php
//接收主键值
$code=$_GET["code"];
//创造连接对象
$db1=new mysqli("localhost","root","","1");
//sql语句
$sql1="select * from info where code='{$code}'";
//执行sql返回结果对象
$result1=$db1->query($sql1);
//取值
$attr1=$result1->fetch_row();
?>
<form action="16613xiugai-2.php" method="post">
<div>代号:<input type="text" name="code" readonly="readonly" value="<?php echo "$attr1[0]";?>"/></div>
<div>姓名:<input type="text" name="name" value="<?php echo "$attr1[1]";?>"/></div>
<div>性别:<input type="radio" name="sex" value="1" <?php echo $attr1[2]?"checked='checked'":"";?>/>男
<input name="sex" type="radio" value="0" <?php echo $attr1[2]?"":"checked='checled'"; ?>/>女</div>
<div>民族:<select name="nation" />
<?php
//创造连接对象
$db=new mysqli("localhost","root","","1");
//sql语句
$sql="select * from nation";
//执行sql返回结果对象
$result=$db->query($sql);
//取值
$attr=$result->fetch_all();
//纵览数组创造下拉选项
foreach ($attr as $a)
{
echo"<option value='{$a[0]}'>{$a[1]}</option>";
}
?>
</select>
</div>
<div>生日:<input type="text" name="birthday" value="<?php echo "$attr1[4]"?>"/></div>
<input type="submit" value="修改"/>
</form>
</body>
</html>
修改处理
<?php
$code=$_POST["code"];
$name=$_POST["name"];
$sex=$_POST["sex"];
$nation=$_POST["nation"];
$birthday=$_POST["birthday"];
//创造对象
$db= new mysqli("localhost","root","","1");
//sql命令
$sql="update info set name='{$name}',sex='{$sex}',nation='{$nation}',birthday='{$birthday}' where code='$code'";
//执行sql
$result=$db->query($sql);
//判断执行是否成功
if($result)
{
header("location:16613-1.php");
}
else
{
echo "修改失败";
}
批量删除处理
<?php
$ck = $_POST["ck"];
include("DBDA.class.php");
$db = new DBDA();
foreach($ck as $v)
{
$sql = "delete from Info where Code='{$v}'";
$db->Query($sql,0);
}
header("location:16613-1.php");
外部引用查询类
<?php
class DBDA
{
public $host="localhost"; //服务器地址
public $uid="root"; //用户名
public $pwd=""; //密码
public $dbconnect; //连接对象
//操作数据库的方法
//$sql代表需要执行的sql语句
//$type代表SQL语句的类型,1代表查询,2代表增删改
//$dbname代表操作的数据库名称
function Query($sql,$type=1,$dbname="1")
{ //造对象建连接
$this->dbconnect=new mysqli($this->host,$this->uid,$this->pwd,$dbname);
//判断连接是否出错
if (!mysqli_connect_error())
{//连接成功执行sql语句
$result=$this->dbconnect->query($sql);
if($type==1)
{ //查询语句
return $result->fetch_all();
}
else
{ //其他语句
return $result;
}
}
else
{
echo "连接失败";
}
}
}
浙公网安备 33010602011771号