php部分---人员表和民族表的显示、修改、删除

1.连接数据库 进行网页的显示

<table width="100%"  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","test1");
    mysqli_connect_error()?"连接失败":"";
    $sql = "select * from info";
    
    $result = $db->query($sql);
    $attr = $result->fetch_all();
    
        
        foreach($attr as $v)
        {
            $aa=$v[2]?"男":"女";                 //因为数据库中存储的性别是1和0,所以要用三元运算符转换成“男”“女”
            $sql1="select name from nation where code='{$v[3]}'";   //因为在人员表中存的民族名称是代号,所以再从民族表中 取出民族的名字  显示
            $result1=$db->query($sql1);
            $attr1=$result1->fetch_row();
            $v[3]=$attr1[0];
            echo "<tr><td>{$v[0]}</td><td>{$v[1]}</td><td>{$aa}</td><td>{$v[3]}</td><td>{$v[4]}</td><td><a href='shanchu.php?code=$v[0]' onclick=\"return confirm('确认删除么')\">删除</a></td><td><a href='xiugai.php?code=$v[0]'>修改</a></td></tr>";

            }

?>    
</table>

//删除操作:用a标签传值 并在传值的地址手写上传的值 再从下个页面用get方式取值

<a href='shanchu.php?code=$v[0]' onclick=\"return confirm('确认删除么')\">删除</a>

//修改操作:

<a href='xiugai.php?code=$v[0]'>修改</a>

删除处理页面

<?php
$code=$_GET['code'];
$db=new MySQLi("localhost","root","123","test1");
mysqli_connect_error()?"链接失败":"";
$sql="delete from info where code='{$code}'";
$result=$db->query($sql);
if($result)
{
    header("location:mian.php");
    }
    else
    {
        echo "删除失败!";
    }

修改界面

<?php
$code=$_GET['code'];             //取出用get方式穿过来的值  用作下面的条件
$db=new MySQLi("localhost","root","123","test1");
mysqli_connect_error()?"连接失败":"";
$sql="select * from info where code='{$code}'";   //把符合条件的一条信息全部取出 ,用作下面表单中的value值,  因为传值是通过表单中的name  取到表单中的value值
$result=$db->query($sql);
$attr=$result->fetch_row();

?>

<form action="xiugaichuli.php" method="post">
<input type="hidden" name="code" value="<?php echo $attr[0] ?>">       
<div>姓名:<input type="text" name="name" value="<?php echo $attr[1] ?>"></div>
<div>性别:<input type="radio" name="sex" value="1" <?php echo $attr[2]?"checked='checked'":""; ?>>男    checked=‘checked’是单选按钮的默认选中
          <input type="radio" name="sex" value="0" <?php echo $attr[2]?"":"checked='checked'"; ?>>女</div>
<div>民族:<select name="nation">
<?php
$sql1="select * from nation";
$result1=$db->query($sql1);           //本着能让用户选择就不让其填写的原则,把性别和民族做成单选和下拉的形式   下拉的形式要设为自动添加选项
$attr1=$result1->fetch_all();
foreach($attr1 as $v)
{
    if($attr[3]==$v[0])
    {
        echo "<option selected='selected' value='$v[0]'>{$v[1]}</option>";     selectde=‘selected’是下拉中的默认选中
    }
    else
    {
        echo "<option value='$v[0]'>{$v[1]}</option>";
        }
}
?>
</select></div>
<div>生日:<input type="text" name="birthday" value="<?php echo $attr[4] ?>"></div>
<div><input type="submit" value="修改"></div>
</form>

修改处理页面

<?php
$code=$_POST['code'];
$name=$_POST['name'];
$sex=$_POST['sex'];
$nation=$_POST['nation'];
$bir=$_POST['birthday'];
$db=new MySQLi("localhost","root","123","test1");
mysqli_connect_error()?"连接失败":"";
$sql="update info set name='{$name}',sex={$sex},nation='{$nation}',birthday='{$bir}' where code='{$code}'";
if($result=$db->query($sql))
{
    header("location:mian.php");
    
    }
    else
    {
        echo "修改失败!";
        }