2017.4.27

页面的删改

一.页面的代码

<h1>显示info信息</h1>
<table width="100%" border="1px" cellpadding="0" cellpadding="0">
<tr>
<td>代号</td>
<td>姓名</td>
<td>性别</td>
<td>民族</td>
<td>操作</td>
</tr>
<?php
$db=new MySQLi("localhost","root","root","test_0306_nzh");
$db->query("set names utf8");
$sql="select*from info";
$result=$db->query($sql);
$arr=$result->fetch_all();
foreach($arr as $v)
{
//修改性别
$sex=$v[2]?"男":"女";
//修改民族
$sql1="select name from nation where code='{$v[3]}'";
$r1=$db->query($sql1);
$a1=$r1->fetch_row();
echo"<tr>

<td>{$v[0]}</td>
<td>{$v[1]}</td>
<td>{$sex}</td>
<td>{$a1[0]}</td>
<td>
<a href='del.php?code={$v[0]}' onclick="return confirm('确定?')">删除<a>
<a href='update.php?code={$v[0]}'>修改</a>
</td>
</tr>

";
}
?>
</table>
</body>
<script type="text/javascript"></script>

二.删除的代码

<?php

$code=$_GET["code"];

$db=new MySQLi("localhost","root","root","test_0306_nzh");
$db->query("set names utf8");
$sql="delete from info where code='{$code}'";
if($db->query($sql))
{
//跳转页面
header("location:main.php");
/* echo "<script>window.location.href='main.php'</script> ";*/
}
else{
echo"OK!";
}

?>

三.修改操作页面的代码

<?php
$code=$_GET["code"];
$db=new MySQLi("localhost","root","root","test_0306_nzh");
$db->query("set names utf8");
$sql="select * from info where code='{$code}'";
$result=$db->query($sql);
$arr=$result->fetch_row();

?>

<h1>修改数据</h1>
<form action="xiugai.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" name="sex" value="1" <?php
echo $arr[2]?"checked='checked'":"";?>/>
女<input type="radio" name="sex" value="0" <?php
echo $arr[2]?"":"checked='checked'";?>/></div>
<div>
民族:
<select name="nation">
<?php
$sqln="select * from nation";
$resultn= $db->query($sqln);
$arrn=$resultn->fetch_all();
foreach($arrn as $v)
{
if($arr[3]/*人员的民族代号*/==$v[0]/*option的民族代号*/){
echo"<option selected='selected' value='{$v[0]}'>{$v[1]}</option>";
}else{
echo"<option value='{$v[0]}'>{$v[1]}</option>";
}
}
?>
</select>
</div>
<input type="submit" value="提交修改" />
</form>

四.修改操作的代码

<?php
$code=$_POST["code"];
$name=$_POST["name"];
$sex=$_POST["sex"];
$nation=$_POST["nation"];

$db=new MySQLi("localhost","root","root","test_0306_nzh");
$db->query("set names utf8");
$sql="update info set
name='{$name}',sex='{$sex}',nation='{$nation}'where code='{$code}'";
if($db->query($sql))
{
header ("location:main.php");//跳转回主页面
}
else
{
echo"KO!";
}
?>

原文地址:https://www.cnblogs.com/nzhcww/p/6775092.html